Greenfoot Avoider Game Tutorial - Part II

by Michael L. Haungs

Part II: Making it a Game

Picking up where we left off in Part I, we will add a game over screen, an introduction screen, and some background music. Remember, as part of your assignment last time, you implemented collision detection and animated the enemies. This tutorial depends on that being completed.

Adding a Game Over Screen

First, you need to draw an entire game over screen in your favorite graphic design/drawing program such as gimp, CorelDRAW, Inkscape, or even Windows Paint. I used Adobe Illustrator to create this screen (feel free to use it):

Game Over Screen

Whatever you use to draw your image, make sure you can save it in either PNG or JPG format. And, its size should be 600x400 (the same size as your world). Save this image in the "images" folder in your AvoiderGame scenario.

A Whole New World...

Using the same steps you used to create AvoiderWorld (Part I), create another world, call it "AvoiderGameOverScreen" and associate the image you created above with it. In the "World Classes" area of your scenario, you should see this:

View of World classes

Collision Detection

Now, we want to display the game over screen if our hero touches an enemy. To do this we need to do the following three steps:

  1. Detect when we collide with an enemy and then tell (by calling a function) our world, AvoiderWorld, that the game is over.
  2. In our AvoiderWorld class, we need to implement the "game over" function that the Avatar will use to signal the end of days.
  3. In our "game over" function, set the world to be AvoiderGameOverScreen instead of AvoiderWorld.

Clear as mud and twice as tasty? Good! Let's start with step 1 above. In the "Assignment" section of Part I of the Avoider Game tutorial, you wrote code to remove the hero from the game if it touches one of the enemies. Some part of the code should look like this:

World world = getWorld();
world.removeObject(this);

To implement step 1 we need to change that code to this:

AvoiderWorld world = (AvoiderWorld) getWorld();
world.endGame();

I hope it makes sense that we are now asking the world to "end the game" as opposed to removing the hero object. The part that may be confusing is the substitution of AvoiderWorld for World and the addition of "(AvoiderWorld)". The problem is that we are going to implement endGame() in AvoiderWorld, not World. So, we need some way of specifying that the return value of getWorld() will be treated as an AvoiderWorld, not just a plain ol' ordinary World. In Java terms, this is called "casting". You can learn more about casting here. (You probably should read that link...may help clear some things up...)

Now for step 2 and 3. What the heck, I'll just give it to ya. Here's the code you need to add to AvoiderWorld:

End game function

Hmmm. There's a part missing....curious! Oh well, why don't you lookup the documentation for the Greenfoot class and see if you find a method that will work. (yup, fill in the blank!)

We have changed and added a minimal amount of code, but if you have followed along carefully, you should be able to save, compile, and run the code. See the "Game Over" screen when your hero touches an enemy? (If not, go back and retrace your steps. Something you typed in is wrong.)

Play Again?

The "Game Over" screen is great and all, but I don't want to just stare at it all day. Ok, so let's make it so that you can restart the game by clicking on the "Game Over" screen. AvoiderGameOverScreen needs to keep checking if the mouse has been clicked and then set the world back to AvoiderWorld so we can play the game again. Looking in the Greenfoot documentation, I discovered the function mouseClicked(). Let's put it in the act() method along with the change world code:

Play again code

Compile and run. Everything working?

Adding an Introduction Screen

Adding an "Introduction Screen" is really easy and you just...uh...you just need....uh...actually, I'm not going to show you. I'll add it to the "Your Assignment" section as you definitely have learned enough in the "Adding a Game Over Screen" section to do this without my help. Oh, you can either create your own introduction screen or use this one:

Avoider Introduction Screen

Background Music

If you tried out the completed game (link in the Tutorial Introduction), you will remember the super-awesome funky music I played during the game. I got the music LEGALLY at "newgrounds.com" and gave credit to the author in my code. In this part of the tutorial, you need to search the web for a song (mp3) you would like to play during the game. As the last bit of Part II, I'll show you how to add that music.

Playing the music

We only want the music to play when we start playing the game, not during the introduction or game over screens. Therefore, we'll start the music when we display AvoiderWorld and turn it off before we display AvoiderGameOverScreen. We only want to start the music once, so we don't want to add the code to play the music in the act() method -- imagine the noise from doing that! What we need is a function that is only called once at the creation of the object. That's what the constructor method of a class provides. (If you need to review what a "class" and an "object" is, see the aside in Part I: "What have we just done?".)

 

Ok, now that we know where to put the code (everyone say "constructor function") we need to know what code to write. Greenfoot provides a class for playing and managing music called "GreenfootSound". This class makes playing music really easy. Before I show you the code to put in the constructor method, you should take a look at the documentation for GreenfootSound and see if you can figure out what to write.

 

(No, really! Go read the documentation!)

 

 

(I'm making space so you don't see the answer before going to read the documentation.)

 

 

(Trying to do it on your own will really help you...)

 

Ok, here's the code you need to add to the constructor function of AvoiderWorld:

Code for starting music

You need to change the code above so that instead of "sounds/UFO_T-Balt.mp3" you use a string that gives the name of the music you downloaded to play (you should save the music in your "sounds" folder in your Greenfoot project's folder).

Ok, save, compile, and run. Did it work?

Make it stop!

If you ran your code above, you had music during the game, but it did not turn off when you died and went to the game over screen. We have to explicitly turn off the music before displaying AvoiderGameOverScreen. This is super easy! All we need to do is add the following line of code to the endGame() method you added to AvoiderWorld earlier:

bkgMusic.stop();

Now, if you compile this you will get an error. (Do it and see what error you get.) The problem is that we only defined the variable bkgMusic in our constructor function and it is not available in our endGame() method. To fix this we need to create the "bkgMusic" variable outside of every method. Change your AvoiderWorld class to look like this:

Changes to avoider world class

Now, save, compile, and run. It should all work according to plan...

Your Assignment

Implement the following before continuing on to the last part of the tutorial:

  1. As I mentioned above, you are going to implement the introduction screen on your own. Remember, it will be very similar to how you implemented the game over screen.
  2. Once the game over screen is displayed, play music. Are you going to make it peppy music to lift the spirits of your player or sad and morose to really rub it in?
  3. Our enemy movements are pretty vanilla. Can you spice it up? Some ideas are to have them have variable speed, drift left or right, or enter from the top OR bottom. What will you come up with?

Next...

Almost done! We have built the basics of our game and will next add some things to make it challenging.

This completes Part II of this tutorial. In Part III, you will enhance the playability of our game.