Where's the error in this simple pygame program? (sys.exit() not working) [duplicate]

StackOverflow https://stackoverflow.com/questions/12288234

  •  30-06-2021
  •  | 
  •  

Question

Possible Duplicate:
Pygame programs hanging on exit

Ran this simple program:

import pygame, sys

pygame.init()

screen = pygame.display.set_mode((800,600))

while True:
    #process input
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()

Got this simple message in IDLE:

Traceback (most recent call last):
  File "C:/Users/Aerovistae/Desktop/GD_in_class", line 12, in <module>
    sys.exit()
SystemExit

And the program stops responding and has to be Ctrl+Alt+Del'd. I can't see where I'm going wrong here, this is as basic as I can get. I was following what a professor did in lecture, I don't see any difference between my code and his. Can anyone suggest what might cause the problem?

Was it helpful?

Solution

Proper way to quit pygame is to call

pygame.quit()

after the main run loop.

Read From the pygame documentation: http://www.pygame.org/wiki/FrequentlyAskedQuestions#In IDLE why does the Pygame window not close correctly?

Just exit the main run loop instead of sys.exit() and end the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top