Search code examples
pythonpygameruntime-errorpyinstallerexe

Is pygame.quit() reliable?


I recently made a small game and then turned it into an application using pyinstaller. When running, there are two ways to close it. It's fullscreen so clicking the red x button isn't included, but basically I have this

if keys[pygame.K_ESCAPE]:
    pygame.quit()

and this

if exit_button.colliderect(cursor_rect) and click==1:
    pygame.quit()

When the exit button is clicked, the game shuts down without a problem. The cursor shows the little loading symbol for a brief moment immediately after. However, when escape is pressed, the window closes but a message pops up saying, "Failed to execute script". I am on windows 10, the game was made with pygame on pycharm, and the exe was made with pyinstaller --onefile -w game.py.

Maybe it's a bug in the code, but it works perfectly fine in the IDE and I'm doing nothing different. I can also confirm it's not missing images or media. Thanks.


Solution

  • pygame.quit() doesn't quit your program, it uninitializes all pygame modules. It's meant to be used when you want the program to continue but without pygame. Your program will crash if you use certain pygame functions after calling pygame.quit().

    If you want to terminate your program, simply let the program end naturally or call quit(). Many people like to call pygame.quit() before calling quit(), but this is actually not necessary.