From 2369c8b2da9b90ad51a9a6406639f29f45425f53 Mon Sep 17 00:00:00 2001 From: IM23a-bachmannj2 Date: Tue, 4 Feb 2025 10:57:54 +0100 Subject: [PATCH] Leaderboard implemted +- --- README.md | 4 ++-- game/game.py | 2 +- game/menu.py | 2 ++ game/util/util.py | 16 ++++++++++++++-- gamescore.json | 8 +++++++- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5e5d750..8eb8330 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,8 @@ This isn't just about coding; it's about building something I love from the grou 1. [ ] **Themes and Visual Effects**: - [ ] Add background or block skins for variety. - [ ] Implement line clear animations. -2. [ ] **Customizable Controls**: - - [ ] Allow players to remap movement, rotation, and drop keys. +2. [x] **Customizable Controls**: + - [x] Allow players to remap movement, rotation, and drop keys. 3. [ ] **Detailed Stats**: - [ ] Track and display total lines cleared, playtime, and other statistics. diff --git a/game/game.py b/game/game.py index 84c2bb9..4ba5098 100644 --- a/game/game.py +++ b/game/game.py @@ -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)) diff --git a/game/menu.py b/game/menu.py index df62c37..4ba3c8c 100644 --- a/game/menu.py +++ b/game/menu.py @@ -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) diff --git a/game/util/util.py b/game/util/util.py index 8d0d82b..edde725 100644 --- a/game/util/util.py +++ b/game/util/util.py @@ -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) diff --git a/gamescore.json b/gamescore.json index 42733fe..4230637 100644 --- a/gamescore.json +++ b/gamescore.json @@ -1 +1,7 @@ -7300 \ No newline at end of file +{ + "score": [ + 2300, + 100, + 6000 + ] +} \ No newline at end of file