Turtle Race
Last week in class, I created a turtle race as apart of a challenge. I learned how to change the shape of turtles, turning the turtle into a turtle. I also learned about time and random imports.
The challenge was not a difficult one. I already knew how to use most of the commands such as functions, goto() commands, and if functions. The challenge that I did have is getting the turtles to stop at the finish line. I kept getting an error that I could not fix. Little did I know that all I had to do was make another repeat and then put the code that was getting an error inside it.
Something I wish I would have done if I knew how was to put all of the rats into a dictionary and call their properties. I could put them into a dictionary, but I could not call their properties without getting an error.
Here’s my finished code.
#imports
import turtle
import random
import time
#testsubjects
rat1 = turtle.Turtle()
rat2 = turtle.Turtle()
rat3 = turtle.Turtle()
rat4 = turtle.Turtle()
#variables
startBottom = (-250, -150)
startTop = (-250,150)
finishBottom = (250, -150)
finishTop = (250, 150)
randomm = random.randrange(0, 50)
#lines
rat1.speed(100)
rat1.penup()
rat1.goto(startBottom)
rat1.pendown()
rat1.goto(startTop)
rat1.penup()
rat1.goto(finishBottom)
rat1.pendown()
rat1.goto(finishTop)
#goratsstartingline
rat1.penup()
rat1.goto(-275, -100)
rat2.penup()
rat2.goto(-275, -50)
rat3.penup()
rat3.goto(-275, 100)
rat4.penup()
rat4.goto(-275, 50)
#ratcolors
rat1.color("red")
rat2.color("orange")
rat3.color("blue")
rat4.color("green")
#shapes
rat1.shape("turtle")
rat2.shape("turtle")
rat3.shape("turtle")
rat4.shape("turtle")
#print
print("Get ready...")
time.sleep(1)
print("Set...")
time.sleep(1)
print("Go!")
#gorats
rat1.pendown()
rat2.pendown()
rat3.pendown()
rat4.pendown()
for ratt in range(100):
if rat1.xcor() > 250:
print("rat1 won")
break
if rat2.xcor() > 250:
print("rat2 won")
break
if rat3.xcor() > 250:
print("rat3 won")
break
if rat4.xcor() > 250:
print("rat4 won")
break
for rattt in range(1):
rat1.forward(randomm)
rat2.forward(randomm)
rat3.forward(randomm)
rat4.forward(randomm)