如何选择随机的乌龟移动随机数量的步骤?

发布于 2025-01-29 03:37:18 字数 5522 浏览 5 评论 0原文

我正在做一场乌龟比赛,其中有5只不同的海龟互相竞赛,它们各自采取了一定的步骤。但是,我该如何选择一个随机的乌龟来移动随机数量的步骤?我找不到使程序选择乌龟的方法。我已经尝试制作一个颜色库,然后制作颜色海龟并选择随机颜色(Reddit上的某人建议这样做),但这只是在屏幕中间添加了一只乌龟,但并没有停止前进。所以我尝试了:

colours = ("red","blue","green","yellow","purple")
turtles = {colour: turtle.Turtle() for colour in colours}
ranTur = turtles[random.choice(colours)]

这正是Reddit上有人给我的东西,但这无效。我希望该程序选择我正在赛车的5只乌龟之一(我有更多的海龟,但它们是为了画车道和东西),然后使乌龟向前移动X空间。只是做rantur = [“红色”,“蓝色”,“绿色”,“黄色”,“紫色”]也不起作用。有没有办法选择随机海龟?还是不可能?

这是我的代码,而没有该部分:

import turtle
import random
import time

#turtles
red = turtle.Turtle()
blue = turtle.Turtle()
green = turtle.Turtle()
yellow = turtle.Turtle()
purple = turtle.Turtle()
lijn = turtle.Turtle()
winner1 = turtle.Turtle()
winner2 = turtle.Turtle()
arrowR = turtle.Turtle()
arrowB = turtle.Turtle()
arrowG = turtle.Turtle()
arrowY = turtle.Turtle()
arrowP = turtle.Turtle()

#font
fontLines = ("Arial", 16, "normal")

#turtle colors
red.color("red")
blue.color("blue")
green.color("green")
yellow.color("yellow")
purple.color("purple")
lijn.color("black")
winner1.color("black")
arrowR.color("red")
arrowB.color("blue")
arrowG.color("green")
arrowY.color("yellow")
arrowP.color("purple")

#turtle penup
red.penup()
blue.penup()
green.penup()
yellow.penup()
purple.penup()
winner1.penup()
winner2.penup()
arrowR.penup()
arrowB.penup()
arrowG.penup()
arrowY.penup()
arrowP.penup()
lijn.penup()

#turtle shapes
red.shape("turtle")
blue.shape("turtle")
green.shape("turtle")
yellow.shape("turtle")
purple.shape("turtle")
arrowR.shape("arrow")
arrowB.shape("arrow")
arrowG.shape("arrow")
arrowY.shape("arrow")
arrowP.shape("arrow")

#turtle speed
arrowR.speed(0)
arrowB.speed(0)
arrowG.speed(0)
arrowY.speed(0)
arrowP.speed(0)
red.speed(0)
blue.speed(0)
green.speed(0)
yellow.speed(0)
purple.speed(0)
winner1.speed(0)
winner2.speed(0)
lijn.speed(0)

#hide turtles
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
winner1.hideturtle()
winner2.hideturtle()
lijn.hideturtle()

#arrow positions
arrowR.goto(-190,70)
arrowB.goto(-190,35)
arrowG.goto(-190,0)
arrowY.goto(-190,-35)
arrowP.goto(-190,-70)

#start turtles
xBegin = -180
def raceTur():
    red.goto(-180,70)
    blue.goto(-180,35)
    green.goto(-180,0)
    yellow.goto(-180,-35)
    purple.goto(-180,-70)

raceTur()

#race lanes
def line(x,y,width,text):
    lijn.penup()
    lijn.goto(x,y)
    
    for i in range (15):
        lijn.write(text, font=fontLines)
        lijn.forward(width)
    
def raceBaan():
    line(-150,60,20,"|")
    line(-150,25,20,"|")
    line(-150,-10,20,"|")
    line(-150,-45,20,"|")
    line(-150,-80,20,"|")

raceBaan()

# reset
def reset():
    raceTur()
    raceBaan()
    
    
#numbers
lijn.goto(-150,90)
lijn.write("0       1       2       3       4       5       6       7       8       9      10     11     12     13     14")

#winner text
winner1.goto(xBegin,-180)
winner1.write("De winnaar is: ",font=("Arial",30,"normal"))
winner2.goto(20,-180)

#finish line
finishLine = 140

#race
def Race():
    while True:
        x = random.randint(1,10)
        red.forward(x)
        if red.xcor() > blue.xcor() and red.xcor() > green.xcor() and red.xcor() > yellow.xcor() and red.xcor() > purple.xcor():
            arrowR.showturtle()
        else:
            arrowR.hideturtle()
        if red.pos()[0]>=finishLine:
            winner2.color("red")
            winner2.write("rood",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        blue.forward(x)
        if blue.xcor() > red.xcor() and blue.xcor() > green.xcor() and blue.xcor() > yellow.xcor() and blue.xcor() > purple.xcor():
            arrowB.showturtle()
        else:
            arrowB.hideturtle()
        if blue.pos()[0]>=finishLine:
            winner2.color("blue")
            winner2.write("blauw",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        green.forward(x)
        if green.xcor() > red.xcor() and green.xcor() > blue.xcor() and green.xcor() > yellow.xcor() and green.xcor() > purple.xcor():
            arrowG.showturtle()
        else:
            arrowG.hideturtle()
        if green.pos()[0]>=finishLine:
            winner2.color("green")
            winner2.write("groen",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        yellow.forward(x)
        if yellow.xcor() > red.xcor() and yellow.xcor() > green.xcor() and yellow.xcor() > blue.xcor() and yellow.xcor() > purple.xcor():
            arrowY.showturtle()
        else:
            arrowY.hideturtle()
        if yellow.pos()[0]>=finishLine:
            winner2.color("yellow")
            winner2.write("geel",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        purple.forward(x)
        if purple.xcor() > blue.xcor() and purple.xcor() > green.xcor() and purple.xcor() > yellow.xcor() and purple.xcor() > red.xcor():
            arrowP.showturtle()
        else:
            arrowP.hideturtle()
        if purple.pos()[0]>=finishLine:
            winner2.color("purple")
            winner2.write("paars",font=("Arial",30,"bold"))
            break
    time.sleep(3)
    winner2.clear()
    arrowR.hideturtle()
    arrowB.hideturtle()
    arrowG.hideturtle()
    arrowY.hideturtle()
    arrowP.hideturtle()
    reset()
    Race()    
        
Race()

turtle.done()

Im making a turtle race where 5 different turtles are racing each other and they each take a random amount of steps. But how do I make it that it also chooses a random turtle to move a random number of steps? I can't find a way to make the program choose a turtle. I already tried to make a library of colors and then make the colors turtles and choose a random color (someone on reddit suggested this) but that just added a turtle in the middle of the screen that didn't stop going forward. so i tried:

colours = ("red","blue","green","yellow","purple")
turtles = {colour: turtle.Turtle() for colour in colours}
ranTur = turtles[random.choice(colours)]

This is exactly what someone on reddit gave me but this didn't work. I want the program to choose one of my 5 turtles that are racing (i have more turtles but they are for drawing the lanes and stuff) and then make that turtle move forward x spaces. And just doing ranTur = ["red","blue","green","yellow","purple"] didn't work either. Is there a way to choose random turtles? Or is that just not possible?

This is my code without that part:

import turtle
import random
import time

#turtles
red = turtle.Turtle()
blue = turtle.Turtle()
green = turtle.Turtle()
yellow = turtle.Turtle()
purple = turtle.Turtle()
lijn = turtle.Turtle()
winner1 = turtle.Turtle()
winner2 = turtle.Turtle()
arrowR = turtle.Turtle()
arrowB = turtle.Turtle()
arrowG = turtle.Turtle()
arrowY = turtle.Turtle()
arrowP = turtle.Turtle()

#font
fontLines = ("Arial", 16, "normal")

#turtle colors
red.color("red")
blue.color("blue")
green.color("green")
yellow.color("yellow")
purple.color("purple")
lijn.color("black")
winner1.color("black")
arrowR.color("red")
arrowB.color("blue")
arrowG.color("green")
arrowY.color("yellow")
arrowP.color("purple")

#turtle penup
red.penup()
blue.penup()
green.penup()
yellow.penup()
purple.penup()
winner1.penup()
winner2.penup()
arrowR.penup()
arrowB.penup()
arrowG.penup()
arrowY.penup()
arrowP.penup()
lijn.penup()

#turtle shapes
red.shape("turtle")
blue.shape("turtle")
green.shape("turtle")
yellow.shape("turtle")
purple.shape("turtle")
arrowR.shape("arrow")
arrowB.shape("arrow")
arrowG.shape("arrow")
arrowY.shape("arrow")
arrowP.shape("arrow")

#turtle speed
arrowR.speed(0)
arrowB.speed(0)
arrowG.speed(0)
arrowY.speed(0)
arrowP.speed(0)
red.speed(0)
blue.speed(0)
green.speed(0)
yellow.speed(0)
purple.speed(0)
winner1.speed(0)
winner2.speed(0)
lijn.speed(0)

#hide turtles
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
winner1.hideturtle()
winner2.hideturtle()
lijn.hideturtle()

#arrow positions
arrowR.goto(-190,70)
arrowB.goto(-190,35)
arrowG.goto(-190,0)
arrowY.goto(-190,-35)
arrowP.goto(-190,-70)

#start turtles
xBegin = -180
def raceTur():
    red.goto(-180,70)
    blue.goto(-180,35)
    green.goto(-180,0)
    yellow.goto(-180,-35)
    purple.goto(-180,-70)

raceTur()

#race lanes
def line(x,y,width,text):
    lijn.penup()
    lijn.goto(x,y)
    
    for i in range (15):
        lijn.write(text, font=fontLines)
        lijn.forward(width)
    
def raceBaan():
    line(-150,60,20,"|")
    line(-150,25,20,"|")
    line(-150,-10,20,"|")
    line(-150,-45,20,"|")
    line(-150,-80,20,"|")

raceBaan()

# reset
def reset():
    raceTur()
    raceBaan()
    
    
#numbers
lijn.goto(-150,90)
lijn.write("0       1       2       3       4       5       6       7       8       9      10     11     12     13     14")

#winner text
winner1.goto(xBegin,-180)
winner1.write("De winnaar is: ",font=("Arial",30,"normal"))
winner2.goto(20,-180)

#finish line
finishLine = 140

#race
def Race():
    while True:
        x = random.randint(1,10)
        red.forward(x)
        if red.xcor() > blue.xcor() and red.xcor() > green.xcor() and red.xcor() > yellow.xcor() and red.xcor() > purple.xcor():
            arrowR.showturtle()
        else:
            arrowR.hideturtle()
        if red.pos()[0]>=finishLine:
            winner2.color("red")
            winner2.write("rood",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        blue.forward(x)
        if blue.xcor() > red.xcor() and blue.xcor() > green.xcor() and blue.xcor() > yellow.xcor() and blue.xcor() > purple.xcor():
            arrowB.showturtle()
        else:
            arrowB.hideturtle()
        if blue.pos()[0]>=finishLine:
            winner2.color("blue")
            winner2.write("blauw",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        green.forward(x)
        if green.xcor() > red.xcor() and green.xcor() > blue.xcor() and green.xcor() > yellow.xcor() and green.xcor() > purple.xcor():
            arrowG.showturtle()
        else:
            arrowG.hideturtle()
        if green.pos()[0]>=finishLine:
            winner2.color("green")
            winner2.write("groen",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        yellow.forward(x)
        if yellow.xcor() > red.xcor() and yellow.xcor() > green.xcor() and yellow.xcor() > blue.xcor() and yellow.xcor() > purple.xcor():
            arrowY.showturtle()
        else:
            arrowY.hideturtle()
        if yellow.pos()[0]>=finishLine:
            winner2.color("yellow")
            winner2.write("geel",font=("Arial",30,"bold"))
            break
        
        x = random.randint(1,10)
        purple.forward(x)
        if purple.xcor() > blue.xcor() and purple.xcor() > green.xcor() and purple.xcor() > yellow.xcor() and purple.xcor() > red.xcor():
            arrowP.showturtle()
        else:
            arrowP.hideturtle()
        if purple.pos()[0]>=finishLine:
            winner2.color("purple")
            winner2.write("paars",font=("Arial",30,"bold"))
            break
    time.sleep(3)
    winner2.clear()
    arrowR.hideturtle()
    arrowB.hideturtle()
    arrowG.hideturtle()
    arrowY.hideturtle()
    arrowP.hideturtle()
    reset()
    Race()    
        
Race()

turtle.done()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2025-02-05 03:37:18

有没有办法选择随机海龟?还是不可能?

简单的答案是:可以使用 random.choice(list_of_turtles) 随机模块的函数。它从项目列表中选择一个随机项目,并与random.randint(1,10)一起使用。要移动

在您的脚本中更改末尾的代码到:

#finish line
finishLine = 140

dict_of_turtles = {
red     : ( arrowR , "red"   , "rood"  ),
blue    : ( arrowB , "blue"  , "blauw" ),
green   : ( arrowG , "green" , "groen" ),
yellow  : ( arrowY , "yellow", "geel"  ),
purple  : ( arrowP , "purple", "paars" ),
}
list_of_turtles = list( dict_of_turtles.keys() )

def Race():
    
    while True: 
        random_turtle = random.choice(list_of_turtles)
        other_turtles = list_of_turtles[:] ; other_turtles.remove(random_turtle)
        arrow_turtle, turtle_color, turtle_color_text  = dict_of_turtles[random_turtle]
        x = random.randint(1,10)
        random_turtle.forward(x)
        if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
            arrow_turtle.showturtle()
        else:
            arrow_turtle.hideturtle()
        if random_turtle.pos()[0] >= finishLine:
            winner2.color(turtle_color)
            winner2.write(turtle_color_text,font=("Arial",30,"bold"))
            break

    time.sleep(3)
    winner2.clear()
    arrowR.hideturtle()
    arrowB.hideturtle()
    arrowG.hideturtle()
    arrowY.hideturtle()
    arrowP.hideturtle()
    reset()
    Race()    
        
Race()

turtle.done()

End享受赛车和较短的脚本,因为无需在每个环内移动所有乌龟。

为了完整性,在重新设计的原始代码的完整版本下,专注于使代码更加紧凑和“ Pythonic”,并提供随机选择乌龟移动的要求的特征。

import turtle
import random
import time
screen = turtle.Screen()
colors_EN       = ['red',   'blue', 'green','yellow', 'purple']
colors_NL       = ["rood", "blauw", "groen",  "geel", "paars" ]
len_list        = len(colors_EN)
turtles_racing  = [ turtle.Turtle() for t in range(len_list)]
arrow_turtles   = [ turtle.Turtle() for t in range(len_list)]
lines_and_texts = [ turtle.Turtle() for t in range(4)]
lines_and_texts[1].color("black")
fontLines = ("Arial", 16, "normal")
#turtle colors
for indx, color in enumerate(colors_EN):
    turtles_racing[indx].color(color); arrow_turtles[indx].color(color) 
#turtle penup and speed
for turtleObjectItem in turtles_racing+arrow_turtles+lines_and_texts: 
    turtleObjectItem.penup() 
    turtleObjectItem.speed(0) 
#turtle shapes
for turtleObjectItem in turtles_racing: turtleObjectItem.shape("turtle")
for turtleObjectItem in arrow_turtles:  turtleObjectItem.shape("arrow")
#hide turtles
for turtleObjectItem in arrow_turtles+lines_and_texts: 
    turtleObjectItem.hideturtle() 
# arrow positions
arrow_turtles[0].goto(-190, 70) # R
arrow_turtles[1].goto(-190, 35) # B
arrow_turtles[2].goto(-190,  0) # G
arrow_turtles[3].goto(-190,-35) # Y
arrow_turtles[4].goto(-190,-70) # P
#start turtles
xBegin = -180
def prepStart():
    turtles_racing[0].goto(-180, 70)
    turtles_racing[1].goto(-180, 35)
    turtles_racing[2].goto(-180,  0)
    turtles_racing[3].goto(-180,-35)
    turtles_racing[4].goto(-180,-70)
    for turtleObjectItem in arrow_turtles: 
        turtleObjectItem.hideturtle()
prepStart()

#race lanes
def line(x,y,width,text):
    lines_and_texts[0].penup()
    lines_and_texts[0].goto(x,y)
    
    for i in range (15):
        lines_and_texts[0].write(text, font=fontLines)
        lines_and_texts[0].forward(width)
def raceBaan():
    line(-150,60,20,"|")
    line(-150,25,20,"|")
    line(-150,-10,20,"|")
    line(-150,-45,20,"|")
    line(-150,-80,20,"|")
raceBaan()

#numbers
lines_and_texts[0].goto(-150,90)
lines_and_texts[0].write("0       1       2       3       4       5       6       7       8       9      10     11     12     13     14")

#winner text
lines_and_texts[1].goto(xBegin,-180)
lines_and_texts[1].write("De winnaar is: "            , font=("Arial",30,"normal"))
lines_and_texts[2].goto(100,-230) # -180)
lines_and_texts[3].goto(xBegin,150)
lines_and_texts[3].color("#abcdef")
lines_and_texts[3].write(" Quit racing event with 'q'", font=("Arial",15,"bold"))

finishLine = 140
# QUIT by pressing 'q' on the keyboard
screen.quit = False
def quit():
    screen.quit = True if not screen.quit else False
screen.onkey(quit, "q")

def eventloop():
    while True: 
        i = index_random_turtle = random.choice([0, 1, 2, 3, 4]) # or randint(0,4)
        random_turtle = turtles_racing[i]
        other_turtles = turtles_racing[:] ; other_turtles.pop(i)
        arrow_turtle  = arrow_turtles[i]
        turtle_color  = colors_EN[i]
        x = random.randint(1,10)
        random_turtle.forward(x)
        if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
            arrow_turtle.showturtle()
        else:
            arrow_turtle.hideturtle()
        if random_turtle.pos()[0] >= finishLine:
            lines_and_texts[2].color(turtle_color)
            lines_and_texts[2].write(turtle_color,font=("Arial",30,"bold"))
            break

    time.sleep(3)
    lines_and_texts[2].clear()
    prepStart()
    if not screen.quit: 
        screen.ontimer(eventloop, 10) # 10ms (0.01s) (1000ms/10ms = 100 FPS)
    else: 
        screen.bye()
#:def eventloop()

eventloop()
screen.listen()
turtle.mainloop()

enter image description here

Is there a way to choose random turtles? Or is that just not possible?

The simple answer is: it is possible using the random.choice(list_of_turtles) function of the random module. It chooses a random item from a list of items and is used along with random.randint(1,10) for a random choice of the step and the turtle to move

Change in your script the code at the end to:

#finish line
finishLine = 140

dict_of_turtles = {
red     : ( arrowR , "red"   , "rood"  ),
blue    : ( arrowB , "blue"  , "blauw" ),
green   : ( arrowG , "green" , "groen" ),
yellow  : ( arrowY , "yellow", "geel"  ),
purple  : ( arrowP , "purple", "paars" ),
}
list_of_turtles = list( dict_of_turtles.keys() )

def Race():
    
    while True: 
        random_turtle = random.choice(list_of_turtles)
        other_turtles = list_of_turtles[:] ; other_turtles.remove(random_turtle)
        arrow_turtle, turtle_color, turtle_color_text  = dict_of_turtles[random_turtle]
        x = random.randint(1,10)
        random_turtle.forward(x)
        if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
            arrow_turtle.showturtle()
        else:
            arrow_turtle.hideturtle()
        if random_turtle.pos()[0] >= finishLine:
            winner2.color(turtle_color)
            winner2.write(turtle_color_text,font=("Arial",30,"bold"))
            break

    time.sleep(3)
    winner2.clear()
    arrowR.hideturtle()
    arrowB.hideturtle()
    arrowG.hideturtle()
    arrowY.hideturtle()
    arrowP.hideturtle()
    reset()
    Race()    
        
Race()

turtle.done()

end enjoy the racing and the much shorter script as there is no need to move within each loop all of the turtles.

For the sake of completeness below a reworked full version of the original code to run focused on making the code a bit more compact and 'Pythonic' along with providing the requested feature of random choice of turtle to move.

import turtle
import random
import time
screen = turtle.Screen()
colors_EN       = ['red',   'blue', 'green','yellow', 'purple']
colors_NL       = ["rood", "blauw", "groen",  "geel", "paars" ]
len_list        = len(colors_EN)
turtles_racing  = [ turtle.Turtle() for t in range(len_list)]
arrow_turtles   = [ turtle.Turtle() for t in range(len_list)]
lines_and_texts = [ turtle.Turtle() for t in range(4)]
lines_and_texts[1].color("black")
fontLines = ("Arial", 16, "normal")
#turtle colors
for indx, color in enumerate(colors_EN):
    turtles_racing[indx].color(color); arrow_turtles[indx].color(color) 
#turtle penup and speed
for turtleObjectItem in turtles_racing+arrow_turtles+lines_and_texts: 
    turtleObjectItem.penup() 
    turtleObjectItem.speed(0) 
#turtle shapes
for turtleObjectItem in turtles_racing: turtleObjectItem.shape("turtle")
for turtleObjectItem in arrow_turtles:  turtleObjectItem.shape("arrow")
#hide turtles
for turtleObjectItem in arrow_turtles+lines_and_texts: 
    turtleObjectItem.hideturtle() 
# arrow positions
arrow_turtles[0].goto(-190, 70) # R
arrow_turtles[1].goto(-190, 35) # B
arrow_turtles[2].goto(-190,  0) # G
arrow_turtles[3].goto(-190,-35) # Y
arrow_turtles[4].goto(-190,-70) # P
#start turtles
xBegin = -180
def prepStart():
    turtles_racing[0].goto(-180, 70)
    turtles_racing[1].goto(-180, 35)
    turtles_racing[2].goto(-180,  0)
    turtles_racing[3].goto(-180,-35)
    turtles_racing[4].goto(-180,-70)
    for turtleObjectItem in arrow_turtles: 
        turtleObjectItem.hideturtle()
prepStart()

#race lanes
def line(x,y,width,text):
    lines_and_texts[0].penup()
    lines_and_texts[0].goto(x,y)
    
    for i in range (15):
        lines_and_texts[0].write(text, font=fontLines)
        lines_and_texts[0].forward(width)
def raceBaan():
    line(-150,60,20,"|")
    line(-150,25,20,"|")
    line(-150,-10,20,"|")
    line(-150,-45,20,"|")
    line(-150,-80,20,"|")
raceBaan()

#numbers
lines_and_texts[0].goto(-150,90)
lines_and_texts[0].write("0       1       2       3       4       5       6       7       8       9      10     11     12     13     14")

#winner text
lines_and_texts[1].goto(xBegin,-180)
lines_and_texts[1].write("De winnaar is: "            , font=("Arial",30,"normal"))
lines_and_texts[2].goto(100,-230) # -180)
lines_and_texts[3].goto(xBegin,150)
lines_and_texts[3].color("#abcdef")
lines_and_texts[3].write(" Quit racing event with 'q'", font=("Arial",15,"bold"))

finishLine = 140
# QUIT by pressing 'q' on the keyboard
screen.quit = False
def quit():
    screen.quit = True if not screen.quit else False
screen.onkey(quit, "q")

def eventloop():
    while True: 
        i = index_random_turtle = random.choice([0, 1, 2, 3, 4]) # or randint(0,4)
        random_turtle = turtles_racing[i]
        other_turtles = turtles_racing[:] ; other_turtles.pop(i)
        arrow_turtle  = arrow_turtles[i]
        turtle_color  = colors_EN[i]
        x = random.randint(1,10)
        random_turtle.forward(x)
        if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
            arrow_turtle.showturtle()
        else:
            arrow_turtle.hideturtle()
        if random_turtle.pos()[0] >= finishLine:
            lines_and_texts[2].color(turtle_color)
            lines_and_texts[2].write(turtle_color,font=("Arial",30,"bold"))
            break

    time.sleep(3)
    lines_and_texts[2].clear()
    prepStart()
    if not screen.quit: 
        screen.ontimer(eventloop, 10) # 10ms (0.01s) (1000ms/10ms = 100 FPS)
    else: 
        screen.bye()
#:def eventloop()

eventloop()
screen.listen()
turtle.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文