mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add Label
This commit is contained in:
parent
22d5f17b9f
commit
ca8aeafed3
@ -6,8 +6,7 @@ authors = [{ name = "Kristofers Solo", email = "dev@kristofers.xyz" }]
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
license = { text = "GPLv3" }
|
license = { text = "GPLv3" }
|
||||||
dependencies = ["pygame-ce==2.3.2", "loguru==0.7.2"]
|
dependencies = ["pygame-ce==2.3.2", "loguru==0.7.2", "attrs==23.1.0"]
|
||||||
|
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
pygame-ce>=2.3.2
|
pygame-ce>=2.3.2
|
||||||
loguru>=0.7.2
|
loguru>=0.7.2
|
||||||
|
attrs>=23.1.0
|
||||||
.
|
.
|
||||||
|
|||||||
34
src/py2048/screens/elements/label.py
Normal file
34
src/py2048/screens/elements/label.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import pygame
|
||||||
|
from attrs import define, field
|
||||||
|
from py2048.color import ColorScheme
|
||||||
|
from py2048.config import Config
|
||||||
|
|
||||||
|
|
||||||
|
@define
|
||||||
|
class Label:
|
||||||
|
text: str
|
||||||
|
position: tuple[int, int]
|
||||||
|
bg_color: ColorScheme
|
||||||
|
font_family: str
|
||||||
|
font_color: ColorScheme
|
||||||
|
font_size: int
|
||||||
|
font: pygame.font.Font = field(init=False)
|
||||||
|
rendered_text: pygame.Surface = field(init=False)
|
||||||
|
rect: pygame.Rect = field(init=False)
|
||||||
|
|
||||||
|
def __attrs_post_init__(self):
|
||||||
|
self.font = pygame.font.SysFont(self.font_family, self.font_size)
|
||||||
|
self._draw_text()
|
||||||
|
|
||||||
|
def _draw_text(self) -> None:
|
||||||
|
self.rendered_text = self.font.render(
|
||||||
|
self.text, True, self.font_color, self.bg_color
|
||||||
|
)
|
||||||
|
self.rect = self.rendered_text.get_rect(topleft=self.position)
|
||||||
|
|
||||||
|
def update(self, new_text: str) -> None:
|
||||||
|
self.text = new_text
|
||||||
|
self._draw_text()
|
||||||
|
|
||||||
|
def draw(self, screen):
|
||||||
|
screen.blit(self.rendered_text, self.position)
|
||||||
@ -1,13 +1,22 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from py2048.config import Config
|
||||||
|
|
||||||
from ..config import Config
|
from .elements.label import Label
|
||||||
|
|
||||||
|
|
||||||
class Header:
|
class Header:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.rect = pygame.Rect(0, 0, Config.HEADER_WIDTH, Config.HEADER_HEIGHT)
|
self.rect = pygame.Rect(0, 0, Config.HEADER_WIDTH, Config.HEADER_HEIGHT)
|
||||||
self.score = 0 # TODO: Implement score
|
self.score = 0 # TODO: Implement score
|
||||||
|
self.highscore = 0 # TODO: Implement highscore
|
||||||
|
|
||||||
def draw(self, screen: pygame.Surface) -> None:
|
def draw(self, screen: pygame.Surface) -> None:
|
||||||
"""Draw the header."""
|
"""Draw the header."""
|
||||||
pygame.draw.rect(screen, "#ff00ee", self.rect, 2)
|
score = Label(
|
||||||
|
text=f"SCORE\n{self.score}",
|
||||||
|
position=(10, 10),
|
||||||
|
bg_color=Config.COLORSCHEME.BOARD_BG,
|
||||||
|
font_family=Config.FONT_FAMILY,
|
||||||
|
font_color=Config.COLORSCHEME.DARK_TEXT,
|
||||||
|
font_size=Config.FONT_SIZE,
|
||||||
|
).draw(screen)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user