//new to java /
The End of the Game
Let’s finish our game and share it with the world.
The game we continued build- ing in the previous issue of
Java Magazine is getting nearer
to completion, but some parts
are still missing. Today, I’ll show
you how to finish the game
and—most importantly—how to
share it with your friends. If you
haven’t been playing along, you
can download Greenfoot and our
project and join in the fun.
Ending the Game
Currently, there is no way to win
this game. Eat all the pizza slices,
and nothing happens. No fanfare,
no big “YOU WIN!” sign. Equally,
when the snakes catch you, the
game just quietly runs on.
We want to fix both those
situations. Let’s say that when
you manage to eat 12 slices of
pizza, you win. The game stops,
and we hear a victorious fanfare.
However, when the snakes catch
you, you lose. We get a rather sad
losing sound, and the game stops.
The second scenario (losing)
is easier to do—we have almost
everything for that already. So
let’s do that first, and then look at
the functionality for winning.
Losing (Being Eaten by Snakes)
We already have a sound effect
for when the snake eats the turtle
(in the Snake’s eat method),
which uses Greenfoot’s playSound
method. The first thing we do is
just change the sound to a new
sound file (which is included with
the project download) named
Then we add a new line: a call to
Greenfoot’s stop method to stop
the game at this point. Together,
the code looks like this:
dot), press Ctrl-Space
on your keyboard, and
you will see a list of all
methods that you can
call at this point, as
shown in Figure 1.
Starting to type a
method name will
narrow the choices.
Then choose any of the
methods from the list.
JAVA IN ACTION
PHOTOGRAPH BY JOHN BLYTHE
Note that both methods are
static methods of the Greenfoot
class, so we have to write the class
name Greenfoot and a dot (full
stop) before the method name in
the method call.
To discover what other methods
the Greenfoot class offers, choose
Greenfoot Class Documentation
from the Help menu.
Alternatively, you can use code
completion. For example, after
typing Greenfoot. (that is, after the
Winning (Finishing Your Pizza)
We want our winning functionality (fanfare and stopping the
game) to occur when we have
eaten our whole pizza ( 12 slices).
In choosing this criterion, we
have two problems. First, we currently have only 7 slices of pizza
in our game. We could, of course,
simply put 12 slices in from the
start, but that would make the
game too easy. Instead, I want
to make a new slice appear each
time one is eaten.
And secondly, we now need to
count how much we have eaten,
so we know when to stop. We
don’t know how to do that yet.
The Magically Appearing Pizza
The pizza slices get eaten in the
Turtle class—to be specific, in
the turtle’s eat method. So this is
where we start to work. To keep
the code clean, we will place the
instructions to create a new pizza
slice in a separate method and
call that method from here. So we
place a call to placeNewPizza() into
our eat method, just below the
playing of the sound:
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012