feat(game): read/save highscore

This commit is contained in:
Kristofers Solo 2024-01-07 20:09:12 +02:00
parent 89fbf2e5ab
commit d129ed845f
5 changed files with 45 additions and 25 deletions

1
.gitignore vendored
View File

@ -161,3 +161,4 @@ cython_debug/
debug debug
.logs/ .logs/
checkpoints checkpoints
./assets/highscore

1
assets/highscore Normal file
View File

@ -0,0 +1 @@
940

View File

@ -51,7 +51,7 @@ class Game(BaseScreen):
self.preview.run() self.preview.run()
self.draw() self.draw()
self.clock.tick(CONFIG.fps) self.clock.tick(CONFIG.game.fps)
def mute(self) -> None: def mute(self) -> None:
"""Mute the game.""" """Mute the game."""

View File

@ -1,15 +1,46 @@
import pygame import pygame
from loguru import logger from utils import CONFIG, GameMode
from utils import CONFIG, GameMode, Size
from .base import BaseScreen, SceenElement, TextScreen 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 Score(BaseScreen, SceenElement, TextScreen):
""" """
Class representing the score on the sidebar. Class representing the score on the sidebar.
Args:
game_mode: The game mode.
Attributes: Attributes:
game_mode: The game mode.
highscore: The highscore.
surface: Pygame surface representing the score. surface: Pygame surface representing the score.
display_surface: Pygame display surface. display_surface: Pygame display surface.
rect: Pygame rectangle representing the score. rect: Pygame rectangle representing the score.
@ -20,6 +51,7 @@ class Score(BaseScreen, SceenElement, TextScreen):
def __init__(self, game_mode: GameMode) -> None: def __init__(self, game_mode: GameMode) -> None:
self.game_mode = game_mode self.game_mode = game_mode
self.highscore: int = read_score()
self._initialize_surface() self._initialize_surface()
self._initialize_rect() self._initialize_rect()
self._initialize_font() self._initialize_font()
@ -39,11 +71,16 @@ class Score(BaseScreen, SceenElement, TextScreen):
score (int): Current game score. score (int): Current game score.
level (int): Current game level. level (int): Current game level.
""" """
if score > self.highscore:
self.highscore = score
save_score(score)
self.text: list[tuple[str, int]] = [ self.text: list[tuple[str, int]] = [
("Score", score), ("Score", score),
("Level", level), ("Level", level),
("Lines", lines), ("Lines", lines),
("High Score", CONFIG.game.highscore), ("High Score", self.highscore),
] ]
if self.game_mode in (GameMode.AI_PLAYING, GameMode.AI_TRAINING): if self.game_mode in (GameMode.AI_PLAYING, GameMode.AI_TRAINING):
self.text.append(("Generations", 0)) self.text.append(("Generations", 0))

View File

@ -28,7 +28,8 @@ class Game:
rotation_delay: int = 200 rotation_delay: int = 200
drop_delay: int = 200 drop_delay: int = 200
score: dict[int, int] = {1: 40, 2: 100, 3: 300, 4: 1200} 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 @define
@ -67,23 +68,6 @@ class Music:
landing: Path = BASE_PATH / "assets" / "music" / "landing.wav" 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 @define
class Config: class Config:
game: Game = Game() game: Game = Game()
@ -95,8 +79,5 @@ class Config:
read_settings()["General"]["colorscheme"], TokyoNightNight read_settings()["General"]["colorscheme"], TokyoNightNight
)() )()
ai = AI()
fps: int = 60
CONFIG = Config() CONFIG = Config()