diff --git a/.gitignore b/.gitignore index a376f21..070453f 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,4 @@ cython_debug/ debug .logs/ checkpoints +./assets/highscore diff --git a/assets/highscore b/assets/highscore new file mode 100644 index 0000000..90afd51 --- /dev/null +++ b/assets/highscore @@ -0,0 +1 @@ +940 \ No newline at end of file diff --git a/src/game/screens/game.py b/src/game/screens/game.py index a429796..8e27485 100644 --- a/src/game/screens/game.py +++ b/src/game/screens/game.py @@ -51,7 +51,7 @@ class Game(BaseScreen): self.preview.run() self.draw() - self.clock.tick(CONFIG.fps) + self.clock.tick(CONFIG.game.fps) def mute(self) -> None: """Mute the game.""" diff --git a/src/game/screens/score.py b/src/game/screens/score.py index 9d39c1f..213416c 100644 --- a/src/game/screens/score.py +++ b/src/game/screens/score.py @@ -1,15 +1,46 @@ import pygame -from loguru import logger -from utils import CONFIG, GameMode, Size +from utils import CONFIG, GameMode from .base import BaseScreen, SceenElement, TextScreen +def save_score(score: int) -> None: + """ + Save the score to the highscore file. + + Args: + score: The score to be saved. + """ + with open(CONFIG.game.highscore, "w") as file: + file.write(str(score)) + + +def read_score() -> int: + """ + Read the score from the highscore file. + + Returns: + The score read from the file. + """ + try: + with open(CONFIG.game.highscore, "r") as file: + return int(file.read()) + except FileNotFoundError: + return 0 + except ValueError: + return 0 + + class Score(BaseScreen, SceenElement, TextScreen): """ Class representing the score on the sidebar. + Args: + game_mode: The game mode. + Attributes: + game_mode: The game mode. + highscore: The highscore. surface: Pygame surface representing the score. display_surface: Pygame display surface. rect: Pygame rectangle representing the score. @@ -20,6 +51,7 @@ class Score(BaseScreen, SceenElement, TextScreen): def __init__(self, game_mode: GameMode) -> None: self.game_mode = game_mode + self.highscore: int = read_score() self._initialize_surface() self._initialize_rect() self._initialize_font() @@ -39,11 +71,16 @@ class Score(BaseScreen, SceenElement, TextScreen): score (int): Current game score. level (int): Current game level. """ + + if score > self.highscore: + self.highscore = score + save_score(score) + self.text: list[tuple[str, int]] = [ ("Score", score), ("Level", level), ("Lines", lines), - ("High Score", CONFIG.game.highscore), + ("High Score", self.highscore), ] if self.game_mode in (GameMode.AI_PLAYING, GameMode.AI_TRAINING): self.text.append(("Generations", 0)) diff --git a/src/utils/config.py b/src/utils/config.py index c25e248..e1e0487 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -28,7 +28,8 @@ class Game: rotation_delay: int = 200 drop_delay: int = 200 score: dict[int, int] = {1: 40, 2: 100, 3: 300, 4: 1200} - highscore: int = 1_000_000 # TODO: read from file + highscore: Path = BASE_PATH / "assets" / "highscore" + fps: int = 60 @define @@ -67,23 +68,6 @@ class Music: landing: Path = BASE_PATH / "assets" / "music" / "landing.wav" -@define -class Checkpoint: - generation_interval: int = 10 - time_interval: float = 900 - filename_prefix: str = str(BASE_PATH / "checkpoints" / "neat-checkpoint-") - - -@define -class AI: - generations: int = 200 - parallels: int = 1 - winner_path: Path = BASE_PATH / "winner" - plot_path: Path = BASE_PATH / "plots" - config_path: Path = BASE_PATH / "config" - checkpoint: Checkpoint = Checkpoint() - - @define class Config: game: Game = Game() @@ -95,8 +79,5 @@ class Config: read_settings()["General"]["colorscheme"], TokyoNightNight )() - ai = AI() - fps: int = 60 - CONFIG = Config()