The game structure
Now that we have nearly all of the pieces of our game in place, we need to start bringing them together into a structure that supports a normal game flow, from title screen, to playing, to game over, and back to the title screen, as shown in the following image:
The game will begin in the TitleScreen state. When the user begins the game, the state switches to Playing, until the user collides with an enemy ship, an enemy shot, or an asteroid. At that point, the state switches to PlayerDead.
The game remains in the PlayerDead state for a few seconds to let the player agonize over their defeat, and to allow any enemy ships on the screen to move off screen. At that point, the new game state depends on the number of remaining ships that the player has.
If the ship they just lost was not their last ship, the state returns to Playing and the game continues. If it was their last ship, however, the state switches to GameOver. The game then returns to the TitleScreen state.