Pygame:display.update() 直到时钟延迟后才更新 [英] Pygame: display.update() does not update until after clock delay

查看:48
本文介绍了Pygame:display.update() 直到时钟延迟后才更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pygame 的 time.Clock 以较低的 FPS 运行我的游戏,我注意到我的输入似乎需要额外的一帧才能生效.我做了一些调试并意识到这不是 pygame.event.get() 的问题,而是 pygame.display.update() 的问题.我写了一个简单的程序来演示这个问题:(程序说明在代码片段下方)

I am using pygame's time.Clock to run my game at a lower FPS and I noticed that my input seemed to take one extra frame to take effect. I did some debugging and realized that it was not a problem with pygame.event.get() but rather with pygame.display.update(). I've written a simple program to demonstrate the issue: (Explanation of program is below the code snippet)

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
colour = 1
key_press = False

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            screen.fill((0,255,0))
            key_press = True
            print("A key was pressed.")

    if not key_press:
        screen.fill((colour*255, colour*255, colour*255))
        colour = not colour
    else:
        key_press = False

    pygame.display.update()
    clock.tick(0.5)

程序每两秒在黑白之间闪烁一次屏幕,每当按下任何键时,屏幕将变为绿色,并且A key was press" 将打印到控制台.但是,当我运行它时,文本会在正确的时间打印出来,但直到下一帧屏幕才会变为绿色.这让我相信 pygame.display.update() 在我调用函数时不会更新屏幕.

The program flashes the screen between black and white every two seconds, and whenever any key is pressed the screen will change to green and "A key was pressed" will be printed to the console. However, when I run it, the text is printed at the correct time but the screen does not change to green until the next frame. This leads me to believe that pygame.display.update() is not updating the screen when I am calling the function.

问题似乎出在调用 pygame.display.update() 后立即延迟.如果更改时钟的 FPS,问题仍然存在.如果您使用 time.wait()pygame.time.delay() 而不是 Clock.tick() 进行延迟,也会发生这种情况.

The problem seems to be with the delay immediately after calling pygame.display.update(). If you change the FPS of the clock, the problem still persists. This also occurs if you delay with time.wait() or pygame.time.delay() instead of Clock.tick().

我注意到的另一个有趣的事情是,如果您颠倒我示例中最后两行代码的顺序(pygame.display.update()clock.tick(0.5)),您可能希望屏幕在按下一个键而不是一个键后改变两个时钟周期.但是,如果我这样做,我会得到与它们处于其他顺序时完全相同的效果.

Another interesting thing that I've noticed is that if you reverse the order of the last two lines of code in my example (pygame.display.update() and clock.tick(0.5)), you might expect that the screen will change two clock cycles after a key is pressed instead of one. However, if I do this I get exactly the same effect as when they were in the other order.

我不确定这是一个错误还是我刚刚错过的一些微妙的事情.我在 macOS 上运行 Python 3.6.2 并使用 Pygame v1.9.3.您能提供的任何帮助将不胜感激!

I'm not sure if this is a bug or if something subtle is going on here that I just missed. I'm running Python 3.6.2 and using Pygame v1.9.3 on a macOS. Any help you can give would be greatly appreciated!

推荐答案

我能够通过调用 pygame.event.pump() 在一个程序中修复这个故障(它不会清除事件队列,因此没有事件处理丢失)在 pygame.display.update() 调用和 clock.tick(FPS) 调用之间.基本上,我的主要游戏循环结束于:

I was able to fix this glitch in one program by calling pygame.event.pump() (which does not clear the event queue, so no event-handling is lost) between the pygame.display.update() call and the clock.tick(FPS) call. Basically, my main game loop ended with:

pygame.display.update()
pygame.event.pump()
clock_object.tick(FPS_CONSTANT)

这篇关于Pygame:display.update() 直到时钟延迟后才更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆