Python Turtle Race

In this challenge we will create a Python script to complete a Turtle race with four turtles racing across the screen.

We have started the code for you to set up the pitch of grass. You will complete this project step by step in order to:

    Add a start line to the left of the screen,
    Add a finish line to the right of the screen,
    Add four turtles on the start line,
    Animate the turtles to race across the screen,
    Find out which turtle is first to cross the finish line

Here is the Python code that you will need to complete:

Step 1: Start lineStep 2: Finish lineStep 3: Adding TurtlesStep 4: Race Time!Step 5: And the winner is...

Step 1: Adding a start line


To draw the start line as a vertical line on the left hand side of the screen, we will need to use the following (x,y) coordinates to tell the computer where does the start line starts and ends:

You will can add the following code to the above trinket to draw the start line:

#Let's draw the start line
myPen.penup()
myPen.goto(-160, 150)
myPen.pendown()
myPen.goto(-160, 0)

Step 2: Adding a finish line


It’s now your turn to use very similar code, this time to draw the finish line using the following (x,y) coordinates:

View Solution!

Step 3: Adding four turtles on the start line

We are now going to add the four competitors on the start line. We will have four turtles that we will need to position using the following (x,y) coordinates:

Here is the code to add the red turtle at position (-180, 140):

#Let's add our turtles
redTurtle = turtle.Turtle()
redTurtle.shape('turtle')
redTurtle.color('red')
redTurtle.pensize(2)
redTurtle.penup()
redTurtle.goto(-180, 140)
redTurtle.pendown()

Can you add some more lines of code to add 3 more turtles using the following coordinates:

  • Green Turtle at position (-180,110)
  • Blue Turtle at position (-180,80)
  • Yellow Turtle at position (-180,50)
View Solution!

Step 4: Animating the turtles to race across the screen


It’s now time for our turtles to race across the screen. To do so we will use an infinite while loop. Within each iteration of the loop, we will move each turtle forward by a randomly generated number of pixels between 1 and 8.

Here is the code to use for our infinite loop…

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))

Step 5: And the winner is…

As you can see with step 4, the four turtles carry on racing even after crossing the finish line. They even disappearing off screen!

Our aim is now to check, within each iteration of the while loop, to see if one of the four turtles has reached the finish line. To do so, we will need to check the x coordinate of each turtle. A turtle with an x coordinate greater than 180 has crossed the finish line!

Here is the final code for our racing loop!

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))
  
  if redTurtle.xcor()>180:
    print("Red Turtle wins!")
    break
  elif greenTurtle.xcor()>180:
    print("Green Turtle wins!")
    break
  elif blueTurtle.xcor()>180:
    print("Blue Turtle wins!")
    break
  elif yellowTurtle.xcor()>180:
    print("Yellow Turtle wins!")
    break
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 264

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,