You are on page 1of 33

Space Wars Game

Copyright 2012, Oracle. All rights reserved.

Oracle Academy Java Fundamentals Institute: Day 1

Greenfoot Activity
Let us create a small scenario in
Greenfoot using what we have
reviewed.

Oracle Academy Java Fundamentals Institute: Day 1

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.

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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.

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

Greenfoot Activity
The image will open up in paint, rotate the image right 90
degrees from
the Rotate drop down.

Now do the same for Rocket only rotate it to an upright


position left 90
degrees.
7

Oracle Academy Java Fundamentals Institute: Day 1

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.

Oracle Academy Java Fundamentals Institute: Day 1

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.

Oracle Academy Java Fundamentals Institute: Day 1

Greenfoot Activity
1. Manually while the Scenario is running.

Positives : Encourages users to add own instances where


they want them.

Negatives : After each compilation and placed instances will be


removed and world refreshed to blank.
Users will have the ability to add instances at any time no matter
which method of adding instances is used.

10

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Oracle Academy Java Fundamentals Institute: Day 1

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

Next, we need to add a method, to update the score.


Add the following code below the existing act() method of
the Label class:
public void updateScore(int amount)
{
score+=amount;
setImage(new GreenfootImage("Score:
"+score,30,Color.black,
Color.white));
}
Compile and correct any errors

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

To update the score, we would need to do this in the Shot


class, as this is where checks for collisions with the Alien
Class take place.
The problem we have is that the Shot class and the Label
class have no way of communicating directly (as they are
not visible to each other).
To solve this issue, we can pass a reference to the Label
when we create the Rocket, and then pass this to the
instances of the Shot class when they are created.
This will allow the Shot class to call the UpdateScore ()
method of the Label when a collision with an Alien takes
place.
This is much simpler than it sounds, and only requires a few
alterations to our existing code!

Greenfoot Activity
Space

Label

Rocket

Alien

Label
Shot

Label

updateScore()

The Shot class will then be able to call the updateScore()


method of the Label

Greenfoot Activity

First we need to make some changes to the constructor of the Rocket


Class
Open the code editor for the Rocket class
Declare a class-level private attribute of type Label, and name it
tempLabel (below the existing variable for private int shotTimer=0;)
public Label tempLabel;
Create a new constructor to take the label as a parameter and assign
it to the tempLabel we have created
public Rocket(Label inLabel)
{
tempLabel= inLabel;
}
In the act() method of Rocket, where the code creates a new shot
(when the space bar is pressed), amend the code to pass tempLabel
to the Shot:
getWorld().addObject(new Shot(tempLabel),getX(),getY());

Greenfoot Activity

We need to make similar changes to the Shot class. Create a


class level variable for tempLabel, and create a constructor
to accept a label as a parameter, and assign to our new
tempLabel.
Open the code editor for the shot class and amend as shown:
public class Shot extends Actor
{
private Label tempLabel;
public Shot(Label inLabel)
{
tempLabel=inLabel;
}

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

Now we need to make one change to the world class. Open


the code editor for the Space Class.
Amend the line of code in the constructor that adds a new
Rocket to the world, to pass the scoreLabel as a parameter
super(600, 400, 1);
Label scoreLabel = new Label();
addObject(scoreLabel,60,380);
addObject(new Rocket(scoreLabel),300,340);

Compile, run and test, the score should now increase by 10


points every time an alien is hit.

Oracle Academy Java Fundamentals Institute: Day 1

Greenfoot Activity

We now have a working scenario which allows keyboard control and


interaction between Actors in our world.
Possible Additions :
End game scenarios :
Alien crashes into Rocket
Alien reaches earth
No Aliens left (add more Aliens)
Timer
Alien shoots back
Multiple rows of Aliens
Levels

33

You might also like