greenfoot introduction (2)
DESCRIPTION
TRANSCRIPT
kakihijau.googlepages.com
Introduction To
GreenfootPart-2
2Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Disclaimer
This document is intended to give a beginner an introductory material for the Greenfoot system. Greenfoot is a software framework made by Poul Henriksen and Michael Koelling at University of Kent / Deakin University. More information can be found at http://www.greenfoot.org
This document is available „AS IS“ free of charge for personal use and non-commercial redistribution. It may not be sold for profit or included in other packages which are sold for profit without written authorisation. This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; your use of the document is at your sole risk. Reusing and modifying (part of) this document is allowed, as long as you state explicitly that your work is based on the original document produced by the copyright holder: Eueung Mulyana. The author can be contacted via eueung-[at]-yahoo.com (http://kakihijau.googlepages.com).
3Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Purpose
This is the second part of the document „Introduction to Greenfoot“ and focuses on understanding the wombats scenario
Part of the content of these slides is based on the Greenfoot Tutorial made by Michael Koelling, publicly accesible at the Greenfoot website, http://www.greenfoot.org
4Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Outline
Class Display Revisited Class Editor The Leaf Class The Wombat Class The WombatWorld Class
5Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Class Display Revisited
Class display contains all classes which are used in a scenario Class World and Actor are abstract superclasses, part of the
Greenfoot system Scenario wombats has 2 actor classes (Wombat, Leaf)
6Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Class Editor (1)
We can modify objects behaviour by editing the source code of the corresponding class
To display the source code editor:– Double-click the class– Right-click the class, choose the
item „Open editor“
7Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
Class Editor (2)
8Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Leaf Class
The simplest class compared to the Wombat and WombatWorld class
Leaf objects do nothing! As you can see in the previous slide, the Leaf
class has no statement and consists of an empty constructor
9Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (1)import necessary packages
class header;Wombat is a subclassfrom Actor
data (constants andvariables)
class constructor and methods
10Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (2)
4 constants : EAST, WEST, NORTH, SOUTH
2 variables : direction, leavesEaten
1 constructor : Wombat() 8 methods :
– getLeavesEaten(), foundLeaf(), eatLeaf()
– setDirection(), turnLeft()– canMove(), move()– act()
11Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (3)
constructor initialisingdirection and leavesEaten
this method is inherited fromthe superclass Actor
is there any Leaf objectin my position?
remove that Leaf objectupdate the variableleavesEaten
12Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (4)
13Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (5)
this method is inherited fromthe superclass Actor
14Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (6)
change direction 90 degreesto the left
15Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (7)
these methods areinherited from Actor
new coordinateif the object movesforward; depends on its direction
checks if the objectreaches the edges of the world
16Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (8)
17Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (9)
if object reaches one ofthe borders, do nothing!
setLocation() isinherited from Actor
the object movesforward; depends on its direction
18Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The Wombat Class (10)
if you find leaves, eat !
if you don‘t find leaves, but you can move forward, then move!
if you don‘t find leaves and you also cannot move, then turn to the left!
19Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The WombatWorld Class (1)
1 constructor : WombatWorld()
2 methods :– populate()– randomLeaves()
20Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The WombatWorld Class (2)
calls the World constructor
sets the background.the method is inheritedfrom the superclass World
cell.jpg
21Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The WombatWorld Class (3)
22Introduction to Greenfoot
Eueung Mulyana | kakihijau.googlepages.com
The WombatWorld Class (4)
Create a Leaf object at a randomposition (x,y); repeat howmanytimes