Pygame window not responding after a few seconds

后端 未结 3 2115
小蘑菇
小蘑菇 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

提交回复
热议问题