mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): read/save highscore
This commit is contained in:
parent
89fbf2e5ab
commit
d129ed845f
1
.gitignore
vendored
1
.gitignore
vendored
@ -161,3 +161,4 @@ cython_debug/
|
||||
debug
|
||||
.logs/
|
||||
checkpoints
|
||||
./assets/highscore
|
||||
|
||||
1
assets/highscore
Normal file
1
assets/highscore
Normal file
@ -0,0 +1 @@
|
||||
940
|
||||
@ -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."""
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user