Finally, a working scoreboard!
This commit is contained in:
parent
23aea6badf
commit
27765e62bc
2
menu.py
2
menu.py
@ -204,7 +204,7 @@ def game_menu(sounds, playCallback=None, *customOptions):
|
||||
elif selectedOption == "learn_sounds":
|
||||
learn_sounds(sounds)
|
||||
elif selectedOption == "high_scores":
|
||||
Scoreboard.displayHigh_scores()
|
||||
Scoreboard.display_high_scores()
|
||||
elif selectedOption == "donate":
|
||||
donate()
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Scoreboard handling for Storm Games.
|
||||
"""
|
||||
Modified Scoreboard class with integrated fixes.
|
||||
|
||||
Provides functionality for:
|
||||
- Tracking high scores with player names
|
||||
- Saving/loading high scores from configuration
|
||||
This code should replace the existing Scoreboard class in scoreboard.py.
|
||||
The modifications ensure proper path handling and config operations.
|
||||
"""
|
||||
|
||||
import time
|
||||
@ -27,6 +27,9 @@ class Scoreboard:
|
||||
configService (ConfigService): Config service (default: global instance)
|
||||
speech (Speech): Speech system (default: global instance)
|
||||
"""
|
||||
# Ensure services are properly initialized
|
||||
self._ensure_services()
|
||||
|
||||
self.configService = configService or ConfigService.get_instance()
|
||||
self.speech = speech or Speech.get_instance()
|
||||
self.currentScore = score
|
||||
@ -72,6 +75,37 @@ class Scoreboard:
|
||||
|
||||
# Sort high scores by score value in descending order
|
||||
self.highScores.sort(key=lambda x: x['score'], reverse=True)
|
||||
|
||||
def _ensure_services(self):
|
||||
"""Ensure PathService and ConfigService are properly initialized."""
|
||||
# Get PathService and make sure it has a game name
|
||||
pathService = PathService.get_instance()
|
||||
|
||||
# If no game name yet, try to get from pygame window title
|
||||
if not pathService.gameName:
|
||||
try:
|
||||
import pygame
|
||||
if pygame.display.get_caption()[0]:
|
||||
pathService.gameName = pygame.display.get_caption()[0]
|
||||
except:
|
||||
pass
|
||||
|
||||
# Initialize path service if we have a game name but no paths set up
|
||||
if pathService.gameName and not pathService.gamePath:
|
||||
pathService.initialize(pathService.gameName)
|
||||
|
||||
# Get ConfigService and connect to PathService
|
||||
configService = ConfigService.get_instance()
|
||||
if not hasattr(configService, 'pathService') or not configService.pathService:
|
||||
if pathService.gameName:
|
||||
configService.set_game_info(pathService.gameName, pathService)
|
||||
|
||||
# Ensure the game directory exists
|
||||
if pathService.gamePath and not os.path.exists(pathService.gamePath):
|
||||
try:
|
||||
os.makedirs(pathService.gamePath)
|
||||
except Exception as e:
|
||||
print(f"Error creating game directory: {e}")
|
||||
|
||||
def get_score(self):
|
||||
"""Get current score."""
|
||||
@ -121,6 +155,9 @@ class Scoreboard:
|
||||
Returns:
|
||||
bool: True if score was added, False if not
|
||||
"""
|
||||
# Ensure services are properly set up
|
||||
self._ensure_services()
|
||||
|
||||
position = self.check_high_score()
|
||||
if position is None:
|
||||
return False
|
||||
@ -153,6 +190,7 @@ class Scoreboard:
|
||||
try:
|
||||
self.configService.write_local_config()
|
||||
except Exception as e:
|
||||
print(f"Error writing config with configService: {e}")
|
||||
# Fallback to old method if configService fails
|
||||
for i, entry in enumerate(self.highScores):
|
||||
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
|
||||
@ -160,6 +198,7 @@ class Scoreboard:
|
||||
write_config()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error writing high scores: {e}")
|
||||
# If all else fails, try direct old method
|
||||
for i, entry in enumerate(self.highScores):
|
||||
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
|
||||
@ -189,8 +228,18 @@ class Scoreboard:
|
||||
pathService = PathService.get_instance()
|
||||
gameName = pathService.gameName
|
||||
|
||||
# If no game name, try to get from window title
|
||||
if not gameName:
|
||||
try:
|
||||
import pygame
|
||||
if pygame.display.get_caption()[0]:
|
||||
gameName = pygame.display.get_caption()[0]
|
||||
pathService.gameName = gameName
|
||||
except:
|
||||
pass
|
||||
|
||||
# Ensure path service is properly initialized
|
||||
if not pathService.gamePath:
|
||||
if gameName and not pathService.gamePath:
|
||||
pathService.initialize(gameName)
|
||||
|
||||
# Get the config file path
|
||||
@ -231,8 +280,18 @@ class Scoreboard:
|
||||
pathService = PathService.get_instance()
|
||||
gameName = pathService.gameName
|
||||
|
||||
# If no game name, try to get from window title
|
||||
if not gameName:
|
||||
try:
|
||||
import pygame
|
||||
if pygame.display.get_caption()[0]:
|
||||
gameName = pygame.display.get_caption()[0]
|
||||
pathService.gameName = gameName
|
||||
except:
|
||||
pass
|
||||
|
||||
# Ensure path service is properly initialized
|
||||
if not pathService.gamePath:
|
||||
if gameName and not pathService.gamePath:
|
||||
pathService.initialize(gameName)
|
||||
|
||||
# Ensure config service is properly connected to path service
|
||||
@ -268,3 +327,6 @@ class Scoreboard:
|
||||
print(f"Error displaying high scores: {e}")
|
||||
info = ["Could not display high scores."]
|
||||
display_text(info)
|
||||
|
||||
# For backward compatibility with older code that might call displayHigh_scores
|
||||
displayHigh_scores = display_high_scores
|
||||
|
Loading…
x
Reference in New Issue
Block a user