Games name their actions with default keys and controller inputs and ask Controls whether they are held, pressed or how far they are pushed. Library menus, text display, message boxes, cut scenes, power bars and pause work with a controller. controls_menu lets players rebind and set rumble; only changes from the defaults are saved in config.ini. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
693 lines
27 KiB
Python
693 lines
27 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Menu systems for Storm Games.
|
|
|
|
Provides functionality for:
|
|
- Game menu navigation
|
|
- Instructions display
|
|
- Credits display
|
|
- Sound learning interface
|
|
- Game exit handling
|
|
"""
|
|
|
|
import pygame
|
|
import time
|
|
import webbrowser
|
|
import os
|
|
from sys import exit
|
|
from os.path import isfile
|
|
from os import listdir
|
|
from os.path import join
|
|
from inspect import isfunction
|
|
from .speech import messagebox, Speech
|
|
from .sound import adjust_master_volume, adjust_bgm_volume, adjust_sfx_volume, play_bgm
|
|
from .display import display_text
|
|
from .scoreboard import Scoreboard
|
|
from .services import PathService, ConfigService
|
|
from .i18n import _, N_, reset_language, translated_file
|
|
from .controls import translate_event
|
|
|
|
# What game_menu() says for its own items. A game's own items are spoken
|
|
# as its optionLabels say, or as they are given with underscores read as spaces.
|
|
standardItemNames = {
|
|
"play": N_("play"),
|
|
"high_scores": N_("high scores"),
|
|
"learn_sounds": N_("learn sounds"),
|
|
"instructions": N_("instructions"),
|
|
"credits": N_("credits"),
|
|
"donate": N_("donate"),
|
|
"exit_game": N_("exit game"),
|
|
}
|
|
|
|
|
|
def is_language_reset(event):
|
|
"""Shift+F12, which in every menu goes back to English."""
|
|
return event.key == pygame.K_F12 and bool(pygame.key.get_mods() & pygame.KMOD_SHIFT)
|
|
|
|
|
|
def option_label(option, optionLabels=None):
|
|
"""What game_menu says for option: the library's name for its own items, else optionLabels' label for it,
|
|
else the option with underscores read as spaces."""
|
|
if option in standardItemNames:
|
|
return _(standardItemNames[option])
|
|
if callable(optionLabels):
|
|
label = optionLabels(option)
|
|
elif optionLabels:
|
|
label = optionLabels.get(option)
|
|
else:
|
|
label = None
|
|
return label if label else option.replace("_", " ")
|
|
|
|
|
|
def instruction_menu(sounds, instruction_text, *options):
|
|
"""Display a menu with an instruction announcement at the top.
|
|
|
|
The instruction text is announced as item 0 but is not selectable.
|
|
The actual menu items start at index 1, and navigation skips the instruction.
|
|
|
|
Args:
|
|
sounds (dict): Dictionary of sound objects
|
|
instruction_text (str): The instruction/context text to announce
|
|
*options: Menu options to display
|
|
|
|
Returns:
|
|
str: Selected menu option or None if cancelled
|
|
"""
|
|
# Get speech instance
|
|
speech = Speech.get_instance()
|
|
|
|
# Create combined list with instruction at index 0
|
|
all_items = [instruction_text] + list(options)
|
|
|
|
loop = True
|
|
pygame.mixer.stop()
|
|
current_index = 0 # Start at instruction
|
|
last_spoken = -1
|
|
|
|
# Clear any pending events
|
|
pygame.event.clear()
|
|
|
|
while loop:
|
|
if current_index != last_spoken:
|
|
speech.speak(all_items[current_index])
|
|
last_spoken = current_index
|
|
|
|
event = translate_event(pygame.event.wait())
|
|
if event.type == pygame.KEYDOWN:
|
|
if is_language_reset(event):
|
|
speech.speak(reset_language())
|
|
last_spoken = current_index
|
|
elif event.key == pygame.K_ESCAPE:
|
|
return None
|
|
elif event.key in [pygame.K_DOWN, pygame.K_s]:
|
|
moved = False
|
|
if current_index == 0: # On instruction, go to first menu item
|
|
current_index = 1
|
|
moved = True
|
|
elif current_index < len(all_items) - 1: # Normal navigation
|
|
current_index += 1
|
|
moved = True
|
|
|
|
if moved:
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
elif event.key in [pygame.K_UP, pygame.K_w]:
|
|
if current_index > 1: # Can move up from menu items (but not to instruction)
|
|
current_index -= 1
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
elif event.key == pygame.K_HOME:
|
|
target_index = 1 if current_index != 1 else current_index # Go to first menu item
|
|
if target_index != current_index:
|
|
current_index = target_index
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
elif event.key == pygame.K_END and current_index != len(all_items) - 1:
|
|
current_index = len(all_items) - 1
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
elif event.key == pygame.K_RETURN:
|
|
if current_index == 0: # Can't select the instruction
|
|
continue
|
|
try:
|
|
sounds['menu-select'].play()
|
|
time.sleep(sounds['menu-select'].get_length())
|
|
except:
|
|
pass
|
|
return options[current_index - 1] # Adjust for instruction offset
|
|
elif event.type == pygame.QUIT:
|
|
return None
|
|
|
|
pygame.event.pump()
|
|
pygame.event.clear()
|
|
time.sleep(0.001)
|
|
|
|
def game_menu(sounds, playCallback=None, *customOptions, soundDescriptions=None, optionLabels=None):
|
|
"""Display and handle the main game menu with standard and custom options.
|
|
|
|
Standard menu structure:
|
|
1. Play (always first)
|
|
2. High Scores
|
|
3. Custom options (if provided)
|
|
4. Learn Sounds
|
|
5. Instructions (if available)
|
|
6. Credits (if available)
|
|
7. Donate
|
|
8. Exit
|
|
|
|
Handles navigation with:
|
|
- Up/Down arrows for selection
|
|
- Home/End for first/last option
|
|
- Enter to select
|
|
- Escape to exit
|
|
- Volume controls (with Alt modifier)
|
|
|
|
Args:
|
|
sounds (dict): Dictionary of sound objects
|
|
playCallback (function, optional): Callback function for the "play" option.
|
|
If None, "play" is returned as a string like other options.
|
|
*customOptions: Additional action identifiers to include after play but before
|
|
standard options. Underscores are spoken as spaces; the original
|
|
identifiers are used for actions and returned unchanged.
|
|
soundDescriptions (dict, optional): What each sound means, passed on to
|
|
learn_sounds. See learn_sounds for the format.
|
|
optionLabels (dict or function, optional): What to say for each custom
|
|
option, for a game that translates its menu: a dictionary from
|
|
option to label, or a function given the option that returns its
|
|
label. A function is asked each time the option is spoken, so its
|
|
labels follow a change of language. An option without a label is
|
|
spoken with underscores read as spaces. The option itself is
|
|
still what game_menu returns.
|
|
|
|
Returns:
|
|
str: Selected menu option or "exit" if user pressed escape
|
|
"""
|
|
# Get speech instance
|
|
speech = Speech.get_instance()
|
|
|
|
# Start with Play option
|
|
allOptions = ["play"]
|
|
|
|
# Add high scores option if scores exist
|
|
if Scoreboard.has_high_scores():
|
|
allOptions.append("high_scores")
|
|
|
|
# Add custom options (other menu items, etc.)
|
|
allOptions.extend(customOptions)
|
|
|
|
# Add standard options in preferred order
|
|
allOptions.append("learn_sounds")
|
|
|
|
# Check for instructions file
|
|
if os.path.isfile('files/instructions.txt'):
|
|
allOptions.append("instructions")
|
|
|
|
# Check for credits file
|
|
if os.path.isfile('files/credits.txt'):
|
|
allOptions.append("credits")
|
|
|
|
# Final options
|
|
allOptions.extend(["donate", "exit_game"])
|
|
|
|
# Track if music was previously playing
|
|
musicWasPlaying = pygame.mixer.music.get_busy()
|
|
|
|
# Only start menu music if no music is currently playing
|
|
if not musicWasPlaying:
|
|
try:
|
|
from .sound import play_bgm
|
|
play_bgm("sounds/music_menu.ogg")
|
|
except:
|
|
pass
|
|
|
|
loop = True
|
|
pygame.mixer.stop()
|
|
currentIndex = 0
|
|
lastSpoken = -1 # Track last spoken index
|
|
pygame.event.clear()
|
|
|
|
while loop:
|
|
if currentIndex != lastSpoken:
|
|
option = allOptions[currentIndex]
|
|
speech.speak(option_label(option, optionLabels))
|
|
lastSpoken = currentIndex
|
|
|
|
event = translate_event(pygame.event.wait())
|
|
if event.type == pygame.KEYDOWN:
|
|
# Check for Alt modifier
|
|
mods = pygame.key.get_mods()
|
|
altPressed = mods & pygame.KMOD_ALT
|
|
|
|
# Volume controls (require Alt)
|
|
if altPressed:
|
|
if event.key == pygame.K_PAGEUP:
|
|
adjust_master_volume(0.1)
|
|
elif event.key == pygame.K_PAGEDOWN:
|
|
adjust_master_volume(-0.1)
|
|
elif event.key == pygame.K_HOME:
|
|
adjust_bgm_volume(0.1)
|
|
elif event.key == pygame.K_END:
|
|
adjust_bgm_volume(-0.1)
|
|
elif event.key == pygame.K_INSERT:
|
|
adjust_sfx_volume(0.1)
|
|
elif event.key == pygame.K_DELETE:
|
|
adjust_sfx_volume(-0.1)
|
|
# Regular menu navigation (no Alt required)
|
|
else:
|
|
if is_language_reset(event):
|
|
speech.speak(reset_language())
|
|
lastSpoken = currentIndex
|
|
elif event.key == pygame.K_ESCAPE:
|
|
# 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
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
if allOptions[currentIndex] != "donate":
|
|
pygame.mixer.music.unpause()
|
|
elif event.key == pygame.K_END:
|
|
if currentIndex != len(allOptions) - 1:
|
|
currentIndex = len(allOptions) - 1
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
if allOptions[currentIndex] != "donate":
|
|
pygame.mixer.music.unpause()
|
|
elif event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(allOptions) - 1:
|
|
currentIndex += 1
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
if allOptions[currentIndex] != "donate":
|
|
pygame.mixer.music.unpause()
|
|
elif event.key in [pygame.K_UP, pygame.K_w] and currentIndex > 0:
|
|
currentIndex -= 1
|
|
try:
|
|
sounds['menu-move'].play()
|
|
except:
|
|
pass
|
|
if allOptions[currentIndex] != "donate":
|
|
pygame.mixer.music.unpause()
|
|
elif event.key == pygame.K_RETURN:
|
|
try:
|
|
lastSpoken = -1
|
|
try:
|
|
sounds['menu-select'].play()
|
|
time.sleep(sounds['menu-select'].get_length())
|
|
except:
|
|
pass
|
|
|
|
selectedOption = allOptions[currentIndex]
|
|
|
|
# Special case for exit_game with fade
|
|
if selectedOption == "exit_game":
|
|
exit_game(500 if pygame.mixer.music.get_busy() else 0)
|
|
# Special case for play option
|
|
elif selectedOption == "play":
|
|
if playCallback:
|
|
# If a play callback is provided, call it directly
|
|
try:
|
|
pygame.mixer.music.fadeout(500)
|
|
time.sleep(0.5)
|
|
except Exception as e:
|
|
print(f"Could not fade music: {e}")
|
|
pass
|
|
playCallback()
|
|
else:
|
|
# Otherwise return "play" to the caller
|
|
try:
|
|
pygame.mixer.music.fadeout(500)
|
|
time.sleep(0.5)
|
|
except Exception as e:
|
|
print(f"Could not fade music: {e}")
|
|
pass
|
|
return "play"
|
|
# Handle standard options directly
|
|
elif selectedOption in ["instructions", "credits", "learn_sounds", "high_scores", "donate"]:
|
|
# Pause music before calling the selected function
|
|
try:
|
|
pygame.mixer.music.pause()
|
|
except:
|
|
pass
|
|
|
|
# Handle standard options
|
|
if selectedOption == "instructions":
|
|
instructions()
|
|
elif selectedOption == "credits":
|
|
credits()
|
|
elif selectedOption == "learn_sounds":
|
|
learn_sounds(sounds, descriptions=soundDescriptions)
|
|
elif selectedOption == "high_scores":
|
|
Scoreboard.display_high_scores()
|
|
elif selectedOption == "donate":
|
|
donate()
|
|
|
|
# Unpause music after function returns
|
|
try:
|
|
# Check if music is actually paused before trying to unpause
|
|
if not pygame.mixer.music.get_busy():
|
|
pygame.mixer.music.unpause()
|
|
# If music is already playing, don't try to restart it
|
|
except:
|
|
# Only start fresh music if no music is playing at all
|
|
if not pygame.mixer.music.get_busy():
|
|
try:
|
|
from .sound import play_bgm
|
|
play_bgm("sounds/music_menu.ogg")
|
|
except:
|
|
pass
|
|
# Return custom options to the calling function
|
|
else:
|
|
lastSpoken = -1
|
|
try:
|
|
pygame.mixer.music.fadeout(500)
|
|
time.sleep(0.5)
|
|
except Exception as e:
|
|
print(f"Could not fade music: {e}")
|
|
pass
|
|
return selectedOption
|
|
except Exception as e:
|
|
print(f"Error handling menu selection: {e}")
|
|
lastSpoken = -1
|
|
try:
|
|
pygame.mixer.music.fadeout(500)
|
|
time.sleep(0.5)
|
|
except:
|
|
pass
|
|
return allOptions[currentIndex]
|
|
|
|
pygame.event.pump()
|
|
pygame.event.clear()
|
|
time.sleep(0.001)
|
|
|
|
def find_sound_description(soundKey, descriptions):
|
|
"""Find what a sound means, for the learn sounds menu.
|
|
|
|
Looks for the whole sound key first, then the filename alone, then each
|
|
of those without trailing digits, so one entry for "falling_skull" covers
|
|
"falling_skull1" and "falling_skull2". Returns None if nothing matches.
|
|
"""
|
|
if not descriptions:
|
|
return None
|
|
filename = soundKey.split('/')[-1]
|
|
for candidate in (soundKey, filename, soundKey.rstrip('0123456789'), filename.rstrip('0123456789')):
|
|
description = descriptions.get(candidate)
|
|
if description:
|
|
return description
|
|
return None
|
|
|
|
|
|
def learn_sounds(sounds, descriptions=None):
|
|
"""Interactive menu for learning game sounds.
|
|
|
|
Allows users to:
|
|
- Navigate through available sounds with up/down arrows
|
|
- Navigate between sound categories (folders) using Page Up/Page Down or Left/Right arrows
|
|
- Play selected sounds with Enter
|
|
- Return to menu with Escape
|
|
|
|
Excluded sounds:
|
|
- Files in folders named 'ambience' (at any level)
|
|
- Files in any directory starting with '.'
|
|
- Files whose filename starts with 'game-intro', 'music_menu', or '_' (regardless of directory)
|
|
|
|
Args:
|
|
sounds (dict): Dictionary of available sound objects
|
|
descriptions (dict, optional): What each sound means, keyed by sound
|
|
key ("enemies/goblin"), filename ("goblin") or either without
|
|
trailing digits ("falling_skull" for "falling_skull1"). A sound
|
|
with a description is announced as "name: description, 1 of 5";
|
|
one without is announced as before, "name, 1 of 5".
|
|
|
|
Returns:
|
|
str: "menu" if user exits with escape
|
|
"""
|
|
# Get speech instance
|
|
speech = Speech.get_instance()
|
|
|
|
# Define exclusion criteria
|
|
excludedPrefixes = ["game-intro", "music_menu", "_"]
|
|
excludedDirs = ["ambience", "."]
|
|
|
|
# Organize sounds by directory
|
|
soundsByDir = {}
|
|
|
|
# Process each sound key in the dictionary
|
|
for soundKey in sounds.keys():
|
|
# Extract the filename (part after the last '/')
|
|
filename = soundKey.split('/')[-1]
|
|
|
|
# Skip if filename has any excluded prefix
|
|
if any(filename.lower().startswith(prefix.lower()) for prefix in excludedPrefixes):
|
|
continue
|
|
|
|
# Split key into path parts
|
|
parts = soundKey.split('/')
|
|
|
|
# Skip if any part of the path is an excluded directory
|
|
if any(part.lower() == dirName.lower() or part.startswith('.') for part in parts for dirName in excludedDirs):
|
|
continue
|
|
|
|
# Determine the directory
|
|
if '/' in soundKey:
|
|
directory = soundKey.split('/')[0]
|
|
else:
|
|
directory = 'root' # Root directory sounds
|
|
|
|
# Add to sounds by directory
|
|
if directory not in soundsByDir:
|
|
soundsByDir[directory] = []
|
|
soundsByDir[directory].append(soundKey)
|
|
|
|
# Sort each directory's sounds
|
|
for directory in soundsByDir:
|
|
soundsByDir[directory].sort()
|
|
|
|
# If no sounds found, inform the user and return
|
|
if not soundsByDir:
|
|
speech.speak(_("No sounds available to learn."))
|
|
return "menu"
|
|
|
|
# Get list of directories in sorted order
|
|
directories = sorted(soundsByDir.keys())
|
|
|
|
# Start with first directory
|
|
currentDirIndex = 0
|
|
currentDir = directories[currentDirIndex]
|
|
currentSoundKeys = soundsByDir[currentDir]
|
|
currentSoundIndex = 0
|
|
|
|
# Display appropriate message based on number of directories
|
|
if len(directories) > 1:
|
|
if currentDir == 'root':
|
|
messagebox(_("Starting with root directory sounds. Use left and right arrows or page up and page down "
|
|
"to navigate categories."))
|
|
else:
|
|
messagebox(_("Starting with {category} sounds. Use left and right arrows or page up and page down "
|
|
"to navigate categories.").format(category=currentDir))
|
|
|
|
# Track last spoken to avoid repetition
|
|
lastSpoken = -1
|
|
directoryChanged = True # Flag to track if directory just changed
|
|
|
|
# Flag to track when to exit the loop
|
|
returnToMenu = False
|
|
pygame.event.clear()
|
|
|
|
while not returnToMenu:
|
|
# Announce current sound
|
|
if currentSoundIndex != lastSpoken:
|
|
totalSounds = len(currentSoundKeys)
|
|
soundName = currentSoundKeys[currentSoundIndex]
|
|
|
|
# Remove directory prefix if present
|
|
if '/' in soundName:
|
|
displayName = '/'.join(soundName.split('/')[1:])
|
|
else:
|
|
displayName = soundName
|
|
|
|
description = find_sound_description(soundName, descriptions)
|
|
if description:
|
|
# A final full stop would run into the comma before the count.
|
|
# Translators: a sound's name and what it is, as in "bell: the lap bell".
|
|
displayName = _("{sound}: {description}").format(sound=displayName,
|
|
description=description.strip().rstrip('.'))
|
|
|
|
# If directory just changed, include directory name in announcement
|
|
if directoryChanged:
|
|
if currentDir == 'root':
|
|
announcement = _("Root directory: {sound}, {number} of {total}").format(
|
|
sound=displayName, number=currentSoundIndex + 1, total=totalSounds)
|
|
else:
|
|
announcement = _("{category}: {sound}, {number} of {total}").format(
|
|
category=currentDir, sound=displayName, number=currentSoundIndex + 1, total=totalSounds)
|
|
directoryChanged = False # Reset flag after announcement
|
|
else:
|
|
announcement = _("{sound}, {number} of {total}").format(
|
|
sound=displayName, number=currentSoundIndex + 1, total=totalSounds)
|
|
|
|
speech.speak(announcement)
|
|
lastSpoken = currentSoundIndex
|
|
|
|
event = translate_event(pygame.event.wait())
|
|
if event.type == pygame.KEYDOWN:
|
|
if is_language_reset(event):
|
|
speech.speak(reset_language())
|
|
elif event.key == pygame.K_ESCAPE:
|
|
returnToMenu = True
|
|
|
|
# Sound navigation
|
|
elif event.key in [pygame.K_DOWN, pygame.K_s] and currentSoundIndex < len(currentSoundKeys) - 1:
|
|
pygame.mixer.stop()
|
|
currentSoundIndex += 1
|
|
|
|
elif event.key in [pygame.K_UP, pygame.K_w] and currentSoundIndex > 0:
|
|
pygame.mixer.stop()
|
|
currentSoundIndex -= 1
|
|
|
|
# Directory navigation
|
|
elif event.key in [pygame.K_PAGEDOWN, pygame.K_RIGHT] and currentDirIndex < len(directories) - 1:
|
|
pygame.mixer.stop()
|
|
currentDirIndex += 1
|
|
currentDir = directories[currentDirIndex]
|
|
currentSoundKeys = soundsByDir[currentDir]
|
|
currentSoundIndex = 0
|
|
directoryChanged = True # Set flag on directory change
|
|
lastSpoken = -1 # Force announcement
|
|
|
|
elif event.key in [pygame.K_PAGEUP, pygame.K_LEFT] and currentDirIndex > 0:
|
|
pygame.mixer.stop()
|
|
currentDirIndex -= 1
|
|
currentDir = directories[currentDirIndex]
|
|
currentSoundKeys = soundsByDir[currentDir]
|
|
currentSoundIndex = 0
|
|
directoryChanged = True # Set flag on directory change
|
|
lastSpoken = -1 # Force announcement
|
|
|
|
# Play sound
|
|
elif event.key == pygame.K_RETURN:
|
|
try:
|
|
soundName = currentSoundKeys[currentSoundIndex]
|
|
pygame.mixer.stop()
|
|
sounds[soundName].play()
|
|
except Exception as e:
|
|
print(f"Error playing sound: {e}")
|
|
speech.speak(_("Could not play sound."))
|
|
|
|
event = pygame.event.clear()
|
|
pygame.event.pump() # Process pygame's internal events
|
|
time.sleep(0.001)
|
|
|
|
return "menu"
|
|
|
|
def instructions():
|
|
"""Display game instructions from file.
|
|
|
|
Reads and displays instructions from 'files/instructions.txt', or from
|
|
its translation in the language being spoken, such as
|
|
'files/instructions.de.txt', when there is one.
|
|
If file is missing, displays an error message.
|
|
"""
|
|
try:
|
|
with open(translated_file('files/instructions.txt'), 'r', encoding='utf-8') as f:
|
|
info = f.readlines()
|
|
except:
|
|
info = [_("Instructions file is missing.")]
|
|
display_text(info)
|
|
|
|
def credits():
|
|
"""Display game credits from file.
|
|
|
|
Reads and displays credits from 'files/credits.txt', or from its
|
|
translation in the language being spoken, such as 'files/credits.de.txt',
|
|
when there is one. Adds game name header before displaying.
|
|
If file is missing, displays an error message.
|
|
"""
|
|
try:
|
|
with open(translated_file('files/credits.txt'), 'r', encoding='utf-8') as f:
|
|
info = f.readlines()
|
|
|
|
pathService = PathService.get_instance()
|
|
info.insert(0, pathService.gameName + "\n")
|
|
except Exception as e:
|
|
print(f"Error in credits: {e}")
|
|
info = [_("Credits file is missing.")]
|
|
|
|
display_text(info)
|
|
|
|
def donate():
|
|
"""Open the donation webpage.
|
|
|
|
Opens the donation page.
|
|
"""
|
|
webbrowser.open('https://www.paypal.com/donate/?business=stormdragon2976@gmail.com&no_recurring=0¤cy_code=USD')
|
|
messagebox(_("The donation page has been opened in your browser."))
|
|
|
|
def exit_game(fade=0):
|
|
"""Clean up and exit the game properly.
|
|
|
|
Args:
|
|
fade (int): Milliseconds to fade out music before exiting.
|
|
0 means stop immediately (default)
|
|
"""
|
|
# Force clear any pending events to prevent hanging
|
|
pygame.event.clear()
|
|
|
|
# Stop all mixer channels first
|
|
try:
|
|
pygame.mixer.stop()
|
|
except Exception as e:
|
|
print(f"Warning: Could not stop mixer channels: {e}")
|
|
|
|
# Get speech instance and handle all providers
|
|
try:
|
|
speech = Speech.get_instance()
|
|
# Try to close speech regardless of provider type
|
|
try:
|
|
speech.close()
|
|
except Exception as e:
|
|
print(f"Warning: Could not close speech: {e}")
|
|
except Exception as e:
|
|
print(f"Warning: Could not get speech instance: {e}")
|
|
|
|
# Handle music based on fade parameter
|
|
try:
|
|
if fade > 0 and pygame.mixer.music.get_busy():
|
|
pygame.mixer.music.fadeout(fade)
|
|
# Wait for fade to start but don't wait for full completion
|
|
pygame.time.wait(min(250, fade))
|
|
else:
|
|
pygame.mixer.music.stop()
|
|
except Exception as e:
|
|
print(f"Warning: Could not handle music during exit: {e}")
|
|
|
|
# Clean up pygame mixer first
|
|
try:
|
|
pygame.mixer.quit()
|
|
except Exception as e:
|
|
print(f"Warning: Error during pygame.mixer.quit(): {e}")
|
|
|
|
# Clean up pygame
|
|
try:
|
|
pygame.quit()
|
|
except Exception as e:
|
|
print(f"Warning: Error during pygame.quit(): {e}")
|
|
|
|
# Use os._exit for immediate termination
|
|
import os
|
|
os._exit(0)
|