我正在使用turtle用python创建一个游戏,但是我无法控制循环中乌龟的速度,因为乌龟的速度是0。它应该像闪存一样运行,但它是以正常速度运行的
import turtle
c=turtle.Screen()
a=turtle.Turtle()
a.speed(0)
b=True
def ch( a , d):
global b
b = False
while b:
a.fd(1)
c.onclick(ch)
c.mainloop()发布于 2017-11-10 12:24:27
speed(0)只能稍微提高动画的速度。尝试使用c.tracer(0, 0)
这将完全禁用所有动画,并应导致它更多的速度。不过,要刷新屏幕,您需要调用c.update()
发布于 2017-11-10 15:42:39
首先,你的代码结构不正确。您不需要在循环中调用onclick(),它只需设置一个处理程序函数,因此只需调用一次。此外,mainloop()应该运行事件,而不是在事件结束后调用。
我不相信你会从这段代码中获得更多的速度,除非你增加前进的距离。简单地递增到fd(3)将产生明显的不同。我对你的代码进行了修改:
from turtle import Turtle, Screen
def click_handler(x, y):
global flag
flag = False
def turtle_forward():
if flag:
turtle.forward(3)
screen.ontimer(turtle_forward, 0)
flag = True
screen = Screen()
screen.onclick(click_handler)
turtle = Turtle()
turtle.speed('fastest')
turtle_forward()
screen.mainloop()https://stackoverflow.com/questions/47204447
复制相似问题