Leaderboard implemted +-

This commit is contained in:
IM23a-bachmannj2
2025-02-04 10:57:54 +01:00
parent 16c1ef8dd6
commit 2369c8b2da
5 changed files with 26 additions and 6 deletions
+1 -1
View File
@@ -120,7 +120,7 @@ class Game:
draw_text(self.screen, f'Score: {settings.game_score}', get_font(20), self.width - 150, 100, (255, 255, 255))
# Render surfaces
self.screen.blit(self.grid_surface, (0, 0))
self.screen.blit(self.grid_surface, (settings.width // 2 - 150, 50))
self.screen.blit(self.next_surface, (self.width - 150, 200))
self.screen.blit(self.hold_surface, (self.width - 150, 400))
+2
View File
@@ -70,6 +70,8 @@ class StartMenu:
draw_text(self.screen, f"Score: {item}", get_font(30), setting.width // 5, setting.height // 3 - 50 + position, (255, 255, 255))
position += 50
self.score = load_score("gamescore.json")
self.play.update(self.screen)
self.options.update(self.screen)
self.quit.update(self.screen)
+14 -2
View File
@@ -37,8 +37,20 @@ def load_score(filename):
def add_score(filename, score):
if os.path.exists(filename):
with open(os.path.join(os.getcwd(), filename), "w") as file:
json.dump(score, file)
with open(filename, "r") as file:
data = json.load(file)
# If the stored data doesn't have a "score" key for some reason, ensure it does.
if "score" not in data:
data["score"] = []
# Append the new score.
# You can also handle multiple scores by extending the list, e.g. `data["score"].extend(new_score)`.
data["score"].append(score)
# Write the updated data back to the file.
with open(filename, "w") as file:
json.dump(data, file, indent=2)
# For keybinds (control.py)