Display Score in Pygame

In this Display Score in Pygame tutorial, you will learn how to create a score system for your game.

Right now, when the player collects a coin, the coin moves to a new position. But the player doesn’t know how many coins have been collected. A score system solves this problem. Every time the player collects a coin, the score increases.

By the end of this lesson, your game will display the score at the top-left corner of the screen.


What Will You Learn?

In this lesson, you will learn:

  • How to create a score variable
  • How to display text in Pygame
  • How to update the score
  • How to show the score on the screen

Step 1: Create a Score Variable

After creating the coin position, add:

score = 0

This variable stores the player’s score.


Step 2: Create a Font

Below the score variable, add:

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

Here:

  • None = Use the default Pygame font.
  • 36 = Font size.

Step 3: Increase the Score

Find the coin collision code:

if player_rect.colliderect(coin_rect):

    print("Coin Collected!")

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

Now add:

score += 1

The updated code becomes:

if player_rect.colliderect(coin_rect):

    score += 1

    print("Coin Collected!")

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

Every collected coin increases the score by 1.


Step 4: Create the Score Text

Inside the game loop, before pygame.display.update(), add:

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

This creates the text that will be shown on the screen.


Step 5: Display the Score

Add:

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

Now the score appears in the top-left corner.


Complete Program

import pygame
import random

pygame.init()

# Create Window
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Display Score in Pygame")

# 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")

# 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

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

running = True

while running:

    screen.blit(background,(0,0))

    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))

    # Enemy Collision
    if player_rect.colliderect(enemy_rect):
        pygame.display.set_caption("Oops! Enemy Hit!")

    # Coin Collision
    if player_rect.colliderect(coin_rect):

        score += 1

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

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

    # Show Score
    score_text = font.render("Score: " + str(score), True, (255,255,255))
    screen.blit(score_text,(10,10))

    pygame.display.update()

    for event in pygame.event.get():

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

pygame.quit()

How Does font.render() Work?

This line creates the score text.

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

Let’s understand each part.

CodeMeaning
"Score: "Displays the word Score
str(score)Converts the number into text
TrueMakes the text smooth (anti-aliased)
(255,255,255)White text color

Example

Suppose:

score = 5

Then this line:

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

will display:

Score: 5

Mini Project

Create a game where:

  • The player collects coins.
  • The score increases after each coin.
  • The score is always visible.

Practice Challenge

Challenge 1

Change the score color to yellow.

(255,255,0)

Challenge 2

Increase the font size.

Try:

48

instead of:

36

Challenge 3

Display the score in the top-right corner.

Hint:

(650,10)

Common Mistakes

Score is not increasing

Check that:

score += 1

is inside the coin collision block.


Score is not visible

Make sure:

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

is called before:

pygame.display.update()

Text color matches the background

Choose a color that is easy to read.

For example:

  • White
  • Yellow
  • Black

depending on your background image.


Best Practice

Keep game information such as:

  • Score
  • Lives
  • Level
  • Timer

at the top of the screen.

Most games follow this design because it is easy for players to read.


What You Learned Today

Today you learned:

  • How to display the score in Pygame
  • How to create text
  • How to use fonts
  • How to update the score
  • How to display information on the screen

Quick Revision

  1. Which variable stores the player’s score?
  2. Which function creates text?
  3. Which function displays the score?
  4. Why do we use str(score)?

What’s Next?

In Lesson 14: Add Sound Effects in Pygame, you will make your game even more exciting.

You will add:

  • Coin collection sound
  • Background music
  • Enemy hit sound

Your game will finally have sound like a real arcade game.


Frequently Asked Questions (SEO & AEO Optimized)

How do I display the score in Pygame?

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

How do I increase the score in Pygame?

Increase a score variable, such as score += 1, whenever the player completes an action like collecting a coin.

Why do we use str(score) in Pygame?

The render() function displays text, so the score number must first be converted into a string using str().

Can I change the font size in Pygame?

Yes. Change the second value in pygame.font.Font(None, size).

Example:

pygame.font.Font(None, 48)

Can I display multiple text messages in Pygame?

Yes. You can create separate text objects for the score, lives, timer, level, and other game information.

How do professional games display scores?

Most games draw the score every frame so it updates instantly whenever the player earns points.

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

Scroll to Top