feat(game): display highscore

This commit is contained in:
Kristofers Solo 2024-01-06 17:23:18 +02:00
parent e77dce308b
commit 126a49f38f
2 changed files with 28 additions and 9 deletions

View File

@ -1,6 +1,8 @@
import pygame
from utils import CONFIG, Size
from game.log import log
from .base import BaseScreen, SceenElement, TextScreen
@ -37,10 +39,12 @@ class Score(BaseScreen, SceenElement, TextScreen):
score (int): Current game score.
level (int): Current game level.
"""
self.text: tuple[tuple[str, int], tuple[str, int], tuple[str, int]] = (
self.text: tuple[tuple[str, int], ...] = (
("Score", score),
("Level", level),
("Lines", lines),
("High Score", CONFIG.game.highscore),
("Generations", 0),
)
def draw(self) -> None:
@ -55,19 +59,33 @@ class Score(BaseScreen, SceenElement, TextScreen):
for idx, text in enumerate(self.text):
x = self.surface.get_width() / 2
y = self.increment_height / 2 + idx * self.increment_height
self._display_text(text, (x, y))
self._display_text(text, pygame.Vector2(x, y))
def _display_text(self, text: tuple[str, int], pos: tuple[float, float]) -> None:
def _display_text(self, text_value: tuple[str, int], pos: pygame.Vector2) -> None:
"""
Display a single text element on the score surface.
Args:
text: A tuple containing the label and value of the text element.
text_value: A tuple containing the label and value of the text element.
pos: The position (x, y) where the text should be displayed.
"""
text_surface = self.font.render(
f"{text[0]}: {text[1]}", True, CONFIG.colors.fg_sidebar
)
text, value = text_value
if len(text) >= 10:
text_surface = self.font.render(f"{text}:", True, CONFIG.colors.fg_sidebar)
value_surface = self.font.render(f"{value}", True, CONFIG.colors.fg_sidebar)
value_rect = value_surface.get_rect(center=(pos.x, pos.y + 40))
self.surface.blit(value_surface, value_rect)
else:
text_surface = self.font.render(
f"{text}:{value}", True, CONFIG.colors.fg_sidebar
)
text_rect = text_surface.get_rect(center=pos)
self.surface.blit(text_surface, text_rect)
text_rect = text_surface.get_rect(center=pos)
self.surface.blit(text_surface, text_rect)
@ -102,7 +120,7 @@ class Score(BaseScreen, SceenElement, TextScreen):
def _initialize_increment_height(self) -> None:
"""Initialize the increment height for positioning text elements."""
self.increment_height = self.surface.get_height() / 3
self.increment_height = self.surface.get_height() / len(self.text)
def _update_display_surface(self) -> None:
"""Update the display surface."""

View File

@ -26,13 +26,14 @@ 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
@define
class SideBar:
padding: int = PADDING
size: Size = Size(200, Game().size.height)
score: Size = Size(size.width, size.height * 0.3 - padding)
score: Size = Size(size.width, size.height - size.width - padding)
preview: Size = Size(size.width, size.width)