Modifying Platformer Starter Kit–Adding Health

So there was another post on the AppHub forums where someone was asking about how to add health to the character in the Platformer Starter Kit rather than having him die immediately when touching an enemy. I’d cobbled together a bunch of mods that had previously been done and thought this might make another good one.

Implementing this turned out to be pretty simple. Just two files need updating, Level.cs and Player.cs and I added two sprites – a health bar and a 1×1 pixel texture to fill in the healthbar.

healthbar

health

(magnified may times Open-mouthed smile)

 

First, the changes to the Player.cs file. At the top where all the members are declared add:

private int health = 100;
//private int lives = 3;
private int lives = 1;
private bool touchingEnemy;

public bool TouchingEnemy
{
    get { return touchingEnemy; }
    set { touchingEnemy = value; }
}

public int Health
{
    get { return health; }
}

 

Notice, the old “lives” member has been commented out and replaced. The player now only has one life, but can be injured a number of times before dying (which we’ll see in a second).

Previously, when the player was touched by an enemy the OnKilled method was called. We need to add a new method (I put it right under the OnKilled method):

public void OnInjured()
{
    touchingEnemy = true;

    health -= 10;

    if (health == 0)
    {
        killedSound.Play();
        sprite.PlayAnimation(dieAnimation);
        isAlive = false;
    }
}

 

Now we’ll add the textures necessary to render the health bar and the members of the level class to load the textures into.

You can either save the textures above by right-clicking on them, create your own, or grab them out of the zip file attached to this post. Drop them into the “sprites” folder in the content project (or wherever you want, but you’ll have to change the relevant code that loads them).

Add the following code to the Level.cs file’s section where the other members are declared:

private Texture2D healthBar;
private Vector2 healthBarLoc;
private Texture2D healthTexture;

 

Then add the following to the Level.cs constructor:

//load healthbar textures
healthBar = Content.Load<Texture2D>("sprites/healthbar");
healthTexture = Content.Load<Texture2D>("sprites/health");
healthBarLoc = new Vector2(Width * Tile.Width - 205, 5);

 

The Width member of the Level class returns the width of the screen measure in number of tiles, so we have to do a little multiplication and offset from the edges of the screen a bit.

Now add the matching method to injure the player that we had in the Player class (again, I added this right below the OnKilled method):

private void OnPlayerInjured()
{
    Player.OnInjured();
}

 

And we’ll call it in the UpdateEnemies method:

private void UpdateEnemies(GameTime gameTime)
{
    foreach (Enemy enemy in enemies)
    {
        enemy.Update(gameTime);

        // Touching an enemy now just injures the player
        if (enemy.BoundingRectangle.Intersects(Player.BoundingRectangle))
        {
            if (!player.TouchingEnemy)
                //OnPlayerKilled(enemy);
                OnPlayerInjured();
        }
        else
            UpdatePlayerCollision(false);
    }
}

private void UpdatePlayerCollision(bool touchingEnemy)
{
    player.TouchingEnemy = touchingEnemy;
}

 

That should be it. Save everything and run the game. You should see the health bar change every time you touch an enemy. You’ll have to advance to the second level to see one. After ten touches the character should die and you’ll be presented with a dialog. I haven’t changed the dialog or relevant code to restart the game, nor did I remove the sprite rendering the number of lives the player has. I wanted to keep the latter in so that the option to add a life power-up can be done without breaking the game. The former is something for you to implement. Smile

Hopefully someone will find this useful.

 

Source code can be downloaded from here.

2 Comments

  1. VS says:

    Thanks, I found this very helpful!

  2. tCUP says:

    Nice :) But isn’t it better to change public void OnInjured()
    {
    touchingEnemy = true;

    health -= 10;

    if (health == 0)
    {
    killedSound.Play();
    sprite.PlayAnimation(dieAnimation);
    isAlive = false;
    }
    }

    to
    public void OnInjured()
    {
    touchingEnemy = true;

    health -= 10;

    if (health == 0)
    {
    killedSound.Play();
    sprite.PlayAnimation(dieAnimation);
    isAlive = false;
    health = 100;
    }
    }

    just to re-set the health when he dies?

Leave a Reply