Pygame Shmup Part 14: Game Over (and wrapping up)
Sat, Nov 12, 2016This is part 14 of our “Shmup” project. If you haven’t already read through the previous parts, please start with Part 1. In this lesson we’ll finish up the game by adding a “Game Over” screen and the ability to play again.
About this series
In this series of lessons we’ll build a complete game using Python and Pygame. It’s intended for beginning programmers who already understand the basics of Python and are looking to deepen their Python understanding and learn the fundamentals of programming games.
You can watch a video version of this lesson here:
Game Over
Right now, when our player runs out of lives, the program just ends abruptly. This isn’t very friendly, so we’re going to make a “Game Over” screen, and allow the player to play again if they wish.
The reason the game ends is that our game loop is controlled by the running
flag (a flag is a variable that can be True
or False
) and we set running
to False
when the player dies. Instead, we want to track what state our game is in (showing game over, or playing), we’ll create a flag called game_over
, and add this to the top of our game loop:
# Game loop
game_over = True
running = True
while running:
if game_over:
show_go_screen()
We’ll define show_go_screen()
in a moment, but we also need to think about something else. When the game ends, and we go to the game over screen, if the player chooses to play again, we need to reset everything - the score, the meteors, the player’s lives, etc. Right now we set those things right before the game loop starts, but now we’re going to move them to just after show_go_screen()
so they will happen whenever the player exits that screen:
# Game loop
game_over = True
running = True
while running:
if game_over:
show_go_screen()
game_over = False
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
powerups = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
newmob()
score = 0
We also set game_over
to False
since we’re starting a new game. Then, we can change what happens when the player runs out of lives:
# if the player died and the explosion has finished playing
if player.lives == 0 and not death_explosion.alive():
game_over = True
The Game Over screen
Now we just need to define what show_go_screen
does. Since for this example, we’re only doing one “screen”, we’re just going to use the game’s title, and put some nice instructions for how to play:
def show_go_screen():
screen.blit(background, background_rect)
draw_text(screen, "SHMUP!", 64, WIDTH / 2, HEIGHT / 4)
draw_text(screen, "Arrow keys move, Space to fire", 22,
WIDTH / 2, HEIGHT / 2)
draw_text(screen, "Press a key to begin", 18, WIDTH / 2, HEIGHT * 3 / 4)
pygame.display.flip()
waiting = True
while waiting:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
waiting = False
The while loop at the end is how we handle the “Press a key to begin” part. It’s kind of like a miniature game loop, but all we’re doing is checking for events. The only events we care about are pygame.QUIT
, which happens when clicking the X on the window, and the event for pressing a key. Did you notice we didn’t use ‘pygame.KEYDOWN’ there?
The reason for using KEYUP
instead is so that we don’t start the game while the player is still holding down a key. They have to tap and release to continue.
This is a very simple way of doing the game over screen. There are many other ways to handle it, but we’ll talk about those in later tutorials.
Wrapping Up
There we have it, your first full Pygame game! If you’ve stuck with it through all these steps, you’re well on your way to becoming a game programmer.
There are obviously many more things we could add to this, like:
- Increasing Difficulty
- More powerups
- Enemies (that shoot back!)
- High score system
- Boss levels
You can probably think of other ideas as well. However, for the purposes of this video, we’re going to call this game complete. In future lessons, we’ll cover other techniques which you could then come back and apply to the Shmup.
You can see what other lessons we’ve made here: KidsCanCode Lessons and Tutorials
Thanks for reading/watching these lessons! Your support, questions, and comments are greatly appreciated.