Rewrote the scoreboard class. Added ability to enter name for scoreboard.
This commit is contained in:
parent
5a791510ea
commit
4a15f951f0
21
__init__.py
21
__init__.py
@ -109,34 +109,37 @@ class Scoreboard:
|
|||||||
return i + 1
|
return i + 1
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def add_high_score(self, name="Player"):
|
def add_high_score(self):
|
||||||
"""Add current score to high scores if it qualifies.
|
"""Add current score to high scores if it qualifies.
|
||||||
|
|
||||||
Args:
|
|
||||||
name (str): Player name for high score entry
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if score was added, False if not
|
bool: True if score was added, False if not
|
||||||
"""
|
"""
|
||||||
position = self.check_high_score()
|
position = self.check_high_score()
|
||||||
if position is None:
|
if position is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Prompt for name using get_input
|
||||||
|
name = get_input("New high score! Enter your name:", "Player")
|
||||||
|
if name is None: # User cancelled
|
||||||
|
name = "Player"
|
||||||
|
|
||||||
# Insert new score at correct position
|
# Insert new score at correct position
|
||||||
self.highScores.insert(position - 1, {
|
self.highScores.insert(position - 1, {
|
||||||
'name': name,
|
'name': name,
|
||||||
'score': self.currentScore
|
'score': self.currentScore
|
||||||
})
|
})
|
||||||
|
|
||||||
# Keep only top 10
|
# Keep only top 10
|
||||||
self.highScores = self.highScores[:10]
|
self.highScores = self.highScores[:10]
|
||||||
|
|
||||||
# Save to config
|
# Save to config
|
||||||
for i, entry in enumerate(self.highScores):
|
for i, entry in enumerate(self.highScores):
|
||||||
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
|
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
|
||||||
localConfig.set("scoreboard", f"name_{i+1}", entry['name'])
|
localConfig.set("scoreboard", f"name_{i+1}", entry['name'])
|
||||||
|
|
||||||
write_config()
|
write_config()
|
||||||
|
speak(f"Congratulations {name}! You got position {position} on the high score table!")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user