Docs › game library

game.game_over()

End the game with a big banner across the screen. Add retry=True and one tap starts a fresh game, so nobody has to press Run again.

How to write it

game.game_over(message, retry=False)
message
The text on the banner. Leave it out and it says Game Over. Put \n in it to start a new line, like "Crash!\nScore: 12".
retry
Optional. True means a tap or click on the game starts it again from the top. Leave it out (or False) and the game just stops on the banner.

It gives nothing back. After it, game.playing() is False, so your while game.playing(): loop ends.

A full example

Dodge the falling rock with the arrow keys. Each rock you dodge scores a point and the next one falls faster. Get hit and it's game over, with a tap to try again:

import game
import random

game.window(480, 360, background="#0b1020")

player = game.sprite("rocket", 240, 320, size=50)
player.angle = -90                         # point it up
rock = game.sprite("asteroid", random.randint(30, 450), 0, size=44)
board = game.label("Score: 0", 70, 24, color="#ffffff")

score = 0
speed = 5

while game.playing():
    if game.pressed("left"):
        player.x = player.x - 7
    if game.pressed("right"):
        player.x = player.x + 7

    rock.y = rock.y + speed
    if rock.y > 380:                      # dodged it
        score = score + 1
        board.content = "Score: " + str(score)
        speed = speed + 0.5
        rock.x = random.randint(30, 450)
        rock.y = 0

    if player.touches(rock):
        game.sound("explosion")
        game.game_over("Crash!\nScore: " + str(score) + "\nTap to play again", retry=True)

    game.frame()

Click the game window first so it hears your arrow keys. When the banner shows, tap or click the game to play again. A key press won't do it: it has to be a tap or click.

What happens after it

  • The banner stays. It sits on top of the game, and every frame drawn after it still shows it.
  • The loop ends. game.playing() becomes False, so while game.playing(): stops at its next check. The rest of that pass still runs, which is why the game.frame() after it is fine.
  • Your program doesn't vanish. Any code after the loop still runs, so a print("Thanks for playing!") at the very bottom shows up in the output.
  • With retry=True, game_over() waits on the banner until the player taps. Then the whole program runs again from the very first line: sprites, score and speed all start fresh. It's great on a phone, where there is no Run button to reach for.
  • Once it's over, it's over for that run. You can't start a second while game.playing(): loop after it, because game.playing() stays False. For a level 2, keep the same loop going and change things inside it instead.

Pairing it with a score

The banner is the perfect place to show the final score. If your game is published, send the score to its leaderboard right before the banner:

if player.touches(rock):
    game.submit_score(score)      # onto the game's leaderboard
    game.game_over("Score: " + str(score), retry=True)

The order matters with retry=True: the line after game_over() only runs once the player taps, so a score sent after it would be late.

Why isn't my game over working?

  • Things keep moving behind the banner. Your loop is while True:. It never checks whether the game is over, so it keeps going forever. Use while game.playing():.
  • The line break shows up as text. It must be a backslash, \n, not a forward slash /n.
  • The score in the banner is wrong. Join text and numbers with str(): "Score: " + str(score). Writing "Score: " + score stops with a TypeError.
  • Tapping does nothing. Did you add retry=True? Without it the game just stops, and you press Run to play again.
  • It ends the moment it starts. The thing that ends the game is already true on the first frame, like a player and an enemy placed on top of each other. Start them apart.