Rewrote the scoreboard class.

This commit is contained in:
Storm Dragon 2025-02-14 16:45:28 -05:00
parent 7cbbc64d27
commit 5a791510ea

View File

@ -54,102 +54,91 @@ sfxVolume = 1.0 # Default sound effects volume
masterVolume = 1.0 # Default master volume masterVolume = 1.0 # Default master volume
class Scoreboard: 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: def __init__(self, score=0):
- Score tracking """Initialize scoreboard with optional starting score."""
- 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
self.old_scores = [] # Load existing high scores
for i in range(1, 11): for i in range(1, 11):
try: 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: except:
self.old_scores.insert(i - 1, 0) self.highScores.append({
for i in range(1, 11): 'name': "Player",
if self.old_scores[i - 1] is None: 'score': 0
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]
def get_score(self): 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): def increase_score(self, points=1):
"""Increase the current score. """Increase the current score."""
self.currentScore += points
Args: def check_high_score(self):
points (int): Number of points to increase (default: 1) """Check if current score qualifies as a high score.
"""
self.score += points
def new_high_score(self):
"""Check if current score qualifies as a new high score.
Returns: 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): for i, entry in enumerate(self.highScores):
if self.score > j: if self.currentScore > entry['score']:
return i + 1 return i + 1
return None 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): def write_config(write_global=False):
"""Write configuration to file. """Write configuration to file.