Why does pygame.display.update() not work if an input is directly followed after it?

余生长醉 提交于 2021-02-04 08:01:10

问题


In my program, I'm trying to have an input right after the display screen is updated in pygame. I noticed for some reason the screen is only updated AFTER the user enters in an input, although the input function is after pygame.display.update(). Why does this happen, and how can the code be fixed?

# python 3.6.5
# pygame 1.9.3
def main():
    pygame.draw.rect(screen,[235,235,235],(200,200,200,200))
    pygame.display.update()
    input('')
    # input is shown first instead of the rectangle

I expected the rectangle to draw first and then the input, but the input occured first and then the screen was updated


回答1:


Call pygame.event.pump() after pygame.display.update() and before input(''):

def main():
    pygame.draw.rect(screen,[235,235,235],(200,200,200,200))
    pygame.display.update()

    pygame.event.pump()

    input('')

At some OS, pygame.display.update() respectively pygame.display.flip() doesn't update the display directly. It just invalidates the display and notifies the system to update the display. Actually the display is updated when the events are handled.
The events are either handled by either pygame.event.pump() or pygame.event.get() (Which is used in event loops, but would do the job as well). Note this instruction do not only handle the IO or user events, they handle a bunch of internal events too, which are required to run run the system.

Not all implementations on all OS behave the same. At some OS it is sufficient to call pygame.display.update(), that is the reason that not everyone at every system can reproduce the issue. But in this case it is never wrong to call pygame.event.pump().



来源:https://stackoverflow.com/questions/58794093/why-does-pygame-display-update-not-work-if-an-input-is-directly-followed-after

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!