13. Health, Game Over, Immortaility
Let's make it possible to lose the game!
Start by adding two properties to the ship. t will be used in some animation calculations. imm is used to indicate if the ship is temporarily invulnerable.
The h=4 in the above snippet isn't a typo. Having the starting number of ship
hearts set to 3 was great for testing the missing heart display
functionality, but we've done that so we no longer need to penalize the player,
especially since we are about to enable them to get hurt!
A couple of steps back, we added a placeholder to check if the ship collided with any enemies. It is time to implement that logic. Add this to update_game.
First, we check the imm flag. If it is set, the ship needs to be invulnerable
for a full second (30 frames). Once that time has passed, clear the flag and
reset the ship.t counter.
As we loop through the enemies, check if the ship collided with the current
enemy. If so, set the imm flag to give the ship temporary invulnerability.
Reduce the number of hearts by one. Check to see if we are out of hearts. If we
are out of heats, transition to the game over state to let the player known.
In draw_game, only draw the ship if it is not invulnerable or for ever other four frames, if it is.
Hit ctrl-r to run the game. Move the ship so it is in the path of the
enemies. The number of hearts should reduce by one each time it is hit. After
each hit, the ship should flash off and on periodically over a second before
becoming vulnerable again.

Once the last heart is lost, the game over screen should be displayed.

At this point, we have something that is starting to feel like a real game. The ship can fire bullets which destroy enemies, it can be damaged by the enemies, and once all its hearts are lost, the game ends!