11 Player collisions and lives

This chapter covers

  • Handling the player’s ship colliding with enemies
  • Animating the player’s death sequence
  • Displaying the current player’s lives
  • Handling the game-over message

In this chapter, we are going to cover handling the collision between the player and enemies, animating the player’s death sequence, and displaying the number of lives the player has remaining. Of course, once the player has run out of lives, we need to show Game Over on the screen, returning to our game intro screen so the player can view the high score and start a new game.

11.1 Player collisions

First, we need to detect if any of the enemy objects have hit the player’s ship. The best place to do this is in our existing enemy object loop, located in the move_enemies function in our Megablast.s source file.

Before we add the collision detection, we will need a lives counter so we know how many lives the player has left, as well as a way to know when the player has died and keep track of the animation of the player’s death sequence. Find our zero-page memory section, and add two new variables, called lives and player_dead.

11.2 Animating the player’s death sequence

11.3 Handling game over

11.4 Displaying the player’s lives

Summary