Objects First With Java

Download Report

Transcript Objects First With Java

Greenfoot
Programming
simulations and
games in Java
Lecture 4. Greenfoot
1
Pablo Romero, Department of Informatics
Lecture plan
Greenfoot in action
 The Wombats scenario
 Modifying the code

* Greenfoot Designed and implemented at the University of Kent and
Deakin University. Copyright held by Poul Henriksen and Michael
Koelling. Slides based on those by Eueung Mulyana
Lecture 4. Greenfoot
2
Pablo Romero, Department of Informatics
Greenfoot
Lecture 4. Greenfoot
3
Pablo Romero, Department of Informatics
Greenfoot
Lecture 4. Greenfoot
4
Pablo Romero, Department of Informatics
Looking at the classes
Lecture 4. Greenfoot
5
Pablo Romero, Department of Informatics
The Wombat class
import necessary packages
class header;
Wombat is a subclass
from Actor
data (constants and
variables)
class constructor and
methods
Lecture 4. Greenfoot
6
Pablo Romero, Department of Informatics
Constructor and leaf methods
constructor  initialising
direction and leavesEaten
is there any Leaf object
in my position?
this method is inherited from
the superclass Actor
remove that Leaf object
update the variable
leavesEaten
Lecture 4. Greenfoot
7
Pablo Romero, Department of Informatics
if you find leaves, eat !
The act method
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!
Lecture 4. Greenfoot
8
Pablo Romero, Department of Informatics
The turnLeft method
change direction 90 degrees
to the left
Lecture 4. Greenfoot
9
Pablo Romero, Department of Informatics
The setDirection method
this method is inherited from
the superclass Actor
Lecture 4. Greenfoot
10
Pablo Romero, Department of Informatics
The move method
if object reaches one of
the borders, do nothing!
the object moves
forward;
depends on
its direction
Lecture 4. Greenfoot
setLocation() is
inherited from Actor
11
Pablo Romero, Department of Informatics
The canMove method
Lecture 4. Greenfoot
12
Pablo Romero, Department of Informatics
The canMove method
these methods are
inherited from Actor
new coordinate
if the object moves
forward;
depends on
its direction
checks if the object
reaches the edges of the world
Lecture 4. Greenfoot
13
Pablo Romero, Department of Informatics
The WombatWorld class



Lecture 4. Greenfoot
14
Constructor
populate
randomLeaves
Pablo Romero, Department of Informatics
The WombatWorld constructor
calls the World constructor
sets the background.
the method is inherited
from the superclass World
cell.jpg
Lecture 4. Greenfoot
15
Pablo Romero, Department of Informatics
The populate method
Lecture 4. Greenfoot
16
Pablo Romero, Department of Informatics
The randomLeaves method
Create a Leaf object at a random
position (x,y); repeat howMany
times
Lecture 4. Greenfoot
17
Pablo Romero, Department of Informatics
So far

Greenfoot environment to program
simulations and games in java


World and Actor are system classes
The wombats program
Leaf
 Wombat (getLeavesEaten, foundLeaf,
eatLeaf, setDirection, turnLeft, canMove,
move, act)
 WombatWorld (populate, randomLeaves)

Lecture 4. Greenfoot
18
Pablo Romero, Department of Informatics
Exercise

How would you change the behaviour
of Wombat objects so that instead of
always turning left, they could turn to
other directions in a random way?

GreenFoot. getRandomNumber(int limit)
Generates a random number
between 0 and limit
Lecture 4. Greenfoot
19
Pablo Romero, Department of Informatics
Exercise

Create a new method, turnRandom(),
that will turn to the left a random
number of times. This method will
replace the method turnLeft() in the
act() method of Wombats. Use the
method of the greenfoot library
GreenFoot. getRandomNumber(int
limit) that produces a random integer
between zero and limit-1.
Lecture 4. Greenfoot
20
Pablo Romero, Department of Informatics
The method turnRandom
Generate a random
number between 0 and 3
call turnLeft()
turns times
Lecture 4. Greenfoot
21
Pablo Romero, Department of Informatics
The updated method act
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 a random direction!
Lecture 4. Greenfoot
22
Pablo Romero, Department of Informatics
Before and after
Lecture 4. Greenfoot
23
Pablo Romero, Department of Informatics
to set the image of an object
programmatically
Lecture 4. Greenfoot
24
Pablo Romero, Department of Informatics
Before and after
Lecture 4. Greenfoot
25
Pablo Romero, Department of Informatics
Adding Rocks
class image for
the Rock class
New classes can
be added easily
 Can import images
for classes
 However wombats
can go through
rocks

images available in the
wombats
scenario folder
Lecture 4. Greenfoot
26
Pablo Romero, Department of Informatics
check all Rock objects
in front of you;
if there is no Rock object,
return true (i.e. okay it‘s
clear!) , otherwise return
false (i.e. you cannot move!)
Lecture 4. Greenfoot
27
Pablo Romero, Department of Informatics
Summary
Greenfoot environment to program
simulations and games (greenfoot.org)
 Provides an action loop
 World subclasses set up the world
 Actor subclasses define the characters of
the game / simulation
 Sophisticated behaviour can be
implemented in Java

Lecture 4. Greenfoot
28
Pablo Romero, Department of Informatics