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.
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

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:

  • Radha Krishna using Python Turtle
    Hello friends, in this article, we will learn to draw a simple drawing of Radha Krishna using Python Turtle. Please share if you like. Image Output: Video output: Hope you had fun going through this article and drawing a picture of Radha Krishna using a few simple methods provided by Python Turtle. Thank you for…
  • Drawing letter A using Python Turtle
    Today, we will learn how to draw the letter ‘A’ using Python Turtle. We all know how to write the letter ‘A’ using pen and paper, but would it not be great if you had a turtle, who can take your commands and draw it for you in whatever size or color you want? This…
  • Wishing Happy New Year 2023 in Python Turtle
    Guess what’s this time for? Yes, this is the time to say Hello to 2023! 2023 is approaching and if you want to wish Happy New Year in programming style, take a look here. Let’s see how we can write code for Wishing Happy New Year 2023 in Python Turtle. Code for Wishing Happy New…
  • Snake and Ladder Game in Python
    Introduction Are you looking for a fun way to learn Python coding? If so, why not create your own Snake and Ladder Game in Python? This step-by-step tutorial will show you how to craft a fully-functional game of Snake and Ladder in just a few simple steps. You’ll learn basic Python programming concepts, the fundamentals…
  • Draw Goku in Python Turtle
    Introduction Are you a big fan of Dragon Ball? Are you a programmer or someone who loves coding? If so, perhaps this tutorial on how to draw Goku in Python Turtle will interest you. If you’re an anime fan, you probably know that Goku is the main character of the popular show Dragon Ball Z….
  • Draw Mickey Mouse in Python Turtle
    Mickey Mouse is an iconic character that many of us are familiar with. If you’re a Python programmer, and a fan of Mickey Mouse, you might be wondering how to Draw Mickey Mouse in Python Turtle. It’s not as difficult as you might think! This blog post will walk you through the process of creating…
  • Happy Diwali in Python Turtle
    Code for Happy Diwali in Python Turtle Output: Also Read:
  • Draw Halloween in Python Turtle
    Introduction Today, we will learn how to Draw Halloween in Python Turtle. Halloween is celebrated in western countries and is dedicated to ones who have died like saints, martyrs, etc. We will draw a pumpkin which is used in celebrating Halloween. Click here to know about Halloween. Code to Draw Halloween in Python Turtle Output: Also…
  • Write Happy Halloween in Python Turtle
    Introduction Hello, in this article we will see how to Write Happy Halloween in Python Turtle which is a library of python. Turtle lets us handle graphics very easily and smoothly. We will initially make the background color black, then we will write Happy Halloween and at last, we will draw some colored circles to…
  • Draw Happy Diwali in Python Turtle
    Introduction Hello there, In this lesson, we will Draw Happy Diwali in Python Turtle. Turtle is a pre-installed Python module that allows users to draw drawings and shapes on a virtual canvas. The onscreen pen used for doodling is known as the turtle. In summary, the Python turtle library provides novice programmers with a fun…
  • Extract Audio from Video using Python
    In this article, we will build a project on how to extract audio from video using Python. Several libraries and techniques are available in Python for converting Video to Audio. We will use the moviepy library to convert video to audio in this project. For building this project make sure that you have the latest version…
  • 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 it with an eraser, as well as the ability to change the thickness of a pencil and eraser. We may also modify the canvas’s…

Share:

Author: Ayush Purawr