Monday, April 21, 2025

Python Turtle - Turtle Race Game

This is source for simple turtle race game. It's not that hard to write and there is a lot of space for experimentation.

First test that you can run this code, and then we will explain:  

from turtle import *
import random

#Code for Players (Turtles)

t1 = Turtle()
t1.shape("turtle")
t1.speed(0)
t1.penup()
t1.left(90)
t1.color("red")

t2 = t1.clone()
t2.color("yellow")

t1.goto(-300, -300)
t2.goto(300,  -300)

#Code for Targets (also Turtle, but Circle Shape)

finish_1 = Turtle()
#finish_1.penup()
finish_1.goto(-300, 300)
#finish_1.pendown()
finish_1.circle(20)
finish_1.hideturtle()

finish_2 = Turtle()
#finish_2.penup()
finish_2.goto(300, 300)
#finish_2.pendown()
finish_2.circle(20)
finish_2.hideturtle()
#-----------------------------

def main():
    for x in range(600):
        t1.fd(random.randrange(3))
        t1.shapesize(5, 5, 5)
        t2.fd(random.randrange(3))
        t2.shapesize(5, 5, 5)
        
main()


We have 2 turtle objects here, turtle t1 and t2. Turtle t2 is clone of turtle t1, but color is different.

Shape for object is real turtle, and starting points are same horizontaly.

Starting orientation is upwards, to the top of the screen, because thats the path turtles need to travel. 

This is code for turtle t1:

t1 = Turtle()
t1.shape("turtle")
t1.speed(0)
t1.penup()
t1.left(90)
t1.color("red")

This is source for t2 which is clone of t1:

t2 = t1.clone()
t2.color("yellow")

Starting positions for both turtles:

t1.goto(-300, -300)
t2.goto(300,  -300)

Target Positions

Another 2 turtles will imitate target positions that t1 and t2 must reach. When turtles finish_1 and finish_2 reach their destination they will paint circle.

Circles are place where turtles t1 and t2 must land to claim victory.

This is code for objects finish_1 and finish_2:

finish_1 = Turtle()
#finish_1.penup()
finish_1.goto(-300, 300)
#finish_1.pendown()
finish_1.circle(20)
finish_1.hideturtle()

finish_2 = Turtle()
#finish_2.penup()
finish_2.goto(300, 300)
#finish_2.pendown()
finish_2.circle(20)
finish_2.hideturtle()

You can use penup() or pendown() methods to see full paths where objects (turtles) are going.

Custom Function for Turtle Movement

def main():
    for x in range(600):
        t1.fd(random.randrange(3))
        t1.shapesize(5, 5, 5)
        t2.fd(random.randrange(3))
        t2.shapesize(5, 5, 5)

With for loop we are setting turtle movement. Value 600 is enough. 

Both turtles t1 and t2 will change shapesize, just for fun.

We use random module and randrange() method to get random steps for both turtles. That will imitate different speeds in specific moment of time.

So far so good. We can visually detect winner, and in next tutorial we will do that automatically.

It is important to check corresponding YouTube tutorial for Turtle Race Game.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...