Pygame window not responding after a few seconds

后端 未结 3 2070
小蘑菇
小蘑菇 2020-11-22 09:35

This simple piece of code crashes (the window is not responding) after a few seconds (around 5).

import pygame
from pygame.locals import *

pygame.init()
sc         


        
相关标签:
3条回答
  • 2020-11-22 10:02

    The window does not respond (freeze), because you do not handle the events. You have to handle the events by either pygame.event.pump() or pygame.event.get(), to keep the window responding.

    See the documentation of pygame.event.pump():

    For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

    Add an event loop, for instance:

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        # [...]
    

    Alternatively just pump the events:

    while True:
        pygame.event.pump()
    
        # [...]
    

    Minimal example: repl.it/@Rabbid76/PyGame-MinimalApplicationLoop

    0 讨论(0)
  • 2020-11-22 10:06

    You need to regularly make a call to one of four functions in the pygame.event module in order for pygame to internally interact with your OS. Otherwise the OS will think your game has crashed. So make sure you call one of these:

    • pygame.event.get() returns a list of all events currently in the event queue.
    • pygame.event.poll() returns a single event from the event queue or pygame.NOEVENT if the queue is empty.
    • pygame.event.wait() returns a single event from the event queue or waits until an event can be returned.
    • pygame.event.pump() allows pygame to handle internal actions. Useful when you don't want to handle events from the event queue.
    0 讨论(0)
  • 2020-11-22 10:11

    Call pygame.event.get() at the beginning of the while loop.

    0 讨论(0)
提交回复
热议问题