Tutorial part 7 : Score and End of the game - ilovethisid/Educational-Ascii-Game-engine GitHub Wiki

So far we have implemented player movement, making enemies, shooting enemies, and representing player life.
This made our game look much like other shooting games. But, we still have to implement a little more.
This time, we will implement scoring system and player death.
First, add int score to Game. This isn't hard, so I think that you could easily do it.
And don't forget to initialize to 0 in Game constructor.
We'll add one function to check. Add showScore written in below.

// in Game.cpp

void Game::showScore()
{
    char score_text[20];
    snprintf(score_text, 20, "score: %d", score);
    getConsole().print(score_text, 3, 23);
}

// and add showscore() to Game::initialize()

This shows score, and we will continue by adding addScore().

void Game::addScore(int increment)
{
    score = score + increment;
    showScore();
}

Now you can add score by increment, and you should put this function when it is needed.
When does a player get score? Normally, if you kill enemies. So I will put addScore when bullet kills an enemy.

// in Game::collisionEvent()
for (int i = 0; i < bullets.size(); i++) {
    if (bullets[i]->getCollidingObjects(enemys).size() >= 1) {
        addScore(10);
    }
}

You can also make score increase by time.

// in Game::updateLoop()
if (count_frames % score_interval == 0) {
    addScore(1);
}
// you can set score_interval by fps operations

Last time, we made hp for our player, but still the player is invincible. Now we are going to make player die.
void Game::dieEvent()
{
    exit();
}

Add this function and call it when player life<=0. We are making a console game, so
exiting from this console window will make the game end. If you run the game,
you will see life, enemies, and you will die when hp = 0. So far, we have made
a simple shooting game with random enemies, player and bullets. Next time, we will introduce
some other features that you can use in making a game.

⚠️ **GitHub.com Fallback** ⚠️