Turtle Race Python Project: Complete Guide

Turtle Race Python Project

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.
assignment advertisement
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()
 #ready to draw
  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
ankur.pendown() #ready todraw


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
turtleVar.pendown() #ready

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:

Turtle Race Python Project
Turtle Race Python Project

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


Also Read:

  • Drawing Application in Python Tkinter
    Introduction In this article, we will design and construct a basic Drawing Application in Python Tkinter GUI, where we can simply draw something on the canvas using a pencil and erase with an eraser, as well as the ability to change the thickness of a pencil and eraser. We may also modify the canvas’s background…
  • Draw Flag of USA using Python Turtle
    Introduction Welcome everyone to copyassignment.com . In this tutorial, we are going to draw the Flag of USA using Python Turtle. The python turtle module is very easy to understand and learn. The python beginners can easily grasp the turtle functions as we are providing the comments and a detailed explanation of the code. Complete…
  • Draw Iron Man Face with Python Turtle: Tony Stark Face
    Introduction This blog will discuss how to draw Iron Man Face with Python Turtle, a GUI-based 2D graphics module in Python. The popular Marvel Cinematic Universe character Iron Man is played by the talented actor Robert Downey Jr. In this article, we will use sketchpy library to draw his face in just 3 lines of…
  • Draw TikTok Logo with Python Turtle
    Introduction This blog will discuss drawing TikTok Logo with Python Turtle, a GUI-based 2D graphics module in Python. TikTok is a trendy social media platform, primarily used for uploading and editing short dance videos, but has now become a funny, educational, and artistic video. Basics of the Python Turtle module: https://copyassignment.com/the-beginners-guide-to-python-turtle/ Official Documentation of the…
  • Draw Instagram Logo using Python Turtle
    Introduction This blog will discuss how to Draw Instagram Logo using Python Turtle module. Instagram is a very popular social media platform, primarily used for uploading and editing photos in the past, but it has now become a short-video platform with the introduction of Instagram Reels. Click here to check how to draw Instagram Reels…
  • I Love You Text in ASCII Art
    I Love You Text in ASCII Art This is simple I LOVE YOU Text in ASCII Art, you can use the button to copy this TEXT in ASCII Art and paste it into your system. Heart with Arrow Text in ASCII Art This is a simple Heart with Arrow Text in ASCII Art, you can…
  • Python Turtle Shapes- Square, Rectangle, Circle
    We all know that Python offers a variety of libraries for game Development, GUI Development, Data Visualization, and many more. These libraries not only make our work easy but also make the code efficient and deployable. Keeping this thing in mind, today, in this article, we will discuss and learn about Graphics Programming by using…
  • Python Turtle Commands and All Methods
    Introduction In this tutorial, we are going to learn about the python turtle commands and methods which we are using in the python turtle programming where Turtle is a python library that is used in creating games, pictures, and graphic images. If you have used Python Turtle a little bit, you may have observed that…
  • Happy Birthday Python Program In Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the Happy Birthday Python Program In Turtle. Let’s say you want to wish Happy Birthday to someone with your Python skills, in that case, the code below can help you to wish Happy Birthday with Python Program In Turtle. Complete code for Happy…
  • I Love You Program In Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the I Love You Program In Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Click here if you want…
  • Draw Python Logo in Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to Draw Python Logo in Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Step 1: Importing Libraries Step 2: Creating a…
  • Space Invaders game using Python
    We have already developed multiple games using the turtle library and now continuing our series of projects development in Python using the turtle library, today, in this article, we are going to develop a Space Invaders game using Python.As we all know the number of various built-in functions that a turtle library offers is more…

Share:
Avatar of Ankur Gajurel

Author: Ankur Gajurel

I am Ankur from Nepal trying to learn different aspects of Information Technology and Physics. I like building websites and minor projects with Python.