Skip to main content

Stage 8: Add a Timer and Win State

Course progressStage 8 of 10
~40 min
One game, one Trinket

Keep building in your saved Python Arcade project.

This stage is part of the same game you started in Setup. Do the work in your own remixed Trinket — it’s the workspace on the right of this page.

Before you start

Your game should end when lives reach zero.

Build

a survival timer and win message

Learn

how time limits shape player goals

Ship

a game that can be won or lost

What we are building

The player needs a clear win condition. In this game, the player wins by surviving until time runs out.

Step 1 - Track time

Near your other variables, add:

GAME_SECONDS = 60
frames = 0

Add a timer writer:

timer_writer = turtle.Turtle()
timer_writer.hideturtle()
timer_writer.penup()
timer_writer.color("white")
timer_writer.setposition(-45, TOP - 40)

Step 2 - Draw the timer

Add this function:

def draw_timer(seconds_left):
timer_writer.clear()
timer_writer.write(f"Time: {seconds_left}", font=("Arial", 16, "bold"))

Call it before the loop:

draw_timer(GAME_SECONDS)

Step 3 - Update time in the game loop

At the top of the game loop, add:

frames += 1
seconds_left = GAME_SECONDS - frames // 50
draw_timer(seconds_left)

if seconds_left <= 0:
game_running = False

This assumes your game loop is sleeping for about 0.02 seconds each frame.

Step 4 - Show win or lose

Before the loop, add:

result = "GAME OVER"

When time runs out, set:

result = "YOU WIN"

At the end, write result instead of always writing GAME OVER:

score_writer.setposition(0, 0)
score_writer.write(result, align="center", font=("Arial", 28, "bold"))

Why this works

The timer turns your game into a challenge: survive for a set amount of time. A clear win state also makes the parent demo easier because the audience understands the goal.

Test your stage

  • Timer appears at the top.
  • Timer counts down.
  • Surviving to zero shows You Win.
  • Losing all lives still shows Game Over.

Debug checklist

  • If time counts too fast, increase the divisor in frames // 50.
  • If time counts too slowly, decrease it.
  • If every ending says Game Over, check that result = "YOU WIN" runs when the timer reaches zero.