Professional Documents
Culture Documents
Greenfoot Activity
Let us create a small scenario in
Greenfoot using what we have
reviewed.
Greenfoot Activity
We will create a new instance of the World class and name it
Space.
Right mouse click on World and select New subclass, name the
new
class Space, In this scenario we will not use a background image
for the
world.
Greenfoot Activity
Now we have our world created let us look at the code
editor and the
Space constructor.
super(600,400,1);
Construct a board(world) 600 x 400
pixels
with a pixel size of 1x1
4
Greenfoot Activity
A world will be empty unless we add some Actors to it.
We will create
3 Actor classes and assign an image to them.
Right mouse click on Actor and select New subclass,
name the new
classes Alien, Rocket and Shot and assign an image from
the gallery,
robot, rocket and beeper which are all in the Gallery.
Greenfoot Activity
Now we have to rotate the images for both the Alien and
the Rocket.
Right mouse click on the Alien actor and select Set
Image, Select the
image from the Scenario images box, click settings and
select Edit
Greenfoot Activity
The image will open up in paint, rotate the image right 90
degrees from
the Rotate drop down.
Greenfoot Activity
Now we have created the Actors we can add instances of
them to the World.
There are 3 ways to add instances of our Actor classes to
the world.
1. Manually while the Scenario is running.
2. Automatically When constructing the World.
3. Automatically while the Scenario is running.
Greenfoot Activity
1. Manually while the Scenario is running.
Compile and Run your scenario. Right mouse click on
One of the classes and select new then click where you want to place
them on the world.
Add one instance of Rocket and two instances of Alien to your
world.
Greenfoot Activity
1. Manually while the Scenario is running.
10
Greenfoot Activity
2. Automatically When constructing the World.
Open the code editor of the Space class and add the following
code :
addObject(new Rocket(),300,340);
Compile and run your scenario, you should now automatically have two
an instance of Rocket positioned bottom centre of the world. We will
cover Automatically while the Scenario is running later .
11
Greenfoot Activity
Now we have a Rocket let us make it move. We will use the left and
right arrow keys to move the Rocket along the bottom of the world. To
do this we will use the Greenfoot.isKeyDown() method. In the Act
method of the Rocket enter the following code :
if (Greenfoot.isKeyDown("left")) {
setLocation(getX()-5,getY());
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX()+5,getY());
}
Compile and run your scenario, the Rocket should now move left and
right using the arrow keys.
12
Greenfoot Activity
What use is having a space Rocket if it cant shoot anything that gets in
its way? Let us add a way for the rocket to shoot a shot Actor.
In the Act method of the Rocket enter the following code after our left
and right controls :
if (Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Shot(), getX(), getY());
}
Compile and run your scenario, the Rocket should now move left and
right using the arrow keys and a shot should appear when space is
pressed.
13
Greenfoot Activity
Notice our shot just stays in the same place? Let us give the shot some
movement.
In the Act method of the Shot Actor enter the following code :
if (getY() > 0) {
setLocation(getX(), getY() -5);
}
Compile and run your scenario, shot should appear when space is
pressed and move to the top of the world.
14
Greenfoot Activity
Notice our shot just stays at the top of the screen? Let us remove the
shot once it goes out of our world.
In the Act method of the Shot Actor edit the previous if statement to
include the following code as an else
if (getY() > 0) {
setLocation(getX(), getY() -5);
}
else {
getWorld().removeObject(this);
}
Compile and run your scenario, shot should appear when space is
pressed and move to the top of the world where it is removed.
15
Greenfoot Activity
Let us now make it a little less easier to shoot all of those shots off, we
will limit the shots to a short time delay in between.
Outwith the Act method of the Rocket Actor enter the following code
To create a variable we can use to control the timing of shots.
private int shotTimer = 0;
16
Greenfoot Activity
Now edit the code to create a shot to the following :
if (shotTimer > 0) {
shotTimer = shotTimer - 1;
}
else if (Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Shot(), getX(), getY());
shotTimer = 50;
}
Compile and run your scenario, now the shot should not appear every
time space is pressed but at a slower interval.
17
Greenfoot Activity
Now we can shoot all we need is something to shoot at, how about one
of those Alien actors we created earlier?
Open the code editor of the Space class and add the following
Code to the world constructor :
addObject(new Alien(),30,30);
Compile and run your scenario an Alien should have appeared and we
can shoot right at it but nothing happens when we hit it.
18
Greenfoot Activity
When we hit an Alien we need to remove both the alien and the aimed
shot from the world. We will edit the code in the Act method of our
Shot class to the following code :
if (getY() > 0) {
setLocation(getX(), getY() 5 );
Actor hit = getOneIntersectingObject(Alien.class);
if (hit != null) {
getWorld().removeObject(hit);
getWorld().removeObject(this);
}
}
else {
getWorld().removeObject(this);
}
19
Greenfoot Activity
One Alien is easy to hit, let us add some more.
Open the code editor of the Space class and edit the code to place the
Alien instance to the following :
for(int i =1; i<10; i++) {
addObject(new Alien(), 60*i,30);
}
Compile and run your scenario we should now have a few more Aliens
to shoot at.
20
Greenfoot Activity
Stationary Aliens are too easy to hit, let us give them some movement.
Open the code editor of the Alien class and edit the Act method to the
following :
setLocation(getX(), getY()+1);
Compile and run your scenario those Aliens should be moving towards
earth now.
21
Greenfoot Activity
Now the Aliens are too fast to hit before they reach earth, let us give
them a little less movement.
Open the code editor of the Alien class and edit the Act method to the
following :
if (Greenfoot.getRandomNumber(4) == 2) {
setLocation(getX(), getY()+2);
}
Compile and run your scenario those Aliens shoot be moving towards
earth a little faster now.
22
Greenfoot Activity
Finally, let us add a Label class to keep track of our score.
Create a new sub-class of Actor, give it the name Label, and
leave the image empty.
Open the code editor for the new class, and add a class-level
attribute to keep track of the score:
private int score;
Create a constructor to initialise the score to 0 and use
setImage() method to display the score variable on screen
public Label()
{
score=0;
setImage(new GreenfootImage("Score: "+ score,
30,Color.black, Color.white));
}
Greenfoot Activity
Greenfoot Activity
Now, we need to add an instance of the Label class to our
world. Open the code editor of the Space class, declare a
new object of class label (after the Space Class declaration
the before the constructor.
private Label scoreLabel;
Add code to the constructor of the world, to create a Label
object, and place it in the World
Label scoreLabel = new Label();
addObject(scoreLabel,60,380);
Compile and test your code. You should now see your score
at the bottom left of the screen (although we still need to
add code to make it update!)
Greenfoot Activity
To understand the relationship between our objects, the
diagram below illustrates how they currently interact.
Space
Rocket
Alien
Label
Shot
The Space class creates instances of the Rocket, Alien and Label
classes, and the Rocket creates instances of the Shot class
when the space bar is pressed.
Greenfoot Activity
Greenfoot Activity
Space
Label
Rocket
Alien
Label
Shot
Label
updateScore()
Greenfoot Activity
Greenfoot Activity
Greenfoot Activity
Next, we need to add one line of code in the Shots act() method
if (getY() > 0) {
setLocation(getX(), getY()-5);
Actor hit = getOneIntersectingObject(Alien.class);
if (hit != null) {
tempLabel.updateScore(10);
getWorld().removeObject(hit);
getWorld().removeObject(this);
}
}
else {
getWorld().removeObject(this);
}
This calls the Labels updateScore() method adding the value 10
to the score
Greenfoot Activity
Greenfoot Activity
33