I just got done with my first Turtle Race and still had one assignment left, a restart function. I just cant get it to work as i want. The goal is, an output of a question: Restart? y or n, and that'll restart the race() function. I am really in need of your guys' help.
from turtle import *
from random import randint
Andi = Turtle("turtle")
Andi.color("red")
Andi.shape("turtle")
Andi.penup()
Andi.goto(-300,200)
Andi.pendown()
Dom = Turtle("turtle")
Dom.color("green")
Dom.shape("turtle")
Dom.penup()
Dom.setpos(-300,180)
Dom.pendown()
Pete = Turtle("turtle")
Pete.color("blue")
Pete.shape("turtle")
Pete.penup()
Pete.setpos(-300,160)
Pete.pendown()
Emir = Turtle("turtle")
Emir.color("yellow")
Emir.shape("turtle")
Emir.penup()
Emir.setpos(-300,220)
Emir.pendown()
windowcolor=Screen()
windowcolor.bgcolor("black")
turtles = [Emir, Pete, Dom, Andi]
def race():
global turtles
winner = False
finishline = 300
while not winner:
for current_turtle in turtles:
move = randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor > finishline):
winner = True
current_turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
def restart():
global turtles
while race():
turtle.clear()
race ()
restart ()
while True:
while True:
answer = str(input("Restart? (y/n): "))
if answer in ("y", "n"):
break
print("Invalid awnser")
if answer == "y":
race
else:
print("Shutting off")
break
windowcolor.mainloop()
while True:
windowcolor.update()
What am I doing wrong, in my restart() function?
turtles
is an array of turtle
. You need to iterate the array and call every clear()
.
def restart():
global turtles
while race():
for turtle in turtles:
turtle.clear()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.