Wednesday, October 23, 2013

Game Over Screen

Today we're going to be adding a "Game Over" screen. To begin with you will probably want to create one, find one online, or use this graphic:
Click to View Full Size
If we add this method to our MyWorld class:
Note: I called my world "MyWorld" so you will just add this to whatever "world" class you made.
public void endGame()
    {
        removeObjects(getObjects(null));
        setBackground(new GreenfootImage("gameover.jpg"));  
        Greenfoot.playSound("ohno.wav");
        Greenfoot.stop();
    }
Then add this piece to your "Try to Eat Player" (or Turtle or whatever) code:
((MyWorld)getWorld()).endGame();
Note #2: Again, I'm referencing "MyWorld" -- you need to adjust for your own name. One thing I added to my new "endGame" method is stopping the background music..
bgSound.stop();
Again, my sound loop was called bgSound so adjust as necessary.
Once you get your code working you can spend some time making your Game Over image or fine-tuning your game a little bit more. Remember to upload your finished [updated] game to GreenFoot.

No comments:

Post a Comment