乌龟图形随机游走 pause/resume 函数
Random walk on turtle graphics pause/resume function
菜鸟来啦!
我修改了我正在参加的在线课程中的随机游走作业,并且我正在尝试创建一个函数来暂停程序,并在再次调用时恢复它。
它目前暂停了,但我似乎无法让它恢复;或者准确地说,一旦暂停我就无法得到任何反应——就好像屏幕停止监听事件一样。
我已经试过了:
- 为恢复创建一个单独的函数 - 如果我在暂停之前调用该函数,但在暂停之后我无法调用它,该函数就可以工作
- 我可以为 if go 条件添加一个 else 子句,其中我将变量声明为 screen.textinput 并根据输入程序将继续或结束,但我希望它像暂停一样简单。
- 我尝试在 if go 中断后添加一个 screen.listen() 调用作为 else 子句,但它似乎不起作用...
- 我尝试将 go=False 设置为默认值,然后通过调用函数将其设置为 True 来启动它,但这也不起作用。
P.S:
我是编程新手,所以如果您有进一步的评论和建议,我将不胜感激。
非常感谢!
在相关行中附上我的代码注释:
from turtle import Turtle, colormode, Screen
from random import choice, randint
colormode(255)
tim = Turtle('circle')
tim.speed('fastest')
tim.width(10)
screen = Screen()
go = True # Boolean to initialize line 31
def pause(): # Currently it pauses, but doesn't resume.
"""Pause the program by setting go to False, and resume it if it was paused"""
global go
if go:
go = False
else:
go = True
def random_color():
"""Return a random color"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
generated_color = (r, g, b)
return generated_color
screen.listen() # Starts listening for events
screen.onkey(fun=pause, key='p') # Event - when the 'p' key is pressed, the 'pause' function is called.
while True: # Outer loop to prevent the code from ending when 'go' is set to False by pressing 'p' the first time
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
问题在于,在您的实现中,当 go
为 False
时,没有函数 运行 获取键盘事件(因为您不是 运行主事件循环)。一种不太优雅的修复方法是在 go == False
时添加一个虚拟操作。将您的最终循环更改为:
while True: # Outer loop to prevent the code from ending when 'go' is set to False by pressing 'p' the first time
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
else:
tim.setheading(0)
它会起作用。
另一种(更好的)方法是使用事件来移动乌龟。在下面的变体中,我使用 screen.ontimer
方法在给定时间后调用 move
函数。在每次调用 move
之后,它会在一段时间后(在示例中为 50 毫秒)将自身重新安排到 运行。这样,你总是有一个事件循环运行宁和接收键盘事件。
from turtle import Turtle, colormode, Screen
from random import choice, randint
colormode(255)
tim = Turtle('circle')
tim.speed('fastest')
tim.width(10)
screen = Screen()
go = True # Boolean to initialize line 31
def pause(): # Currently it pauses, but doesn't resume.
"""Pause the program by setting go to False, and resume it if it was paused"""
global go
if go:
go = False
else:
go = True
def random_color():
"""Return a random color"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
generated_color = (r, g, b)
return generated_color
def move():
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
screen.ontimer(fun=move, t=50) # Move every 0.05 s
screen.listen() # Starts listening for events
screen.onkey(fun=pause, key='p') # Event - when the 'p' key is pressed, the 'pause' function is called.
screen.ontimer(fun=move, t=0) # Schedule the first movement
screen.mainloop()
菜鸟来啦! 我修改了我正在参加的在线课程中的随机游走作业,并且我正在尝试创建一个函数来暂停程序,并在再次调用时恢复它。 它目前暂停了,但我似乎无法让它恢复;或者准确地说,一旦暂停我就无法得到任何反应——就好像屏幕停止监听事件一样。
我已经试过了:
- 为恢复创建一个单独的函数 - 如果我在暂停之前调用该函数,但在暂停之后我无法调用它,该函数就可以工作
- 我可以为 if go 条件添加一个 else 子句,其中我将变量声明为 screen.textinput 并根据输入程序将继续或结束,但我希望它像暂停一样简单。
- 我尝试在 if go 中断后添加一个 screen.listen() 调用作为 else 子句,但它似乎不起作用...
- 我尝试将 go=False 设置为默认值,然后通过调用函数将其设置为 True 来启动它,但这也不起作用。
P.S: 我是编程新手,所以如果您有进一步的评论和建议,我将不胜感激。
非常感谢!
在相关行中附上我的代码注释:
from turtle import Turtle, colormode, Screen
from random import choice, randint
colormode(255)
tim = Turtle('circle')
tim.speed('fastest')
tim.width(10)
screen = Screen()
go = True # Boolean to initialize line 31
def pause(): # Currently it pauses, but doesn't resume.
"""Pause the program by setting go to False, and resume it if it was paused"""
global go
if go:
go = False
else:
go = True
def random_color():
"""Return a random color"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
generated_color = (r, g, b)
return generated_color
screen.listen() # Starts listening for events
screen.onkey(fun=pause, key='p') # Event - when the 'p' key is pressed, the 'pause' function is called.
while True: # Outer loop to prevent the code from ending when 'go' is set to False by pressing 'p' the first time
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
问题在于,在您的实现中,当 go
为 False
时,没有函数 运行 获取键盘事件(因为您不是 运行主事件循环)。一种不太优雅的修复方法是在 go == False
时添加一个虚拟操作。将您的最终循环更改为:
while True: # Outer loop to prevent the code from ending when 'go' is set to False by pressing 'p' the first time
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
else:
tim.setheading(0)
它会起作用。
另一种(更好的)方法是使用事件来移动乌龟。在下面的变体中,我使用 screen.ontimer
方法在给定时间后调用 move
函数。在每次调用 move
之后,它会在一段时间后(在示例中为 50 毫秒)将自身重新安排到 运行。这样,你总是有一个事件循环运行宁和接收键盘事件。
from turtle import Turtle, colormode, Screen
from random import choice, randint
colormode(255)
tim = Turtle('circle')
tim.speed('fastest')
tim.width(10)
screen = Screen()
go = True # Boolean to initialize line 31
def pause(): # Currently it pauses, but doesn't resume.
"""Pause the program by setting go to False, and resume it if it was paused"""
global go
if go:
go = False
else:
go = True
def random_color():
"""Return a random color"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
generated_color = (r, g, b)
return generated_color
def move():
if go: # As default is True, after pausing can't restart it...
if 200 > tim.xcor() > -200 and 200 > tim.ycor() > -200:
tim.color(random_color())
tim.forward(choice(range(50)))
tim.setheading(choice(range(360)))
else:
tim.goto(0, 0)
screen.ontimer(fun=move, t=50) # Move every 0.05 s
screen.listen() # Starts listening for events
screen.onkey(fun=pause, key='p') # Event - when the 'p' key is pressed, the 'pause' function is called.
screen.ontimer(fun=move, t=0) # Schedule the first movement
screen.mainloop()