Game Over Screen in Pygame

In this Game Over Screen in Pygame tutorial, you will learn how to end the game when the player touches an enemy.

Right now, if the player touches the enemy, only the window title changes. That doesn’t feel like a real game. Instead, we want to:

  • Stop the game.
  • Show a Game Over message.
  • Display the final score.
  • Ask the player to close the window.

By the end of this lesson, your game will have its first ending screen.


What Will You Learn?

In this lesson, you will learn:

  • How to stop the game
  • How to display a Game Over message
  • How to show the final score
  • How to use game state variables

Step 1: Create a Game State

Above the game loop, add:

game_over = False

This variable tells us whether the game is still running.


Step 2: Detect Enemy Collision

Replace:

pygame.display.set_caption("Oops! Enemy Hit!")

with:

game_over = True

Now, when the player touches the enemy, the game will enter the Game Over state.


Step 3: Draw the Game Over Screen

Inside the game loop, replace your drawing code with this logic:

if not game_over:

    # Draw game normally

else:

    # Show Game Over screen

Step 4: Create Fonts

Before the game loop, create two fonts.

font = pygame.font.Font(None,36)

game_over_font = pygame.font.Font(None,72)

The first font is for the score.

The second font is for the large Game Over message.


Step 5: Display the Game Over Message

When the game ends, create this text:

game_over_text = game_over_font.render(
    "GAME OVER",
    True,
    (255,0,0)
)

Display it:

screen.blit(game_over_text,(220,200))

Step 6: Show the Final Score

Create another message.

score_text = font.render(
    "Final Score: " + str(score),
    True,
    (255,255,255)
)

Display it:

screen.blit(score_text,(300,300))

Complete Program

import pygame
import random

pygame.init()

# -----------------------------
# Create Window
# -----------------------------
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Game Over Screen")

# -----------------------------
# Load Images
# -----------------------------
background = pygame.image.load("images/background.png")
player = pygame.image.load("images/player.png")
enemy = pygame.image.load("images/enemy.png")
coin = pygame.image.load("images/coin.png")

# -----------------------------
# Fonts
# -----------------------------
font = pygame.font.Font(None,36)
game_over_font = pygame.font.Font(None,72)

# -----------------------------
# Player
# -----------------------------
player_x = 350
player_y = 250
player_speed = 5

# -----------------------------
# Enemy
# -----------------------------
enemy_x = 0
enemy_y = 100
enemy_speed = 3

# -----------------------------
# Coin
# -----------------------------
coin_x = random.randint(0,736)
coin_y = random.randint(0,536)

# -----------------------------
# Score
# -----------------------------
score = 0

# -----------------------------
# Game State
# -----------------------------
game_over = False

running = True

while running:

    # Close Window
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    if not game_over:

        # Draw Background
        screen.blit(background,(0,0))

        # Keyboard Input
        keys = pygame.key.get_pressed()

        if keys[pygame.K_RIGHT] and player_x < 736:
            player_x += player_speed

        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_speed

        if keys[pygame.K_UP] and player_y > 0:
            player_y -= player_speed

        if keys[pygame.K_DOWN] and player_y < 536:
            player_y += player_speed

        # Enemy Movement
        enemy_x += enemy_speed

        if enemy_x > 800:
            enemy_x = -64

        # Rectangles
        player_rect = player.get_rect(topleft=(player_x,player_y))
        enemy_rect = enemy.get_rect(topleft=(enemy_x,enemy_y))
        coin_rect = coin.get_rect(topleft=(coin_x,coin_y))

        # Coin Collision
        if player_rect.colliderect(coin_rect):

            score += 1

            coin_x = random.randint(0,736)
            coin_y = random.randint(0,536)

        # Enemy Collision
        if player_rect.colliderect(enemy_rect):

            game_over = True

        # Draw Objects
        screen.blit(player,(player_x,player_y))
        screen.blit(enemy,(enemy_x,enemy_y))
        screen.blit(coin,(coin_x,coin_y))

        # Draw Score
        score_text = font.render(
            "Score: " + str(score),
            True,
            (255,255,255)
        )

        screen.blit(score_text,(10,10))

    else:

        screen.fill((20,20,20))

        game_over_text = game_over_font.render(
            "GAME OVER",
            True,
            (255,0,0)
        )

        score_text = font.render(
            "Final Score: " + str(score),
            True,
            (255,255,255)
        )

        info_text = font.render(
            "Close the window to exit.",
            True,
            (255,255,255)
        )

        screen.blit(game_over_text,(210,180))
        screen.blit(score_text,(300,280))
        screen.blit(info_text,(240,340))

    pygame.display.update()

pygame.quit()

What Changed in This Lesson?

Compared to the previous lesson, we added only three new ideas:

  1. A game_over variable to control the game state.
  2. A large GAME OVER message.
  3. The Final Score screen.

This keeps the lesson simple while introducing a concept used in almost every game.


Mini Project

Play your game and try to:

  • Collect as many coins as possible.
  • Avoid the moving enemy.
  • See your final score when the game ends.

Challenge yourself to beat your own high score!


Practice Challenge

Challenge 1

Change the GAME OVER text color from red to yellow.

Challenge 2

Display this message below the score:

Thanks for Playing!

Challenge 3

Change the background color of the Game Over screen to dark blue.


Common Mistakes

Game Over never appears

Make sure this line is inside the enemy collision block:

game_over = True

Score disappears

The score is only shown during gameplay.

Use another font.render() call inside the Game Over screen to display the final score.


Player still moves after Game Over

All movement code should be inside:

if not game_over:

What You Learned Today

You learned:

  • How to create a Game Over screen
  • How to stop the game after a collision
  • How to display the final score
  • How to use a game state variable
  • How real games handle the end of a level

Frequently Asked Questions (SEO & AEO Optimized)

How do I create a Game Over screen in Pygame?

Create a game state variable, detect the collision, stop updating the gameplay, and display a Game Over message with font.render().

How do I stop a game in Pygame?

Set a variable such as game_over = True and skip the gameplay logic while showing a Game Over screen.

How do I display text in Pygame?

Create a font with pygame.font.Font(), render the text using font.render(), and display it with screen.blit().

How do I show the final score in Pygame?

Store the score in a variable and display it on the Game Over screen using font.render().

Why do games use a Game Over screen?

A Game Over screen lets players know the game has ended, shows their score, and prepares them to restart or quit the game.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top