Oops, I accidently committed my scrp file for menu instead of the actual file.
This commit is contained in:
parent
1dc0ac2a7f
commit
91f39aad88
153
menu.py
153
menu.py
@ -244,5 +244,154 @@ def game_menu(sounds, playCallback=None, *customOptions):
|
||||
event = pygame.event.clear()
|
||||
time.sleep(0.001)
|
||||
|
||||
# Rest of menu.py functions here...
|
||||
# (learn_sounds, instructions, credits, donate, exit_game)
|
||||
def learn_sounds(sounds):
|
||||
"""Interactive menu for learning game sounds.
|
||||
|
||||
Allows users to:
|
||||
- Navigate through available sounds
|
||||
- Play selected sounds
|
||||
- Return to menu with escape key
|
||||
|
||||
Args:
|
||||
sounds (dict): Dictionary of available sound objects
|
||||
|
||||
Returns:
|
||||
str: "menu" if user exits with escape
|
||||
"""
|
||||
# Get speech instance
|
||||
speech = Speech.get_instance()
|
||||
|
||||
currentIndex = 0
|
||||
|
||||
# Get list of available sounds, excluding special sounds
|
||||
soundFiles = [f for f in listdir("sounds/")
|
||||
if isfile(join("sounds/", f))
|
||||
and (f.split('.')[1].lower() in ["ogg", "wav"])
|
||||
and (f.split('.')[0].lower() not in ["game-intro", "music_menu"])
|
||||
and (not f.lower().startswith("_"))]
|
||||
|
||||
# Track last spoken index to avoid repetition
|
||||
lastSpoken = -1
|
||||
|
||||
# Flag to track when to exit the loop
|
||||
returnToMenu = False
|
||||
|
||||
while not returnToMenu:
|
||||
if currentIndex != lastSpoken:
|
||||
speech.speak(soundFiles[currentIndex][:-4])
|
||||
lastSpoken = currentIndex
|
||||
|
||||
event = pygame.event.wait()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
returnToMenu = True
|
||||
|
||||
if event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(soundFiles) - 1:
|
||||
pygame.mixer.stop()
|
||||
currentIndex += 1
|
||||
|
||||
if event.key in [pygame.K_UP, pygame.K_w] and currentIndex > 0:
|
||||
pygame.mixer.stop()
|
||||
currentIndex -= 1
|
||||
|
||||
if event.key == pygame.K_RETURN:
|
||||
try:
|
||||
soundName = soundFiles[currentIndex][:-4]
|
||||
pygame.mixer.stop()
|
||||
sounds[soundName].play()
|
||||
except:
|
||||
lastSpoken = -1
|
||||
speech.speak("Could not play sound.")
|
||||
|
||||
event = pygame.event.clear()
|
||||
time.sleep(0.001)
|
||||
|
||||
return "menu"
|
||||
|
||||
def instructions():
|
||||
"""Display game instructions from file.
|
||||
|
||||
Reads and displays instructions from 'files/instructions.txt'.
|
||||
If file is missing, displays an error message.
|
||||
"""
|
||||
try:
|
||||
with open('files/instructions.txt', 'r') 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'.
|
||||
Adds game name header before displaying.
|
||||
If file is missing, displays an error message.
|
||||
"""
|
||||
try:
|
||||
with open('files/credits.txt', 'r') 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 Ko-fi donation page.
|
||||
"""
|
||||
webbrowser.open('https://ko-fi.com/stormux')
|
||||
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
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user