diff --git a/__init__.py b/__init__.py index f7ee25d..307e8b2 100755 --- a/__init__.py +++ b/__init__.py @@ -54,102 +54,91 @@ sfxVolume = 1.0 # Default sound effects volume masterVolume = 1.0 # Default master volume class Scoreboard: - """Handles score tracking and top 10 high scores for games. + """Handles high score tracking with player names.""" - This class manages the scoring system including: - - 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) - """ + def __init__(self, score=0): + """Initialize scoreboard with optional starting score.""" read_config() + self.currentScore = score + self.highScores = [] + try: localConfig.add_section("scoreboard") except: pass - self.score = starting_score - self.old_scores = [] + + # Load existing high scores for i in range(1, 11): try: - self.old_scores.insert(i - 1, localConfig.getint("scoreboard", str(i))) + score = localConfig.getint("scoreboard", f"score_{i}") + name = localConfig.get("scoreboard", f"name_{i}") + self.highScores.append({ + 'name': name, + 'score': score + }) except: - self.old_scores.insert(i - 1, 0) - for i in range(1, 11): - if self.old_scores[i - 1] is None: - self.old_scores[i - 1] = 0 - - def __del__(self): - """Save scores when object is destroyed.""" - self.update_scores() - try: - write_config() - except: - pass - - def decrease_score(self, points=1): - """Decrease the current score. - - Args: - points (int): Number of points to decrease (default: 1) - """ - self.score -= points - - def get_high_score(self, position=1): - """Get a high score at specified position. - - Args: - position (int): Position in high score list (1-10, default: 1) - - Returns: - int: Score at specified position - """ - return self.old_scores[position - 1] - + self.highScores.append({ + 'name': "Player", + 'score': 0 + }) + def get_score(self): - """Get current score. + """Get current score.""" + return self.currentScore + + def get_high_scores(self): + """Get list of high scores.""" + return self.highScores + + def decrease_score(self, points=1): + """Decrease the current score.""" + self.currentScore -= points - Returns: - int: Current score - """ - return self.score - def increase_score(self, points=1): - """Increase the current score. + """Increase the current score.""" + self.currentScore += points - 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. + def check_high_score(self): + """Check if current score qualifies as a high score. Returns: - int: Position of new high score (1-10), or None if not a high score + int: Position (1-10) if high score, None if not """ - for i, j in enumerate(self.old_scores): - if self.score > j: + for i, entry in enumerate(self.highScores): + if self.currentScore > entry['score']: return i + 1 return None + + def add_high_score(self, name="Player"): + """Add current score to high scores if it qualifies. + + Args: + name (str): Player name for high score entry + + Returns: + bool: True if score was added, False if not + """ + position = self.check_high_score() + if position is None: + return False + + # 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() + return True - def update_scores(self): - """Update the high score list with current score if qualified.""" - # Update the scores - for i, j in enumerate(self.old_scores): - if self.score > j: - self.old_scores.insert(i, self.score) - break - # Only keep the top 10 scores - self.old_scores = self.old_scores[:10] - # Update the scoreboard section of the games config file - for i, j in enumerate(self.old_scores): - localConfig.set("scoreboard", str(i + 1), str(j)) def write_config(write_global=False): """Write configuration to file.