Timer in Pygame

In the previous lesson, we added Lives to our game. Now the player gets three chances before the game ends.

Let’s make the game even more exciting. Instead of allowing the player to play forever, we’ll give them 60 seconds to collect as many coins as possible. When the timer reaches 0, the game will end automatically. This is a common feature in racing games, puzzle games, and arcade games.

By the end of this lesson, your game will display a countdown timer that updates every second.


What You’ll Build Today

Your game will display something like this.

Score : 12

High Score : 25

Lives : 3

Time : 60

As the game runs, the timer will decrease.

Time : 59

Time : 58

Time : 57

When the timer reaches 0, the Game Over screen will appear.


Step 1: Create the Timer

Below your lives variable, add:

game_time = 60

The game will last for 60 seconds.


Step 2: Save the Start Time

Before the game loop, add:

start_time = pygame.time.get_ticks()

This records the time when the game starts.


Teacher’s Note

Don’t worry if pygame.time.get_ticks() looks new.

It simply tells us how many milliseconds have passed since Pygame started.

We will use this value to calculate how much time is left.


Step 3: Calculate the Remaining Time

Inside the game loop, before displaying the score, add:

elapsed_time = (pygame.time.get_ticks() - start_time) // 1000

remaining_time = game_time - elapsed_time

Let’s understand this.

pygame.time.get_ticks() returns milliseconds.

We divide by 1000 to convert milliseconds into seconds.

Now remaining_time tells us how many seconds are left.


Step 4: Display the Timer

Create a timer text.

timer_text = font.render(
    "Time : " + str(max(0, remaining_time)),
    True,
    (0,255,255)
)

Display it.

screen.blit(timer_text,(10,115))

Run the program.

You will see the timer counting down.


Step 5: End the Game

After calculating the remaining time, add:

if remaining_time <= 0:

    pygame.mixer.music.stop()

    gameover_sound.play()

    game_over = True

Now the game ends automatically when time runs out.


Complete Working Program

You only need to make five changes to the program from Lesson 19.

1. Create the timer

game_time = 60

2. Save the starting time

start_time = pygame.time.get_ticks()

3. Calculate the remaining time

elapsed_time = (pygame.time.get_ticks() - start_time) // 1000

remaining_time = game_time - elapsed_time

4. Display the timer

timer_text = font.render(
    "Time : " + str(max(0, remaining_time)),
    True,
    (0,255,255)
)

screen.blit(timer_text,(10,115))

5. Stop the game

if remaining_time <= 0:

    pygame.mixer.music.stop()

    gameover_sound.play()

    game_over = True

Save the file and run the game.

Now you have only 60 seconds to collect as many coins as possible.


How the Timer Works

Suppose the game started now.

Start Time = 0 seconds

After 15 seconds,

Elapsed Time = 15

The program calculates:

60 - 15 = 45

So the screen displays:

Time : 45

The timer continues until it reaches 0.


Common Errors

Timer Becomes Negative

Use:

max(0, remaining_time)

This prevents negative values from appearing on the screen.


Timer Starts Again After Restart

Inside your restart function, remember to reset the starting time.

start_time = pygame.time.get_ticks()

Otherwise, the timer will continue from the previous game.


Timer Is Not Updating

Make sure you calculate elapsed_time inside the game loop.

If you calculate it only once, the timer will never change.


Practice Challenge

Challenge 1

Change the timer to 30 seconds.


Challenge 2

Create a 90-second version of the game.


Challenge 3

When only 10 seconds are left, change the timer color to red.

Hint:

(255,0,0)

Teacher’s Tip

Whenever you create a timer, ask yourself one question:

“Should the timer stop when the game is paused?”

Professional games usually pause the timer when the player pauses the game.

We’ll learn how to build a Pause feature in a future lesson.


What You Learned Today

Today you learned:

  • How to create a timer in Pygame.
  • How to calculate elapsed time.
  • How to display a countdown timer.
  • How to end the game when the timer reaches zero.

Frequently Asked Questions

How do I create a timer in Pygame?

Use pygame.time.get_ticks() to measure elapsed time and subtract it from the total game time.

How do I make a countdown timer in Pygame?

Store the start time, calculate the elapsed time every frame, and display the remaining seconds.

Why does my timer show negative numbers?

Use max(0, remaining_time) before displaying the timer.

Can I create a 30-second or 2-minute game?

Yes. Change the value of game_time.

Example:

game_time = 30

or

game_time = 120

Why do games use timers?

Timers make games more exciting by creating a challenge and encouraging players to score more points before time runs out.


Summary

Today, your game became even more interesting.

The player now has:

  • A Score
  • A High Score
  • Three Lives
  • A Countdown Timer

The timer creates excitement and encourages players to collect coins quickly before the game ends.


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

Scroll to Top