Integrate scoreboard with game_menu()

This commit is contained in:
Storm Dragon 2025-03-15 17:07:51 -04:00
parent fe772cbb1e
commit 468c663cc1
2 changed files with 74 additions and 13 deletions

@ -51,7 +51,7 @@ from .input import get_input, check_for_exit, pause_game
from .display import display_text, initialize_gui
# Import menu functions
from .menu import game_menu, learn_sounds, instructions, credits, donate, exit_game
from .menu import game_menu, learn_sounds, instructions, credits, donate, exit_game, high_scores, has_high_scores
# Import utility functions and Game class
from .utils import (
@ -109,15 +109,15 @@ __all__ = [
'display_text', 'initialize_gui',
# Menu
'game_menu', 'learn_sounds', 'instructions', 'credits', 'donate', 'exit_game',
'game_menu', 'learn_sounds', 'instructions', 'credits', 'donate', 'exit_game', 'high_scores', 'has_high_scores',
# Game class
'Game',
# Utils
'check_for_updates', 'get_version_tuple', 'check_compatibility',
'sanitize_filename', 'lerp', 'smooth_step', 'distance_2d',
'x_powerbar', 'y_powerbar', 'generate_tone',
'check_for_updates', 'get_version_tuple', 'check_compatibility',
'sanitize_filename', 'lerp', 'smooth_step', 'distance_2d',
'x_powerbar', 'y_powerbar', 'generate_tone',
# Re-exported functions from pygame, math, random
'get_ticks', 'delay', 'wait',

77
menu.py

@ -29,12 +29,13 @@ def game_menu(sounds, playCallback=None, *customOptions):
Standard menu structure:
1. Play (always first)
2. Custom options (high scores, etc.)
3. Learn Sounds
4. Instructions (if available)
5. Credits (if available)
6. Donate
7. Exit
2. High Scores (if scores exist)
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
@ -58,7 +59,11 @@ def game_menu(sounds, playCallback=None, *customOptions):
# Start with Play option
allOptions = ["play"]
# Add custom options (high scores, etc.)
# Add high scores option if scores exist
if has_high_scores():
allOptions.append("high_scores")
# Add custom options (other menu items, etc.)
allOptions.extend(customOptions)
# Add standard options in preferred order
@ -183,7 +188,7 @@ def game_menu(sounds, playCallback=None, *customOptions):
# Otherwise return "play" to the caller
return "play"
# Handle standard options directly
elif selectedOption in ["instructions", "credits", "learn_sounds", "donate"]:
elif selectedOption in ["instructions", "credits", "learn_sounds", "high_scores", "donate"]:
# Pause music before calling the selected function
try:
pygame.mixer.music.pause()
@ -197,6 +202,8 @@ def game_menu(sounds, playCallback=None, *customOptions):
credits()
elif selectedOption == "learn_sounds":
learn_sounds(sounds)
elif selectedOption == "high_scores":
high_scores()
elif selectedOption == "donate":
donate()
@ -300,6 +307,60 @@ def learn_sounds(sounds):
return "menu"
def has_high_scores():
"""Check if the current game has any high scores.
Returns:
bool: True if at least one high score exists, False otherwise
"""
try:
from .scoreboard import Scoreboard
board = Scoreboard()
scores = board.get_high_scores()
# Check if any score is greater than zero
return any(score['score'] > 0 for score in scores)
except Exception as e:
print(f"Error checking high scores: {e}")
return False
def high_scores():
"""Display high scores for the current game.
Reads the high scores from Scoreboard and displays them in order.
Shows the game name at the top followed by the available scores.
"""
try:
# Get PathService to access game name
pathService = PathService.get_instance()
gameName = pathService.gameName
# Create Scoreboard instance and get high scores
from .scoreboard import Scoreboard
board = Scoreboard()
scores = board.get_high_scores()
# Filter out scores with zero points
validScores = [score for score in scores if score['score'] > 0]
# Prepare the lines to display
lines = [f"High Scores for {gameName}:"]
# Add scores to the display list
if validScores:
for i, entry in enumerate(validScores, 1):
scoreStr = f"{i}. {entry['name']}: {entry['score']}"
lines.append(scoreStr)
else:
lines.append("No high scores yet.")
# Display the high scores
display_text(lines)
except Exception as e:
print(f"Error displaying high scores: {e}")
info = ["Could not display high scores."]
display_text(info)
def instructions():
"""Display game instructions from file.