Search code examples
pythonpygameexit

pygame.quit(), quit(), or run = False?


Lets says I just have a normal game loop using pygame.

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

pygame.quit()

Where run = False is, should I use run = False and pygame.quit() at the end? Should I just put pygame.quit() where run = False is? Should I use quit() without pygame in front of it at all? Thanks.


Solution

  • I recommend to do it exactly as you do it in the question.

    pygame.quit() uninitialize all pygame modules. Any further call to a pygame module (except pygame.init()) will cause an exception. To terminate a pygame application correctly, pygame.quit() has to be the called at the end. If you do pygame.quit() in the main application loop, then the application will crash if you do something after it (e.g. pygame.disaply.update()).