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
CartYou'll need
Raspberry Pi 5 8GB Board Only$175.00
Vilros Raspberry Pi 5 Basic Starter Kit4GB / Aluminum Alloy W/ Passive+ Active Cooling$179.99
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.
- On Raspberry Pi, Python 3 is pre-installed.
- Open a terminal or Python IDE (Thonny recommended).
- Alternatively, use online editor at trinket.io for browser-based coding.
- 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.
- Create a new Python file: racing_turtles.py
- Import the turtle module.
- Use a for loop to draw vertical lane markers.
- Draw horizontal lines for start and finish positions.
- The track should have 3-4 lanes for different turtles.
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.
- Create 3-4 turtle objects, one per player.
- Give each turtle a unique color.
- Position turtles at the starting line in different lanes.
- Set turtle shape and speed.
# 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.
- Create a race loop that runs for 10 rounds.
- Each round, each turtle moves a random number of steps (1-5).
- Track which turtle goes the furthest.
- Display the winner at the end.
# 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.