O'Reilly logo

Learning XNA 3.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game-Over Logic and the Game-Over Screen

So, now you have to determine how your game will end. You already have an objective for the game: avoid the three- and four-blade sprites. But when is the game actually over? It seems a bit rough to end the game as soon as the user hits a single blade sprite. Instead, it might make the game a bit more enjoyable if the player has a certain number of lives to play with.

To accomplish this, first you'll need to create a class-level variable in your Game1 class to keep track of the number of lives remaining, as well as a public property with get and set accessors to allow the SpriteManager to access and modify the value:

int numberLivesRemaining = 3;
public int NumberLivesRemaining
{
    get { return numberLivesRemaining; }
    set
    {
        numberLivesRemaining = value;
        if (numberLivesRemaining == 0)
        {
            currentGameState = GameState.GameOver;
            spriteManager.Enabled = false;
            spriteManager.Visible = false;
        }
    }
}

Notice that when the property is set, its value is assigned to the numberLivesRemaining variable, and then that variable is checked to see if its value is zero. If the value is zero, the game state is changed to GameState.GameOver and the SpriteManager is disabled and hidden. This allows you to decrement this value from the SpriteManager class and then, when the player is out of lives, have the game automatically shut down and enter a state in which you can display a game-over screen.

Now, not only do you want to keep track of the number of lives that a player ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required