Press ESC to close

Turtle Race in Python

Photo by Thought.co

Creating a turtle race in Python using the turtle module is a fun way to visualize basic programming concepts. Here’s a step-by-step guide on how to create a simple turtle race:

1. Setup

First, ensure you have Python installed, as the turtle module comes pre-installed with Python.

2. Import the turtle Module Start by importing the turtle module and setting up the screen.

import turtle
import random

# Set up the screen
screen = turtle.Screen()
screen.title("Turtle Race")
screen.bgcolor("lightblue")

# Set up the finish line
finish_line = turtle.Turtle()
finish_line.penup()
finish_line.goto(200, 150)
finish_line.pendown()
finish_line.right(90)
finish_line.forward(300)
finish_line.hideturtle()

3. Create the Turtles

You can create multiple turtles for the race. Each turtle will be assigned a different color.

# Create turtles
colors = ["red", "blue", "green", "orange"]
turtles = []

starting_y = 100

for color in colors:
    racer = turtle.Turtle()
    racer.color(color)
    racer.shape("turtle")
    racer.penup()
    racer.goto(-200, starting_y)
    starting_y -= 50
    turtles.append(racer)

4. Create the Race Logic

Now, define the race logic where each turtle will move forward by a random number of steps.

# Start the race
race_on = True

while race_on:
    for racer in turtles:
        # Move the turtle forward by a random distance
        racer.forward(random.randint(1, 10))
        
        # Check if any turtle has crossed the finish line
        if racer.xcor() >= 200:
            race_on = False
            winner = racer.color()[0]
            break

# Declare the winner
print(f"The winner is the {winner} turtle!")

5. Add a Delay and Keep the Window Open

Ensure the window stays open after the race, so you can see the result.

# Delay to keep the window open
turtle.done()

6. Complete Code

Here’s the complete code to create a simple turtle race:

import turtle
import random

# Set up the screen
screen = turtle.Screen()
screen.title("Turtle Race")
screen.bgcolor("lightblue")

# Set up the finish line
finish_line = turtle.Turtle()
finish_line.penup()
finish_line.goto(200, 150)
finish_line.pendown()
finish_line.right(90)
finish_line.forward(300)
finish_line.hideturtle()

# Create turtles
colors = ["red", "blue", "green", "orange"]
turtles = []

starting_y = 100

for color in colors:
    racer = turtle.Turtle()
    racer.color(color)
    racer.shape("turtle")
    racer.penup()
    racer.goto(-200, starting_y)
    starting_y -= 50
    turtles.append(racer)

# Start the race
race_on = True

while race_on:
    for racer in turtles:
        racer.forward(random.randint(1, 10))
        if racer.xcor() >= 200:
            race_on = False
            winner = racer.color()[0]
            break

# Declare the winner
print(f"The winner is the {winner} turtle!")

# Keep the window open
turtle.done()

Summary

  • Turtle Setup: Create a screen and set up the turtles at the starting positions.
  • Race Logic: Use a loop to move each turtle forward randomly until one crosses the finish line.
  • Winner Announcement: Declare the winning turtle.

This simple turtle race is a great introduction to using the turtle module for animations and basic event handling in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *