在显示更新之间使用 pygame.time.wait() [英] Using pygame.time.wait() between display updates
问题描述
我目前正在 Pygame 中开发一个简单的河内塔动画,它应该显示河内塔的正确解决方案,每秒移动一个.
I am currently developing a simple Tower of Hanoi animation in Pygame, that should show the correct solution of Tower of Hanoi, moving one piece per second.
但是,在我的河内求解算法中,我试图在每次移动后更新显示并使用 pygame.time.wait();而不是更新一个动作并等待一秒钟,程序会等待移动的总数秒,然后显示所有动作一次完成的塔.
However, in my hanoi solving algorithm, I'm trying to update the display and use pygame.time.wait() after each movement; and instead of updating one movement and waiting one second, the program waits the total number of movements amount of seconds and then displays the tower with all the movements done at once.
我想知道的是我是否错误地使用了等待功能,或者在这种情况下我是否缺少任何其他有用的功能.
What I would like to know is if I am using the wait function wrongly or if there is any other useful function in this situation that I am missing.
代码如下:
def hanoi(n, origin, destination, aux):
# solves the game with n pieces
if n == 1:
positions[0] = destination
# updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
else:
hanoi(n-1, origin, aux, destination)
positions[n-1] = destination
#updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
hanoi(n-1, aux, destination, origin)
和循环:
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if running:
hanoi(numPieces, 0, 2, 1)
running = False
谢谢!
推荐答案
您需要将算法与代码的绘图方面分开.
You need to seperate your algorithm from the drawing aspect of your code.
更新代码的一种简单方法是使用协程,在递归 hanoi
函数的每一步,将控制权交还给主循环,主循环依次绘制屏幕,并每秒将控制权交还给 hanoi
协程.
A simple way to update your code would be to use a coroutine that, at every step of your recursive hanoi
function, gives the control back to the main loop, which in turn draws the screen, and gives control back to the hanoi
coroutine every second.
这是一个简单的倒计时示例:
Here's a simplified example that just counts down:
#-*- coding-utf8 -*-
import pygame
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font = pygame.freetype.SysFont(None, 30)
def hanoi(num):
# We calculated something and want to print it
# So we give control back to the main loop
yield num
# We go to the next step of the recursive algorithm
yield from hanoi(num-1) #
steps = hanoi(1000)
ticks = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
exit()
# step every second only
if not ticks or pygame.time.get_ticks() - ticks >= 1000:
ticks = pygame.time.get_ticks()
screen.fill((200, 200, 200))
# the value from the next step of the coroutine
value = str(next(steps))
# render stuff onto the screen
font.render_to(screen, (100, 100), value)
pygame.display.flip()
clock.tick(60)
在你的代码中,你应该替换
In your code, you should replace
# updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
使用 yield 位置
将控制权交还给主循环和
with yield positions
to give control back to the main loop and
hanoi(n-1, aux, destination, origin)
与
yield from hanoi(n-1, aux, destination, origin)
保持协程运行并调用
...
screen.fill((200, 200, 200))
positions = next(steps)
printBackground()
printPieces(positions)
...
在主循环中的 if
内.
(如果算法完成,它将引发您可能想要捕获的 StopIterationException
).
(If the algorithm finish, it will raise a StopIterationException
which you probably want to catch).
这篇关于在显示更新之间使用 pygame.time.wait()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!