Attempt to fix traceback on game exit with some older games.

This commit is contained in:
Storm Dragon
2025-03-14 23:10:14 -04:00
parent 2ad22ff1ae
commit be6dfdf53a
9 changed files with 483 additions and 457 deletions

50
menu.py
View File

@ -69,10 +69,10 @@ def game_menu(sounds, *options):
if event.type == pygame.KEYDOWN:
# Check for Alt modifier
mods = pygame.key.get_mods()
alt_pressed = mods & pygame.KMOD_ALT
altPressed = mods & pygame.KMOD_ALT
# Volume controls (require Alt)
if alt_pressed:
if altPressed:
if event.key == pygame.K_PAGEUP:
adjust_master_volume(0.1)
elif event.key == pygame.K_PAGEDOWN:
@ -88,7 +88,8 @@ def game_menu(sounds, *options):
# Regular menu navigation (no Alt required)
else:
if event.key == pygame.K_ESCAPE:
exit_game()
# Exit with fade if music is playing
exit_game(500 if pygame.mixer.music.get_busy() else 0)
elif event.key == pygame.K_HOME:
if currentIndex != 0:
currentIndex = 0
@ -131,13 +132,17 @@ def game_menu(sounds, *options):
time.sleep(sounds['menu-select'].get_length())
except:
pass
eval(options[currentIndex] + "()")
# Special case for exit_game with fade
if options[currentIndex] == "exit_game":
exit_game(500 if pygame.mixer.music.get_busy() else 0)
else:
eval(options[currentIndex] + "()")
except:
lastSpoken = -1
pygame.mixer.music.fadeout(500)
try:
pygame.mixer.music.fadeout(750)
time.sleep(1.0)
pygame.mixer.music.fadeout(500)
time.sleep(0.5)
except:
pass
@ -274,13 +279,34 @@ def donate():
pygame.mixer.music.pause()
webbrowser.open('https://ko-fi.com/stormux')
def exit_game():
"""Clean up and exit the game."""
def exit_game(fade=0):
"""Clean up and exit the game.
Args:
fade (int): Milliseconds to fade out music before exiting.
0 means stop immediately (default)
"""
# Get speech instance and check provider type
speech = Speech.get_instance()
if speech.provider_name == "speechd":
if speech.providerName == "speechd":
speech.close()
pygame.mixer.music.stop()
pygame.quit()
# Handle music based on fade parameter
try:
if fade > 0:
pygame.mixer.music.fadeout(fade)
# Brief pause to allow fade to start, but not complete
pygame.time.wait(min(fade // 4, 200)) # Wait up to 200ms maximum
else:
pygame.mixer.music.stop()
except Exception as e:
print(f"Warning: Could not handle music during exit: {e}")
# Clean up pygame
try:
pygame.quit()
except Exception as e:
print(f"Warning: Error during pygame.quit(): {e}")
# Exit the program
exit()