From 22d5f17b9f5a588b0b1c4fe1425d73320f98a1bf Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 1 Jan 2024 22:22:02 +0200 Subject: [PATCH] feat(game): add `Board` background --- src/py2048/block.py | 29 +++++++++-------------------- src/py2048/board.py | 22 +++++++++++++++++++--- src/py2048/color.py | 15 ++++++++------- src/py2048/config.py | 8 ++++---- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/py2048/block.py b/src/py2048/block.py index 25bc339..c24951b 100644 --- a/src/py2048/block.py +++ b/src/py2048/block.py @@ -29,37 +29,26 @@ class Block(pygame.sprite.Sprite): self.group = group self.update() - def _draw_rounded_rect( - self, - surface: pygame.Surface, - color: ColorScheme | tuple[int, int, int, int], - rect: tuple[int, int, int, int], - border_radius: int, - width: int, - ) -> None: + def _draw_background(self, surface: pygame.Surface) -> None: """Draw a rounded rectangle with borders on the given surface.""" - pygame.draw.rect(surface, color, rect, border_radius=border_radius) + rect = (0, 0, Config.BLOCK_SIZE, Config.BLOCK_SIZE) + pygame.draw.rect( + surface, self._get_color(), rect, border_radius=Config.BLOCK_BORDER_RADIUS + ) # background pygame.draw.rect( surface, (0, 0, 0, 0), rect, - border_radius=border_radius, - width=width, - ) + border_radius=Config.BLOCK_BORDER_RADIUS, + width=Config.BLOCK_BORDER_WIDTH, + ) # border def _create_block_surface(self) -> pygame.Surface: """Create a surface for the block.""" block_surface = pygame.Surface( (Config.BLOCK_SIZE, Config.BLOCK_SIZE), pygame.SRCALPHA ) - self._draw_rounded_rect( - block_surface, - self._get_color(), - # (255, 255, 255, 0), - (0, 0, Config.BLOCK_SIZE, Config.BLOCK_SIZE), - Config.BLOCK_BORDER_RADIUS, - Config.BLOCK_BORDER_WIDTH, - ) + self._draw_background(block_surface) return block_surface def draw(self) -> None: diff --git a/src/py2048/board.py b/src/py2048/board.py index 9ee954a..b1beb1a 100644 --- a/src/py2048/board.py +++ b/src/py2048/board.py @@ -20,12 +20,28 @@ class Board(pygame.sprite.Group): """Initiate the game.""" self.generate_initial_blocks() - def draw(self, screen: pygame.Surface) -> None: + def draw(self, surface: pygame.Surface) -> None: """Draw the board.""" block: Block - pygame.draw.rect(screen, "#fff500", self.rect, 2) + self._draw_background(surface) - super().draw(screen) + super().draw(surface) + + def _draw_background(self, surface: pygame.Surface) -> None: + """Draw the board background.""" + pygame.draw.rect( + surface, + Config.COLORSCHEME.BOARD_BG, + self.rect, + border_radius=Config.BLOCK_BORDER_RADIUS, + ) # background + pygame.draw.rect( + surface, + Config.COLORSCHEME.BOARD_BG, + self.rect, + width=Config.BLOCK_BORDER_WIDTH, + border_radius=Config.BLOCK_BORDER_RADIUS, + ) # border def move(self, direction: Direction): """Move the blocks in the specified direction.""" diff --git a/src/py2048/color.py b/src/py2048/color.py index ac9a0af..5f99fc5 100644 --- a/src/py2048/color.py +++ b/src/py2048/color.py @@ -2,13 +2,13 @@ from enum import Enum class Original: - BLOCK_0 = "#ccc0b3" + BLOCK_0 = "#cdc1b4" BLOCK_2 = "#eee4da" - BLOCK_4 = "#ede0c8" - BLOCK_8 = "#f2b179" - BLOCK_16 = "#f59563" - BLOCK_32 = "#f67c5f" - BLOCK_64 = "#f65e3b" + BLOCK_4 = "#eee1c9" + BLOCK_8 = "#f3b27a" + BLOCK_16 = "#f69664" + BLOCK_32 = "#f77c5f" + BLOCK_64 = "#f75f3b" BLOCK_128 = "#edcf72" BLOCK_256 = "#edcc61" BLOCK_512 = "#edc850" @@ -18,7 +18,8 @@ class Original: LIGHT_TEXT = "#f9f6f2" DARK_TEXT = "#776e65" OTHER = "#000000" - BG = "#bbada0" + BG = "#faf8ef" + BOARD_BG = "#bbada0" class ColorScheme(Enum): diff --git a/src/py2048/config.py b/src/py2048/config.py index 064fd47..5dbc36c 100644 --- a/src/py2048/config.py +++ b/src/py2048/config.py @@ -6,11 +6,11 @@ class Config: FONT_SIZE = 32 COLORSCHEME = ColorScheme.ORIGINAL.value - BOARD_SIZE = 4 - BLOCK_SIZE = 50 - BLOCK_BORDER_WIDTH = 2 - BLOCK_BORDER_RADIUS = 5 + BLOCK_SIZE = 75 + BLOCK_BORDER_WIDTH = BLOCK_SIZE // 20 + BLOCK_BORDER_RADIUS = BLOCK_SIZE // 10 + BOARD_SIZE = 4 BOARD_WIDTH = BOARD_SIZE * BLOCK_SIZE BOARD_HEIGHT = BOARD_SIZE * BLOCK_SIZE