why is the exit window button work but the exit button in the game does not work?

The main loop runs as long as playing is True.

playing = True
while playing:
    # [...]

When the pygame.QUIT event is handled, playing is set False the main loop condition is fails:

if event.type == pygame.QUIT:
    playing = False
    # [...]

Note, pygame.quit() doesn’t terminate the loop, but it uninitialize all pygame modules, which will cause an exception in the following, if it is done in the middle of the application.

If you want to quit the application by the keypad enter key pygame.K_KP_ENTER, the you’ve to do the same when the pygame.KEYDOWN event is handled:

if event.type == pygame.KEYDOWN:
    if event.key==pygame.K_KP_ENTER:
        playing = False

Or you’ve to send a pygame.QUIT event by pygame.event.post():

if event.type == pygame.KEYDOWN:
    if event.key==pygame.K_KP_ENTER:
        pygame.event.post(pygame.event.Event(pygame.QUIT))

Leave a Comment