in

Platform starter kit: Adding player lives

This post continues the enhancement of the Platform Starter Kit to make a version of the game to publish on Xbox Live Community Games.

The default platformer starter kit has the level timer which counts down from 2 minutes causing a level restart if the player fails to complete the level within this period. But the games doesn’t have any mechanism for managing player lives. Typically, the player starts with a set number of lives and looses them or gains addition lives through the game play. Game over being when the player has no more lives.

Adding player Lives.

Adding life management is straight forward. We simply require a variable to store the current number of player lives, a mechanism to add more lives and remove lives as appropriate to the game play and a test to give game over when the player has no more lives.

Clearly the number of lives belongs to the Player class. So add a lives property to the Player class (after the Level property):

public int Lives
{
    get { return lives; }
    set { lives = value; }
}
int lives;

Setting up the number of lives and passing this current number of lives between levels requires a bit more effort.

I chose to do the set up of Lives in the Level constructor but the value of Lives is passed down to this method by the LoadNextLevel method.

So modify the LoadNextLevel method in the PlatformerGame class to include a currentLives parameter:

private void LoadNextLevel(int currentLives)

And change the call to the Level to include this parameter so that we pass it on:

// Load the level.
level = new Level(ScreenManager.Game.Services, levelPath, totalScore, levelIndex, currentLives);

In the LoadContent method change the LoadNextLevel call to be:

LoadNextLevel(StartingLives);

Set up a StartingLives constant at the top of the class:

private const int StartingLives = 3;

For the constructor of the Level class simply add the currentLives parameter and then within the constructor:

Player.Lives = currentLives;

after the LoadTiles call. The Player property of the level will not be initialised until after this call.

Now we need to ensure the lives are decreased when the player is killed and the game is over when the number of player lives equals 0. We also want to reward the player for collecting gems by adding lives at certain score values.

To decrease the player lives we can just add one live of code to the StartNewLife method of the Level class:

public void StartNewLife()
{
    Player.Lives = Player.Lives -1;
    Player.Reset(start);
}

To check for game over we add a test to the HandleInput method of the game class. This is the method where the tests for player IsAlive and TimeRemaining are made.

We’ll add the test to the current if else if section of code:

if (!wasContinuePressed && continuePressed)
{
    if (level.Player.Lives < 1)
    {
       // Game Over! Put finishing screen stuff here
    }
    else if (!level.Player.IsAlive)
    {
        level.StartNewLife();
    }
    else if (level.TimeRemaining == TimeSpan.Zero)
    {
        if (level.ReachedExit)
            LoadNextLevel(level.Player.Lives);
        else
            ReloadCurrentLevel(level.Player.Lives);
    }
}

The ‘finishing screen stuff’ needs to present a message and take the game to the start. The default platformer game only loops round the game without any starting menu screens, so we can just reload the current level and reset the lives for the moment:

    if (level.Player.Lives < 1)
    {
       // Game Over! Put finishing screen stuff here
       ReloadCurrentLevel(StartingLives);
    }

Now we want to be able to reward the player with additional lives as they hit big scores by collecting gems. For the moment I’ve done this very simply by having an initial value set for the score when a life reward is given. I call the variable NextLife. Once NextLife has been reached it is incremented by a given amount. Initially this is just 2000 points, but it would be cool to change this passed on the level reached by the player so change the game play as the player proceeds with the game.

I do the life reward test in the Level update method like this:

if (Score > NextLife)
{
    Player.Lives++;
    NextLife += 2000;
}

The value of NextLife needs to survive across levels to this approach is flawed but it’ll do for the moment. Although I designed all my levels I’m finding them very challenging to complete (something to do with being over 40 I guess)  - so I’m happy to have a bug or two that awards me extra lives!

Shout out

Subscriptions

Tags

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.