在显示更新之间使用pygame

在显示更新之间使用pygame

本文介绍了在显示更新之间使用pygame.time.wait()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在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.

我想知道的是我是否错误使用了wait函数,或者在这种情况下是否缺少其他有用的函数.

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 positions一起将控制权交还给主循环,并且

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:13