# Turtle Race Python Project: Complete Guide In this tutorial, we will be learning how to create a game where three turtles will randomly race each other. We will be using the “turtle” and the “random” module. If you don’t know much about turtle module basics, click here to get a complete turtle guide. I hope you will enjoy the Turtle Race Python Project. I will try to not go that much advanced. We will use very basic knowledge of Python and the Turtle module. So, let’s get started.

Turtle is a drawing module in Python, and it generally comes in-built with Python but if still, you found some problems like “No module named turtle”, then you can use pip command to install the turtle, type the following command in the terminal:-

pip install PythonTurtle

• First, import the “turtle” and the “random” module. Then, pick the pen up as we are not ready to draw yet. Position the turtle to (-140, 140) with the goto() method().
``````from random import *
from turtle import *
penup()
goto(-140,140) ``````
• Likewise, create a loop with a range of 15. This loop will draw the lines that are labeled with numbers from 0 to 15. Set the speed to 10 and write the current value of the range. Then, set the turtle right at an angle of 90 degrees and move it forward at 10 units. Now, as we are ready to draw, put the pen down and again up. Move the turtle backward at 160 units and left at an angle of 90 degrees. At last of the loop, move the turtle forward at 20 units.
``````for sp in range(15):
speed(10)
write(sp)
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)``````
• Likewise, create an instance of a turtle into an “ankur” variable. Set the color of the turtle to green and the shape to “turtle”. Pick the pen up as we are not ready to draw. Position the turtle to (-160, 100) and put the pen down.
``````ankur = Turtle()
ankur.color('green')
ankur.shape('turtle')
ankur.penup()
ankur.goto(-160,100)
ankur.pendown()``````
• Similarly, create another instance of a turtle into “gajurel” variable. Set the color of the turtle to “red” and the shape of the turtle to “turtle”. Pick the pen up and position it to (-160, 80). Put the pen down.
``````gajurel = Turtle()
gajurel.color('red')
gajurel.shape('turtle')
gajurel.penup()
gajurel.goto(-160,80)
gajurel.pendown()``````
• Accordingly, create an instance of a turtle into a “turtleVar” variable. Set he color of the turtle to blue and the shape to “turtle”. Pick the pen up as we are not ready to draw. Position the turtle to (-160, 60) and put the pen down.
``````turtleVar = Turtle()
turtleVar.color('blue')
turtleVar.shape('turtle')
turtleVar.penup()
turtleVar.goto(-160,60)
turtleVar.pendown()``````
• Lastly, create a loop with the range of 100 where the turtle will race. Move the “ankur” turtle forward at a random integer from 1 to 5 units. Move the “gajurel” turtle also forward at a random integer from 1 to 5 units. Similarly, move the “turtleVar” turtle even forward at a random integer from 1 to 5 units.
``````for turn in range(100):
ankur.forward(randint(1,5))
gajurel.forward(randint(1,5))
turtleVar.forward(randint(1,5))``````

Now, let’s see the complete code of Turtle Race Python Project, and we will use comments to explain the

## Complete Turtle Race Python Project Code:

```import turtle
from random import *
from turtle import *

penup()
goto(-140,140) #positioning the pen

for sp in range(15): #loop for creating the lines labelled with numbers
speed(10)
#setting the speed
write(sp)
#writing the int
right(90)
#setting right at 90 degrees
forward(10)
#forward at 10 units
pendown()
forward(150)
#forward at 150 units
penup()
#not ready to draw
backward(160)
#back at 160 units
left(90)
#left set at 90 degrees
forward(20)
#forward at 20 units

ankur = Turtle() #inheriting the turtle
ankur.color('green') #setting the color to green for the first turtle
ankur.shape('turtle') #setting the shape to "turtle"
ankur.penup() #not ready to draw
ankur.goto(-160,100) #positioning the turtle

gajurel = Turtle() #inheriting another turtle
gajurel.color('red') #setting the color og the turtle to red
gajurel.shape('turtle') #declaring the shape of the turtle to "turtle"
gajurel.penup() #not ready to draw
gajurel.goto(-160,80) #positioning
gajurel.pendown() #ready to draw

turtleVar = Turtle() #inheriting the last turtle
turtleVar.color('blue') #setting the color of the turtle as "blue"
turtleVar.shape('turtle') #declaring the shape of the turtle
turtleVar.penup() #not ready to draw
turtleVar.goto(-160,60) #positioning

for turn in range(100): #loop for the racew
ankur.forward(randint(1,5)) #setting the speed randomly with the "random" module
gajurel.forward(randint(1,5)) #setting the speed randomly with the "random" module
turtleVar.forward(randint(1,5)) #setting the speed randomly with the "random" module

turtle.done()```

## Output:

Thank you for reading till the end. Hope you have learned all the basic concepts of the “turtle” module. There is a Beginner’s Guide to the Python Turtle Module. We also have many other small projects on Python Turtle.

Keep Learning, Keep Coding 