首页 > 解决方案 > Why does Pygame NEED an event handler to function?

问题描述

It makes no sense to me why it's absolutely necessary to make a call to an event handler in order for the screen to be coloured. With the event handler called, the screen is shown green.

Take out the call to the event handler and the screen remains uncoloured - it doesn't even hang in a red state and the loop can still be seen to run continuously. So the addition of an event handler makes all the difference, but WHY?!

import pygame
from pygame.locals import *
clock = pygame.time.Clock()

def event_handler():      
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            quit()  

pygame.init()
screen = pygame.display.set_mode((200,200))

screen.fill((255,0,0)) # red screen
pygame.display.update()

while True:
    screen.fill((0,250,0)) # green screen
    pygame.display.update()
    print("running")
    event_handler()
    clock.tick(60)  # Limit the frame rate.

标签: python-3.xpygame

解决方案


推荐阅读