Python Turtle Race

In this Python Turtle tutorial, we will learn the turtle race in Python turtle and we will also cover different examples related to the Python turtle race. And, we will cover these topics.

  • Python turtle race
  • Python trinket turtle race
  • Python turtle race finish line

Python turtle race

In this section, we will learn about the turtle race in Python turtle.

In following the Python Turtle race we introduce the use of loops with help of the turtle module, where loops are helping the turtles (Players) to move from their stable position by using a number of steps.

Code:

In the following code, we will import the turtle module and also import math module for the turtle race.

  • turtle.screensize(wn_length, wn_height) is used to set the screen with a given length and height.
  • random.randrange(1, 20) is used to generate the random numbers.
  • self.tur.shape(‘turtle’) is used to give turtle shape to pen.
  • self.tur.color(color) is used to give the colors to the turtle.
  • self.tur.setpos(position) is used to set the position of the turtle where they start the race.
from turtle import *
import math
import random
import turtle

wn_length = 500
wn_height = 500

turtles = 8

turtle.screensize(wn_length, wn_height)


class racer(object):
    def __init__(self, color, position):
        self.position = position
        self.color = color
        self.tur = turtle.Turtle()
        self.tur.shape('turtle')
        self.tur.color(color)
        self.tur.penup()
        self.tur.setpos(position)
        self.tur.setheading(90)

    def move1(self):
        rand = random.randrange(1, 20)
        self.position = (self.position[0], self.position[1] + rand)
        self.tur.pendown()
        self.tur.forward(rand)

    def reset1(self):
        self.tur.penup()
        self.tur.setposition(self.position)


def setupfile(name, colors):
    file1 = open(name, 'w')
    for color in colors:
        file1.write(color + ' 0 \n')
    file1.close()


def startGame1():
    t_List = []
    turtle.clearscreen()
    turtle.hideturtle()
    colors = ["yellow", "blue", "green", 'cyan', 'red', 'purple', 'pink', 'brown', 'black']
    start = -(wn_length/2) + 20
    for t in range(turtles):
        newpositionX = start + t*(wn_length)//turtles
        t_List.append(racer(colors[t],(newpositionX, -230)))
        t_List[t].tur.showturtle()

    run = True
    while run:
        for t in t_List:
            t.move1()

        maxcolor = []
        maxdis = 0
        for t in t_List:
            if t.position[1] > 230 and t.position[1] > maxdis:
                maxdis = t.position[1]
                maxcolor = []
                maxcolor.append(t.color)
            elif t.position[1] > 230 and t.position[1] == maxdis:
                maxDis = t.pos[1]
                maxcolor.append(t.color)

        if len(maxcolor) > 0:
            run = False
            print('The winner is: ')
            for win in maxcolor:
                print(win)

    oldscore1 = []
    file = open('scores.txt', 'r')
    for line in file:
        l = line.split()
        color = l[0]
        score = l[1]
        oldscore1.append([color, score])

    file.close()

    file = open('scores.txt', 'w')

    for entry in oldscore1:
        for winner in maxcolor:
            if entry[0] == winner:
                entry[1] = int(entry[1]) + 1

        file.write(str(entry[0]) + ' ' + str(entry[1]) + '\n')


    file.close()


start = input('Would you like to play this game')
startGame1()

while True:
    print('-----')
    start = input('Would you like to play again')
    startGame1()

Output:

READ:  How to Create a Snake game in Python using Turtle

After running the above code we get the following output in which we can see the turtles are at the starting point and the race is starting. After starting the race they move from their initial position to their target position to win the race.

Python turtle race
Python turtle race Output

Python trinket turtle race

in this section, we will learn about the trinket turtle race in Python turtle.

Trinket turtle race in which turtle is placed at the starting line the turtle played the role of the player. When the game starts they move their starting point and run to the ending point and win the game.

In this game, the loop is used to draw the track and move the turtle from its stable position by using a number of steps.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle, and also import the randint module for generating random values.

  • speed(0) is used to give the speed to the turtle and 0 is the fastest speed.
  • forward(10) function is used to move the turtle in the forward direction.
  • backward(160) function is used to move the turtle in the backward direction.
  • tur1.color(‘green’) is used to give the green color to the turtle.
  • tur1.forward(randint(1,5)) is used to move forward with a random number of steps.
from turtle import *
import turtle
from random import randint

speed(0)
penup()
goto(-140, 140)

for step in range(15):
  write(step, align='center')
  right(90)
  for num in range(8):
    penup()
    forward(10)
    pendown()
    forward(10)
  penup()
  backward(160)
  left(90)
  forward(20)

tur1 = Turtle()
tur1.color('green')
tur1.shape('turtle')

tur1.penup()
tur1.goto(-160, 100)
tur1.pendown()

for turn in range(10):
  tur1.right(36)

tur2 = Turtle()
tur2.color('red')
tur2.shape('turtle')

tur2.penup()
tur2.goto(-160, 70)
tur2.pendown()

for turn in range(72):
  tur2.left(5)

tur3 = Turtle()
tur3.shape('turtle')
tur3.color('brown')

tur3.penup()
tur3.goto(-160, 40)
tur3.pendown()

for turn in range(60):
  tur3.right(6)

tur4 = Turtle()
tur4.shape('turtle')
tur4.color('orange')

tur4.penup()
tur4.goto(-160, 10)
tur4.pendown()

for turn in range(30):
  tur4.left(12)

for turn in range(100):
  tur1.forward(randint(1,5))
  tur2.forward(randint(1,5))
  tur3.forward(randint(1,5))
  tur4.forward(randint(1,5))
  

Output:

READ:  Django vs ReactJS: Which one is Best

After running the above code we get the following output in which we can see the race is done by the turtles ànd we can see the use of loop how it is working in steps and moving from its position and trying to reach the last line.

Python trinket turtle race
Python trinket turtle race Output

Python turtle race finish line

In this section, we will learn about the turtle race finish line in Python turtle.

In the following python turtle race, the turtle is played as a player which is placed at the starting point, and to win the race they reach their goal which is the finish line.

For wins the race which turtle run fast and reach first to the finish line won and the race is finished. Loop help the turtle to move from their starting position to the final.

Code:

In the following code, we will import the turtle library from turtle import *, import turtle, and also import randint function for generating the random numbers.

from turtle import *
import turtle
from random import randint

speed(0)
penup()
goto(-100,200)
for step in range(15):
    write(step, align='center')
    right(90)
    forward(10)
    pendown()
    forward(160)
    penup()
    backward(170)
    left(90)
    forward(20)

goto(200,250)
write("FinishLine", align='center')
pendown()
right(90)
forward(300)

tur = Turtle()
tur.color('red')
tur.shape('turtle')
tur.penup()
tur.goto(-120,160)
tur.pendown()

tur1 = Turtle()
tur1.color('blue')
tur1.shape('turtle')
tur1.penup()
tur1.goto(-120,130)
tur1.pendown()

tur2 = Turtle()
tur2.color('green')
tur2.shape('turtle')
tur2.penup()
tur2.goto(-120,100)
tur2.pendown()

for turn in range(100):
    speed(0)
    tur.forward(randint(1,5))
    tur1.forward(randint(1,5))
    tur2.forward(randint(1, 5))

Output:

After running the above code we get the following output in which we can see the race starts and the turtle is moving and want to reach their goal which is the finish line for winning the race.

Python turtle race finish line
Python turtle race finish line Output

You may like the following Python turtle tutorials:

READ:  PyTorch Conv1d [With 12 Amazing Examples]

So, in this tutorial, we discussed the Python turtle race and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle race
  • Python trinket turtle race
  • Python turtle race finish line