Skip to content

Tutorial part 6 : Life!

ilovethisid edited this page Jun 20, 2021 · 6 revisions

This time, we'll create life for our player. For now, our player is invincible, and can destroy enemies with crashing into them.
This does sound good, it's not fun. We'll create a life system and display the remaining health of player.
First, add variable player_life to Game.

int player_life;

And initialize to 5 in Game::Game(). Since we initialized, we are going to draw.
Make function drawLife() in Game.

void Game::drawLife()
{
    for (int i = 0; i < player_life; i++) {
        getConsole().drawTmp(2 * i + 3, 3, L'', FG_RED);
        getConsole().drawTmp(2 * i + 4, 3, L' ', FG_RED);
    }
}

Write drawLife() in updateLoop function. We have to constantly update life to apply changes.
If you run, you will see five hearts aligned neatly to represent life.
Now we are going to make it diminish.

When does the player lose life? It is always important to think scenarios in making a game.
Well, this was an easy question. Player lose life when attacked by enemy. But there can be many types of attacks from enemy.
Crashing into enemy will lose life. Getting shoted by enemy will lose life. In this simple game, there is two, but in complex games
there can be many situation that player loses life. We'll focus on those two. In top of collisionEvent(), put below :

    vector<Object*> player_colliding_objects = player->getCollidingObjects(enemys);

    if (player_colliding_objects.size() >= 1) {
        for (int i = 0; i < player_colliding_objects.size(); i++) {
            if (player_life > 0) player_life--;
            if (player_life <= 0) {
                // die
            }
        }
    }

Object->getCollidingObjects() gets the colliding objects with current object. This function is useful when checking colliding
objects that collide with important object, such as player. Code on top reduces life by number of colliding enemies. If life is below zero, player should die, but we didn't implemented that yet. Also, we didn't implement collision for enemy bullet yet,
so let's add collision to enemy bullet too.

    // in collisionEvent()
    for (int j = 0; j < enemy_bullets.size(); ) {
        if (enemy_bullets[j]->collision_flg) {
            delete enemy_bullets[j];
            enemy_bullets.erase(enemy_bullets.begin() + j);
        }
        else j++;
    }

    for (int i = 0; i < player_colliding_objects.size(); i++) {
        player_colliding_objects.pop_back();
    }
    // free memory

    player_colliding_objects = player->getCollidingObjects(enemy_bullets);

    if (player_colliding_objects.size() >= 1) {
        for (int i = 0; i < player_colliding_objects.size(); i++) {
            if (player_life > 0) player_life--;
            if (player_life <= 0) {
                // die
            }
        }
    }
    // collision with enemy bullet

    for (int i = 0; i < player_colliding_objects.size(); i++) {
        player_colliding_objects.pop_back();
    }
    // free memory

Now if you run the game, you will see player life decreasing when it collides to enemy or enemy bullet.
Next time, we will implement score and gameover.