feat(game): add Board background

This commit is contained in:
Kristofers Solo 2024-01-01 22:22:02 +02:00
parent 94a5eb8e0e
commit 22d5f17b9f
4 changed files with 40 additions and 34 deletions

View File

@ -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:

View File

@ -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."""

View File

@ -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):

View File

@ -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