Compare commits

..

No commits in common. "4a15f951f0ac31a2020217f764ec66076b9fd77a" and "7cbbc64d27fe533e27332371a332ffb298a82d3c" have entirely different histories.

View File

@ -54,94 +54,102 @@ sfxVolume = 1.0 # Default sound effects volume
masterVolume = 1.0 # Default master volume masterVolume = 1.0 # Default master volume
class Scoreboard: class Scoreboard:
"""Handles high score tracking with player names.""" """Handles score tracking and top 10 high scores for games.
def __init__(self, score=0): This class manages the scoring system including:
"""Initialize scoreboard with optional starting score.""" - Score tracking
- High score storage
- Score updates and persistence
"""
def __init__(self, starting_score=0):
"""Initialize a new scoreboard with optional starting score.
Args:
starting_score (int): Initial score value (default: 0)
"""
read_config() read_config()
self.currentScore = score
self.highScores = []
try: try:
localConfig.add_section("scoreboard") localConfig.add_section("scoreboard")
except: except:
pass pass
self.score = starting_score
# Load existing high scores self.old_scores = []
for i in range(1, 11): for i in range(1, 11):
try: try:
score = localConfig.getint("scoreboard", f"score_{i}") self.old_scores.insert(i - 1, localConfig.getint("scoreboard", str(i)))
name = localConfig.get("scoreboard", f"name_{i}")
self.highScores.append({
'name': name,
'score': score
})
except: except:
self.highScores.append({ self.old_scores.insert(i - 1, 0)
'name': "Player", for i in range(1, 11):
'score': 0 if self.old_scores[i - 1] is None:
}) self.old_scores[i - 1] = 0
def get_score(self): def __del__(self):
"""Get current score.""" """Save scores when object is destroyed."""
return self.currentScore self.update_scores()
try:
def get_high_scores(self): write_config()
"""Get list of high scores.""" except:
return self.highScores pass
def decrease_score(self, points=1): def decrease_score(self, points=1):
"""Decrease the current score.""" """Decrease the current score.
self.currentScore -= points
def increase_score(self, points=1): Args:
"""Increase the current score.""" points (int): Number of points to decrease (default: 1)
self.currentScore += points """
self.score -= points
def check_high_score(self): def get_high_score(self, position=1):
"""Check if current score qualifies as a high score. """Get a high score at specified position.
Args:
position (int): Position in high score list (1-10, default: 1)
Returns: Returns:
int: Position (1-10) if high score, None if not int: Score at specified position
""" """
for i, entry in enumerate(self.highScores): return self.old_scores[position - 1]
if self.currentScore > entry['score']:
def get_score(self):
"""Get current score.
Returns:
int: Current score
"""
return self.score
def increase_score(self, points=1):
"""Increase the current score.
Args:
points (int): Number of points to increase (default: 1)
"""
self.score += points
def new_high_score(self):
"""Check if current score qualifies as a new high score.
Returns:
int: Position of new high score (1-10), or None if not a high score
"""
for i, j in enumerate(self.old_scores):
if self.score > j:
return i + 1 return i + 1
return None return None
def add_high_score(self): def update_scores(self):
"""Add current score to high scores if it qualifies. """Update the high score list with current score if qualified."""
# Update the scores
Returns: for i, j in enumerate(self.old_scores):
bool: True if score was added, False if not if self.score > j:
""" self.old_scores.insert(i, self.score)
position = self.check_high_score() break
if position is None: # Only keep the top 10 scores
return False self.old_scores = self.old_scores[:10]
# Update the scoreboard section of the games config file
# Prompt for name using get_input for i, j in enumerate(self.old_scores):
name = get_input("New high score! Enter your name:", "Player") localConfig.set("scoreboard", str(i + 1), str(j))
if name is None: # User cancelled
name = "Player"
# Insert new score at correct position
self.highScores.insert(position - 1, {
'name': name,
'score': self.currentScore
})
# Keep only top 10
self.highScores = self.highScores[:10]
# Save to config
for i, entry in enumerate(self.highScores):
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
localConfig.set("scoreboard", f"name_{i+1}", entry['name'])
write_config()
speak(f"Congratulations {name}! You got position {position} on the high score table!")
return True
def write_config(write_global=False): def write_config(write_global=False):
"""Write configuration to file. """Write configuration to file.