mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): display highscore
This commit is contained in:
parent
e77dce308b
commit
126a49f38f
@ -1,6 +1,8 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from utils import CONFIG, Size
|
from utils import CONFIG, Size
|
||||||
|
|
||||||
|
from game.log import log
|
||||||
|
|
||||||
from .base import BaseScreen, SceenElement, TextScreen
|
from .base import BaseScreen, SceenElement, TextScreen
|
||||||
|
|
||||||
|
|
||||||
@ -37,10 +39,12 @@ 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.
|
||||||
"""
|
"""
|
||||||
self.text: tuple[tuple[str, int], tuple[str, int], tuple[str, int]] = (
|
self.text: tuple[tuple[str, int], ...] = (
|
||||||
("Score", score),
|
("Score", score),
|
||||||
("Level", level),
|
("Level", level),
|
||||||
("Lines", lines),
|
("Lines", lines),
|
||||||
|
("High Score", CONFIG.game.highscore),
|
||||||
|
("Generations", 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
@ -55,19 +59,33 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
|||||||
for idx, text in enumerate(self.text):
|
for idx, text in enumerate(self.text):
|
||||||
x = self.surface.get_width() / 2
|
x = self.surface.get_width() / 2
|
||||||
y = self.increment_height / 2 + idx * self.increment_height
|
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.
|
Display a single text element on the score surface.
|
||||||
|
|
||||||
Args:
|
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.
|
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)
|
text_rect = text_surface.get_rect(center=pos)
|
||||||
self.surface.blit(text_surface, text_rect)
|
self.surface.blit(text_surface, text_rect)
|
||||||
|
|
||||||
@ -102,7 +120,7 @@ class Score(BaseScreen, SceenElement, TextScreen):
|
|||||||
|
|
||||||
def _initialize_increment_height(self) -> None:
|
def _initialize_increment_height(self) -> None:
|
||||||
"""Initialize the increment height for positioning text elements."""
|
"""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:
|
def _update_display_surface(self) -> None:
|
||||||
"""Update the display surface."""
|
"""Update the display surface."""
|
||||||
|
|||||||
@ -26,13 +26,14 @@ 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
|
||||||
|
|
||||||
|
|
||||||
@define
|
@define
|
||||||
class SideBar:
|
class SideBar:
|
||||||
padding: int = PADDING
|
padding: int = PADDING
|
||||||
size: Size = Size(200, Game().size.height)
|
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)
|
preview: Size = Size(size.width, size.width)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user