Reputation: 75
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.
Upvotes: 2
Views: 390
Reputation: 59
Just want to add a little something.
As you'll see in most pygame projects (source), we use both pygame.quit()
and sys.exit()
.
Assuming that your intention is to shut down both the pygame program and the Python script it's running from, you'll use something like the below code.
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
First pygame.quit()
is called to stop all the pygame modules that activate when you run pygame.init()
, followed by sys.exit()
which stops the python script.
This is how I've been doing it for several projects that I've created and I've never had any errors while exiting the program, whether it be manually or through the code.
Upvotes: 1
Reputation: 9766
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.
Upvotes: 5