From 879167822e39063a30a3159c2c06de60e66266f5 Mon Sep 17 00:00:00 2001 From: csd4ni3l Date: Sat, 29 Nov 2025 15:38:01 +0100 Subject: [PATCH] Add statistics, per level high score and tries --- game/play.py | 15 +++++++++---- menus/main.py | 7 ++++++ menus/statistics.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 menus/statistics.py diff --git a/game/play.py b/game/play.py index 3b66b05..03017bd 100644 --- a/game/play.py +++ b/game/play.py @@ -1,6 +1,6 @@ import arcade, arcade.gui, json, time, os -from utils.constants import FOLLOW_DECAY_CONST, GRAVITY, PLAYER_MOVEMENT_SPEED, PLAYER_JUMP_SPEED, GRID_PIXEL_SIZE, PLAYER_JUMP_COOLDOWN, LEFT_RIGHT_DIAGONAL_ID, RIGHT_LEFT_DIAGONAL_ID +from utils.constants import FOLLOW_DECAY_CONST, GRAVITY, PLAYER_MOVEMENT_SPEED, PLAYER_JUMP_SPEED, GRID_PIXEL_SIZE, PLAYER_JUMP_COOLDOWN, LEFT_RIGHT_DIAGONAL_ID, RIGHT_LEFT_DIAGONAL_ID, AVAILABLE_LEVELS from utils.preload import tilemaps, player_still_animation, player_jump_animation, player_walk_animation, freeze_sound, background_sound class Game(arcade.gui.UIView): @@ -61,7 +61,14 @@ class Game(arcade.gui.UIView): with open("data.json", "r") as file: self.data = json.load(file) else: - self.data = {} + self.data = { + f"{level_num}_high_score": 9999 + for level_num in range(AVAILABLE_LEVELS) + } + self.data.update({ + f"{level_num}_tries": 0 + for level_num in range(AVAILABLE_LEVELS) + }) self.high_score = self.data.get("high_score", 9999) self.tries = self.data.get("tries", 1) @@ -238,8 +245,8 @@ class Game(arcade.gui.UIView): def update_data_file(self): with open("data.json", "w") as file: file.write(json.dumps({ - "high_score": self.high_score, - "tries": self.tries + f"{self.level_num}_high_score": self.high_score, + f"{self.level_num}_tries": self.tries }, indent=4)) def on_key_press(self, symbol, modifiers): diff --git a/menus/main.py b/menus/main.py index 4034a88..543a97c 100644 --- a/menus/main.py +++ b/menus/main.py @@ -55,9 +55,16 @@ class Main(arcade.gui.UIView): self.play_button = self.box.add(arcade.gui.UITextureButton(text="Play", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) self.play_button.on_click = lambda event: self.play() + self.statistics_button = self.box.add(arcade.gui.UITextureButton(text="Statistics", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.statistics_button.on_click = lambda event: self.statistics() + self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) self.settings_button.on_click = lambda event: self.settings() + def statistics(self): + from menus.statistics import Statistics + self.window.show_view(Statistics(self.pypresence_client)) + def play(self): from menus.level_selector import LevelSelector self.window.show_view(LevelSelector(self.pypresence_client)) diff --git a/menus/statistics.py b/menus/statistics.py new file mode 100644 index 0000000..95ef490 --- /dev/null +++ b/menus/statistics.py @@ -0,0 +1,52 @@ +import arcade, arcade.gui, json, os + +from utils.constants import AVAILABLE_LEVELS, button_style +from utils.preload import button_texture, button_hovered_texture + +class Statistics(arcade.gui.UIView): + def __init__(self, pypresence_client): + super().__init__() + + self.pypresence_client = pypresence_client + + self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) + self.box = self.anchor.add(arcade.gui.UIBoxLayout(align="left"), anchor_x="center", anchor_y="top") + + if os.path.exists("data.json"): + with open("data.json", "r") as file: + self.data = json.load(file) + else: + self.data = { + f"{level_num}_high_score": 9999 + for level_num in range(AVAILABLE_LEVELS) + } + self.data.update({ + f"{level_num}_tries": 0 + for level_num in range(AVAILABLE_LEVELS) + }) + + self.total_tries = sum([self.data[f"{level_num}_tries"] for level_num in range(AVAILABLE_LEVELS)]) + + def on_show_view(self): + super().on_show_view() + + self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50) + self.back_button.on_click = lambda event: self.main_exit() + self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=10, align_y=-10) + + self.box.add(arcade.gui.UILabel("Statistics", font_size=40)) + + self.box.add(arcade.gui.UISpace(height=self.window.height / 15)) + + for level_num in range(AVAILABLE_LEVELS): + self.box.add(arcade.gui.UILabel(f"Level {level_num + 1}", font_size=32)) + self.box.add(arcade.gui.UILabel(f"High Score: {self.data[f'{level_num}_high_score']}", font_size=24)) + self.box.add(arcade.gui.UILabel(f"Tries: {self.data[f'{level_num}_tries']}", font_size=24)) + self.box.add(arcade.gui.UISpace(height=self.window.height / 15)) + + self.box.add(arcade.gui.UILabel(f"Total", font_size=32)) + self.box.add(arcade.gui.UILabel(f"Total Tries: {self.total_tries}", font_size=24)) + + def main_exit(self): + from menus.main import Main + self.window.show_view(Main(self.pypresence_client)) \ No newline at end of file