Code Clips - Insert Game Over Text:

Code Clip 1:

Paste near the beginning of the GameOver screen, below the line import greenfoot.*;

//Code Clip 1 **********************************************************************************
import java.awt.Color; //gives Greenfoot access to common colors in caps, like WHITE, GREEN, etc.
//**********************************************************************************************

Code Clip 2:

Paste inside the act() method of the GameOver class

// Code Clip 2***********************************************************
setImage(new GreenfootImage("Game Over", 48, Color.WHITE, Color.BLACK));
// Uses setImage to draw text. The first argument is the text to draw,
// the second argument is the size (48 pixels), the third is the foreground
// color (WHITE) and the fourth is the background color (BLACK).
// **********************************************************************


Code Clip 3:

Paste inside the hitBanana() method of the PieShip class

// Code Clip 3 ***********************************************************************
World myWorld = getWorld(); // Assigns world to myWorld variable.
GameOver gameover = new GameOver(); // Creates a new GameOver Actor.
int textX = myWorld.getWidth()/2; // The x and y coordinates of GameOver actor
int textY = myWorld.getHeight()/2; // are half the width and height of screen.
myWorld.addObject(gameover, textX, textY); // Places the GameOver actor on screen.
// ************************************************************************************


Demo Lesson - Game Over Text

First, download the demo lesson here:


Game Over Demo

Instructions:

First, open the GameOver actor code window. (If you create your own GameOver actor in another game do not give it an image.)

At the beginning of the GameOver class below the line import greenfoot.*; remove the comment and paste code clip 1.

Inside the act() method paste Code Clip 2.

Close the GameOver code window and open the PieShip code window. Inside the hitBanana() method of the PieShip class paste Code Clip 3.

Close the PieShip code window, compile and run the game. Try hitting banana to end the game, and watch the Game Over screen come up!

How it works:

Code Clips 1 and 2 are inside an actor called GameOver with no image associated with it. If you want to create text in a game you are making, first make an actor with no image.

Code clip 1 contains the line:

import java.awt.Color;

This imports a module that has all of the common colors listed as bold-faced text, such as Color.WHITE, Color.GREEN, Color.BLACK, and so on, which we will call when we dynamically create text in Code Clip 2.

Code clip 2 contains the lines:

setImage(new GreenfootImage("Game Over", 48, Color.WHITE, Color.BLACK));
This dynamically creates a text image. The "Game Over" argument is the text you want to show. The argument 48 is the size of the text, change to make the text bigger or smaller. Color.WHITE is the color of the text, and Color.BLACK is the background color.

Code clip 3 contains the lines:

World myWorld = getWorld();
GameOver gameover = new GameOver();
int textX = myWorld.getWidth()/2;
int textY = myWorld.getHeight()/2;
myWorld.addObject(gameover, textX, textY);

This text is inside the hitBanana() method, which only runs when the ship has hit a banana and the game is over. To see how this works look at the Remove Object On Collision Code Clip.

The first line assigns the world to the variable myWorld, so we can use its methods. The second line creates a new GameOver actor and assigns it to the variable named gameover.

The line int textX = myWorld.getWidth()/2; gets the width of the screen, and divides it by 2 to find an x coordinate in the middle of the screen. The screen has a width of 600, so we could have made the x coordinate equal to 300. But this way it will still be in the middle of the screen even if we change the size of the screen. The next line does the same thing for the y coordinate.

The line that begins myWorld.addObject... adds the GameOver actor we assigned to the gameover variable in the second line of this clip, with the coordinates textX and textY we created in the previous two lines.