Add statistics, per level high score and tries

This commit is contained in:
csd4ni3l
2025-11-29 15:38:01 +01:00
parent 9b80dba667
commit 879167822e
3 changed files with 70 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
import arcade, arcade.gui, json, time, os 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 from utils.preload import tilemaps, player_still_animation, player_jump_animation, player_walk_animation, freeze_sound, background_sound
class Game(arcade.gui.UIView): class Game(arcade.gui.UIView):
@@ -61,7 +61,14 @@ class Game(arcade.gui.UIView):
with open("data.json", "r") as file: with open("data.json", "r") as file:
self.data = json.load(file) self.data = json.load(file)
else: 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.high_score = self.data.get("high_score", 9999)
self.tries = self.data.get("tries", 1) self.tries = self.data.get("tries", 1)
@@ -238,8 +245,8 @@ class Game(arcade.gui.UIView):
def update_data_file(self): def update_data_file(self):
with open("data.json", "w") as file: with open("data.json", "w") as file:
file.write(json.dumps({ file.write(json.dumps({
"high_score": self.high_score, f"{self.level_num}_high_score": self.high_score,
"tries": self.tries f"{self.level_num}_tries": self.tries
}, indent=4)) }, indent=4))
def on_key_press(self, symbol, modifiers): def on_key_press(self, symbol, modifiers):

View File

@@ -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 = 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.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 = 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() 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): def play(self):
from menus.level_selector import LevelSelector from menus.level_selector import LevelSelector
self.window.show_view(LevelSelector(self.pypresence_client)) self.window.show_view(LevelSelector(self.pypresence_client))

52
menus/statistics.py Normal file
View File

@@ -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))