mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): Label
This commit is contained in:
parent
624401d27b
commit
65653457ed
@ -36,7 +36,7 @@ class Game:
|
||||
"""Render the game."""
|
||||
self.screen.fill(Config.COLORSCHEME.BG)
|
||||
self.board.draw(self.screen)
|
||||
self.header.draw(self.screen, 0)
|
||||
self.header.draw(self.screen, 2048)
|
||||
# self.menu.draw(self.screen)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
@ -16,6 +16,8 @@ class UIElement(ABC, metaclass=ABCMeta):
|
||||
position: Position,
|
||||
bg_color: str,
|
||||
font_color: str,
|
||||
font_size: int = Config.FONT.size,
|
||||
font_family: str = Config.FONT.family,
|
||||
size: Size = Size(50, 50),
|
||||
text: str = "",
|
||||
border_radius: int = 0,
|
||||
@ -30,11 +32,15 @@ class UIElement(ABC, metaclass=ABCMeta):
|
||||
self.border_width = border_width
|
||||
self.position = position
|
||||
self.x, self.y = self.position
|
||||
self.font = pygame.font.SysFont(Config.FONT.family, Config.FONT.size)
|
||||
self.font = pygame.font.SysFont(font_family, font_size)
|
||||
|
||||
@abstractmethod
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
"""Draw the element on the given surface."""
|
||||
"""Draw the element."""
|
||||
|
||||
@abstractmethod
|
||||
def update(self) -> None:
|
||||
"""Update the element."""
|
||||
|
||||
@abstractmethod
|
||||
def _draw_background(self, surface: pygame.Surface) -> None:
|
||||
|
||||
@ -1,34 +1,57 @@
|
||||
import pygame
|
||||
from attrs import define, field
|
||||
from loguru import logger
|
||||
|
||||
from py2048 import Config
|
||||
from py2048.utils import Position, Size
|
||||
|
||||
from .abc import UIElement
|
||||
|
||||
|
||||
@define
|
||||
class Label:
|
||||
text: str
|
||||
position: tuple[int, int]
|
||||
bg_color: str
|
||||
font_family: str
|
||||
font_color: str
|
||||
font_size: int
|
||||
font: pygame.Font = field(init=False)
|
||||
rendered_text: pygame.Surface = field(init=False)
|
||||
rect: pygame.Rect = field(init=False)
|
||||
class Label(UIElement):
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def __attrs_post_init__(self):
|
||||
self.font = pygame.font.SysFont(self.font_family, self.font_size)
|
||||
self._draw_text()
|
||||
self.image = self._create_surface()
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.topleft = self.position
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
surface.blit(self.rendered_text, self.position)
|
||||
|
||||
def update(self, new_text: str) -> None:
|
||||
self.text = new_text
|
||||
"""Draw the element on the given surface."""
|
||||
# self._draw_background(surface)
|
||||
self._draw_text()
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update the sprite."""
|
||||
self._draw_background(self.image)
|
||||
self._draw_text()
|
||||
self.image.blit(self.rendered_text, self.position)
|
||||
|
||||
def _draw_background(self, surface: pygame.Surface) -> None:
|
||||
"""Draw a background for the given surface."""
|
||||
rect = (0, 0, *self.size)
|
||||
pygame.draw.rect(
|
||||
surface, self.bg_color, rect, border_radius=Config.TILE.border.radius
|
||||
) # background
|
||||
pygame.draw.rect(
|
||||
surface,
|
||||
(0, 0, 0, 0),
|
||||
rect,
|
||||
border_radius=Config.TILE.border.radius,
|
||||
width=Config.TILE.border.width,
|
||||
) # border
|
||||
|
||||
def _draw_text(self) -> None:
|
||||
"""Draw the text of the element."""
|
||||
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)
|
||||
self.image.blit(
|
||||
self.rendered_text,
|
||||
self.rendered_text.get_rect(center=self.image.get_rect().center),
|
||||
)
|
||||
|
||||
def _create_surface(self) -> pygame.Surface:
|
||||
"""Create a surface for the element."""
|
||||
surface = pygame.Surface(self.size, pygame.SRCALPHA)
|
||||
self._draw_background(surface)
|
||||
return surface
|
||||
|
||||
@ -2,7 +2,7 @@ import pygame
|
||||
|
||||
from py2048 import Config
|
||||
from py2048.objects import Label
|
||||
from py2048.utils import Position
|
||||
from py2048.utils import Position, Size
|
||||
|
||||
|
||||
class Header:
|
||||
@ -11,15 +11,17 @@ class Header:
|
||||
|
||||
def draw(self, screen: pygame.Surface, score: int) -> None:
|
||||
"""Draw the header."""
|
||||
self.score = Label(
|
||||
self.label = Label(
|
||||
text=f"{score}",
|
||||
position=Position(10, 10),
|
||||
size=Size(50, 50),
|
||||
position=Position(0, 0),
|
||||
bg_color=Config.COLORSCHEME.BOARD_BG,
|
||||
font_family=Config.FONT.family,
|
||||
font_color=Config.COLORSCHEME.DARK_TEXT,
|
||||
font_size=Config.FONT.size,
|
||||
).draw(screen)
|
||||
font_size=16,
|
||||
)
|
||||
self.label.draw(screen)
|
||||
|
||||
def update(self, score: int) -> None:
|
||||
"""Update the header."""
|
||||
self.Label.text = f"{score}"
|
||||
self.label.text = f"SCORE\n{score}"
|
||||
self.label.update()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user