Turtle Game Development

Turtle Game

This section develops a Turtle Game using the Python turtle module. This program gives an introduction to turtle-based game development.

Turtle Race Program

The program’s name is Turtle Race.  This program creates four turtle instances and steps them forward. Each turtle moves forward a random amount using the randint() function of the random() module.

PROGRAM EXAMPLE: TURTLE RACE GAME

Program name: turtle_race.py

import turtle
from turtle import *
import random					#1

# WINDOW SET-UP
window = turtle.Screen()
window.title("Turtle RACE") # WINDOW HEADING "TURTLE RACE"
turtle.bgcolor("lightgreen")
turtle.penup()

# Set Window title text position and text color. #2
# Write "TURTLE RACE" at (-140, 250)
turtle.setpos(-140, 250)
turtle.color("red")
turtle.write("TURTLE RACE", font = ("calibri", 36, "bold"))
turtle.penup()

# Write "RACE TRACK FINISH LINE" at (100, 180) 
# in black text.                           #3
turtle.setpos(100, 180)
turtle.color("black")
turtle.pendown()
turtle.write("RACE TRACK FINISH LINE", font = ("verdana", 13, "bold"))
turtle.penup()

# DRAW FINISH LINE
# Top of the finish line is at (200, (160-30))
# Use symbol "||" to draw finish line.
finishLine_Ystep = 30				#4
for i in range(10):
    turtle.setpos(200, (160 - (i * finishLine_Ystep)))
    turtle.write("||", font = ("Arial", 16, "bold"))

# We do not want the turtle that drew the finish line to be visible anymore.
turtle.hideturtle()

# Space between turtles in the y-direction is 50 pixels. 
turtle_Yspace = 50				#5

# Race start line is at x = -250.
# TURTLE1: draw turtle #1
turtle1 = Turtle()
turtle1.color("black")
turtle1.shape("turtle")
turtle1.penup()
y1 = 100 # Turtle#1 starting coordinates 
# are at (-250,100).                            #6
turtle1.goto(-250, y1)				#7
turtle1.pendown()
turtle1.write("turtle1", font = ("Arial", 16, "bold"))

###### TURTLE2: draw turtle2
turtle2 = Turtle()
turtle2.color("blue")
turtle2.shape("turtle")
turtle2.penup()
y2 = y1 - turtle_Yspace 	# Turtle2 is 50 pixels below Turtle#1.
turtle2.goto(-250, y2)
turtle2.pendown()
turtle2.write("turtle2", font = ("Arial", 16, "bold"))

###### TURTLE3: Draw turtle3
turtle3 = Turtle()
turtle3.color("magenta")
turtle3.shape("turtle")
turtle3.penup()
y3 = y2 - turtle_Yspace	# Turtle3 is 50 pixels below Turtle#2.
turtle3.goto(-250, y3)
turtle3.pendown()                  
turtle3.write("turtle3", font = ("Arial", 16, "bold"))

###### TURTLE4: draw turtle4
turtle4 = Turtle()
turtle4.color("red")
turtle4.shape("turtle")
turtle4.penup()
y4 = y3 - turtle_Yspace	# Turtle4 is 50 pixels below Turtle#2.
turtle4.goto(-250, y4)
turtle4.pendown()
turtle4.write("turtle4", font = ("Arial", 16, "bold"))

###### TURTLE RACE STARTS
# Each turtle moves forward random number of pixels between 1 and 5 pixels.
for i in range(1, 150):
    turtle1.forward(random.randint(1, 5))	#8
    turtle2.forward(random.randint(1, 5))
    turtle3.forward(random.randint(1, 5))
    turtle4.forward(random.randint(1, 5))

# If the turtle has reached the finish line (x = 190),
# check which turtle is farthest.
if turtle1.xcor() > 190 and turtle1.xcor() > turtle2.xcor() and turtle1.xcor() > turtle3.xcor() and turtle1.xcor() > turtle4.xcor(): 
                                                #9
    turtle1.penup()
    turtle1.goto(230, 100)			#10
    turtle1.pendown()
    turtle1.write("turtle1 won", font = ("Arial", 16, "bold"))

elif turtle2.xcor() > 190 and turtle2.xcor() > turtle1.xcor() and turtle2.xcor() > turtle3.xcor() and turtle2.xcor() > turtle4.xcor():
    turtle2.penup()
    turtle2.goto(230, 50)
    turtle2.pendown()
    turtle2.write("turtle2 won", font = ("Arial", 16, "bold"))

elif turtle3.xcor() > 190 and turtle3.xcor() > turtle1.xcor() and turtle3.xcor() > turtle2.xcor() and turtle3.xcor() > turtle4.xcor(): #  
    turtle3.penup()
    turtle3.goto(230, 0)
    turtle3.pendown()
    turtle3.write("turtle3 won", font = ("Arial", 16, "bold"))

else:
    turtle4.penup()
    turtle4.goto(230, -50)
    turtle4.pendown()
    turtle4.write("turtle4 won",font = ("Arial", 16, "bold"))

Below is the program output.

Turtle Game Program Called The Turtle Race
TURTLE RACE

Detailed line-by-line Explanation of the Program:

  1. import random
  2. turtle.setpos(-140, 250).

Set the coordinates of the text location of window title “TURTLE RACE”.

3. turtle.setpos(100, 180)

Set the coordinates of the text location “RACE TRACK FINISH LINE”.

4) finishLine_Ystep = 30

Use the symbol ‘||’ to draw the finish line. Space this symbol by 30 pixels in the y-direction to make the finish line.

5) turtle_Yspace = 50

Define the space between the four turtles. We space the turtles by 50 pixels in the y-direction.

6) turtle1.goto(-250, y1)

We set the starting line at x = -250.  All four turtles will start the race at x = -250.

Turtle #1 y-coordinate is y1 = 100. The next turtle will be placed at y = 100 – turtle_Yspace, which is defined as 50 in step 5. 

Thus, turtle # 1 starts at (-250, 100).

Turtle #2 starts at (-250, 50).

Turtle #3 starts at (-250, 0).

Turtle #4 starts at (-250, -50)

Move the Turtles Forward by Random Amounts

8) for i in range(1, 150):

turtle1.forward(random.randint(1, 5))

Step 8) uses the for loop to move all four turtles forward. 

Each turtle moves a different number of pixels in each loop. The number of pixels, a turtle moves depends on the random number returned by the random.randint(1,5) function. 

The random integer returned by this function is between 1 and 5.  Thus, the turtles will move forward at different speeds depending on the random number returned by the randint(1,5) function.

Decide Which Turtle Has Reached the Finish Line

9) if turtle1.xcor() > 190) AND turtle1.xcor() > turtle2.xcor() AND turtle1.xcor() > turtle3.xcor() AND turtle1.xcor() > turtle4.xcor:

In step 9), the program logic makes a decision about which turtle reached the finish line first (x = 190) AND which turtle’s x-coordinate (xcor) is larger than all other turtles’ x-coordinate.

The program first compares turtle #1 xcor against turtle #2 xcor, turtle #3 xcor, and turtle #4 xcor. If the result of the if condition is true, the program logic declares turtle #1 as the winner and prints ‘turtle1 won’.

If the result of the if test is false, the program then uses the elif test to compare turtle #2 xcor against turtle #3 xcor, and turtle #4 xcor.  If the result of this elif test condition is true, the program logic writes “turtle2 won”.

If the result is the elif test is false, the program does the same elif condition test with turtle #3 and declares “turtle3 won” if the result of the test is true, else it declares turtle #4 as the winner.

10) turtle1.goto(230, 100)

Step 10) moves the turtle to the coordinates where we would like to write ‘turtle1 won’. A similar goto statement is used to write ‘turtle2 won’, ‘turtle3 won’, and ‘turtle4 won’.

Copyright © 2019 – 2024 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights