Skip to content

Racing Turtle Game with Python

Create a fun racing game with Python turtle graphics where multiple players compete through random movement.

  • Raspberry Pi
  • Beginner
  • 45 minutes
  • 4 steps

Shop this project

Cart

You'll need

Prices from vilros.com. Tax and shipping at checkout.

Before you start, you'll need

  • Raspberry Pi (any model) or computer with Python 3
  • Internet connection (if using online Trinket editor)

Set up your Python environment

About 5 minutes

Get Python turtle graphics ready.

  1. On Raspberry Pi, Python 3 is pre-installed.
  2. Open a terminal or Python IDE (Thonny recommended).
  3. Alternatively, use online editor at trinket.io for browser-based coding.
  4. The turtle module is included with Python, no installation needed.

You should see

Python 3 environment is ready.

Create the race track

About 15 minutes

Draw lanes and start/finish markings.

  1. Create a new Python file: racing_turtles.py
  2. Import the turtle module.
  3. Use a for loop to draw vertical lane markers.
  4. Draw horizontal lines for start and finish positions.
  5. The track should have 3-4 lanes for different turtles.
python
import turtle
from random import randint

# Set up the screen
screen = turtle.Screen()
screen.setup(600, 600)
screen.title("Racing Turtles!")
screen.bgcolor("white")

# Draw track markings
marker = turtle.Turtle()
marker.speed(0)
marker.penup()
marker.hideturtle()

# Draw start line
marker.goto(-280, -100)
marker.pendown()
marker.forward(560)

# Draw finish line  
marker.penup()
marker.goto(-280, 200)
marker.pendown()
marker.forward(560)

# Draw lane dividers
for lane in range(-150, 200, 100):
    marker.penup()
    marker.goto(lane, -100)
    marker.setheading(90)
    marker.pendown()
    
    for _ in range(15):
        marker.forward(10)
        marker.penup()
        marker.forward(10)
        marker.pendown()

This creates a track with 4 lanes marked by dashed vertical lines.

You should see

The race track is drawn with start line, finish line, and lane dividers.

Add racing turtles

About 15 minutes

Create turtle racers with different colors.

  1. Create 3-4 turtle objects, one per player.
  2. Give each turtle a unique color.
  3. Position turtles at the starting line in different lanes.
  4. Set turtle shape and speed.
python
# Create racing turtles
colors = ["red", "blue", "green", "orange"]
turtles = []
start_positions = [-150, -50, 50, 150]

for i, color in enumerate(colors):
    racer = turtle.Turtle()
    racer.shape("turtle")
    racer.color(color)
    racer.penup()
    racer.goto(start_positions[i], -80)
    racer.setheading(90)
    turtles.append(racer)

Each turtle starts at its own lane position.

You should see

Four colored turtles are positioned at the start line.

Program the race

About 10 minutes

Make turtles move randomly each round.

  1. Create a race loop that runs for 10 rounds.
  2. Each round, each turtle moves a random number of steps (1-5).
  3. Track which turtle goes the furthest.
  4. Display the winner at the end.
python
# Race for 10 rounds
rounds = 10

for round_num in range(1, rounds + 1):
    print(f"Round {round_num}")
    
    for racer in turtles:
        # Move random steps (1-5)
        steps = randint(1, 5)
        racer.forward(steps * 10)
    
    screen.update()

# Find the winner
winner = max(turtles, key=lambda t: t.ycor())
winner_color = winner.color()[0]

# Display result
result = turtle.Turtle()
result.hideturtle()
result.penup()
result.goto(0, 250)
result.write(f"{winner_color.upper()} TURTLE WINS!", 
             align="center", font=("Arial", 24, "bold"))

screen.mainloop()

The turtle that travels the furthest wins after 10 rounds.

You should see

Turtles race and the winner is displayed.

What to do next

Stuck? Contact Vilros Support with your board model and what you see on screen.