Python乌龟模块.ontimer循环问题

时间:2018-11-28 22:22:04

标签: python loops turtle-graphics

def h1():
    tess.forward(70)
    tess.fillcolor("orange")
def h2():
    tess.forward(70)
    tess.fillcolor("red")

def h3():
    tess.back(140)
    tess.fillcolor("green")

timer = 1000

for _ in range(timer):
    wn.ontimer(h1,timer)
    wn.ontimer(h2, timer+1000)
    wn.ontimer(h3, timer+2000)
    timer +=3000

wn.listen()
wn.mainloop()

当我使用for循环时,该功能按预期工作。 当我尝试使用while循环时,什么也没有发生,也没有运行时错误:

while True:
    wn.ontimer(h1,timer)
    wn.ontimer(h2, timer+1000)
    wn.ontimer(h3, timer+2000)
    timer +=3000

1 个答案:

答案 0 :(得分:0)

我会说您的for循环和while循环在启动计时器方面都存在问题,我希望有更多类似的交接方式:

from turtle import Screen, Turtle

TIME = 1000  # in milliseconds

def h1():
    tess.forward(70)
    tess.fillcolor("orange")
    screen.ontimer(h2, TIME)

def h2():
    tess.forward(70)
    tess.fillcolor("red")
    screen.ontimer(h3, TIME)

def h3():
    tess.back(140)
    tess.fillcolor("green")
    screen.ontimer(h1, TIME)

screen = Screen()

tess = Turtle()

h1()

screen.mainloop()

但是您的问题中没有足够的代码来理解全局。