mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add Board background
This commit is contained in:
parent
94a5eb8e0e
commit
22d5f17b9f
@ -29,37 +29,26 @@ class Block(pygame.sprite.Sprite):
|
|||||||
self.group = group
|
self.group = group
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def _draw_rounded_rect(
|
def _draw_background(self, surface: pygame.Surface) -> None:
|
||||||
self,
|
|
||||||
surface: pygame.Surface,
|
|
||||||
color: ColorScheme | tuple[int, int, int, int],
|
|
||||||
rect: tuple[int, int, int, int],
|
|
||||||
border_radius: int,
|
|
||||||
width: int,
|
|
||||||
) -> None:
|
|
||||||
"""Draw a rounded rectangle with borders on the given surface."""
|
"""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(
|
pygame.draw.rect(
|
||||||
surface,
|
surface,
|
||||||
(0, 0, 0, 0),
|
(0, 0, 0, 0),
|
||||||
rect,
|
rect,
|
||||||
border_radius=border_radius,
|
border_radius=Config.BLOCK_BORDER_RADIUS,
|
||||||
width=width,
|
width=Config.BLOCK_BORDER_WIDTH,
|
||||||
)
|
) # border
|
||||||
|
|
||||||
def _create_block_surface(self) -> pygame.Surface:
|
def _create_block_surface(self) -> pygame.Surface:
|
||||||
"""Create a surface for the block."""
|
"""Create a surface for the block."""
|
||||||
block_surface = pygame.Surface(
|
block_surface = pygame.Surface(
|
||||||
(Config.BLOCK_SIZE, Config.BLOCK_SIZE), pygame.SRCALPHA
|
(Config.BLOCK_SIZE, Config.BLOCK_SIZE), pygame.SRCALPHA
|
||||||
)
|
)
|
||||||
self._draw_rounded_rect(
|
self._draw_background(block_surface)
|
||||||
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,
|
|
||||||
)
|
|
||||||
return block_surface
|
return block_surface
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
|
|||||||
@ -20,12 +20,28 @@ class Board(pygame.sprite.Group):
|
|||||||
"""Initiate the game."""
|
"""Initiate the game."""
|
||||||
self.generate_initial_blocks()
|
self.generate_initial_blocks()
|
||||||
|
|
||||||
def draw(self, screen: pygame.Surface) -> None:
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
"""Draw the board."""
|
"""Draw the board."""
|
||||||
block: Block
|
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):
|
def move(self, direction: Direction):
|
||||||
"""Move the blocks in the specified direction."""
|
"""Move the blocks in the specified direction."""
|
||||||
|
|||||||
@ -2,13 +2,13 @@ from enum import Enum
|
|||||||
|
|
||||||
|
|
||||||
class Original:
|
class Original:
|
||||||
BLOCK_0 = "#ccc0b3"
|
BLOCK_0 = "#cdc1b4"
|
||||||
BLOCK_2 = "#eee4da"
|
BLOCK_2 = "#eee4da"
|
||||||
BLOCK_4 = "#ede0c8"
|
BLOCK_4 = "#eee1c9"
|
||||||
BLOCK_8 = "#f2b179"
|
BLOCK_8 = "#f3b27a"
|
||||||
BLOCK_16 = "#f59563"
|
BLOCK_16 = "#f69664"
|
||||||
BLOCK_32 = "#f67c5f"
|
BLOCK_32 = "#f77c5f"
|
||||||
BLOCK_64 = "#f65e3b"
|
BLOCK_64 = "#f75f3b"
|
||||||
BLOCK_128 = "#edcf72"
|
BLOCK_128 = "#edcf72"
|
||||||
BLOCK_256 = "#edcc61"
|
BLOCK_256 = "#edcc61"
|
||||||
BLOCK_512 = "#edc850"
|
BLOCK_512 = "#edc850"
|
||||||
@ -18,7 +18,8 @@ class Original:
|
|||||||
LIGHT_TEXT = "#f9f6f2"
|
LIGHT_TEXT = "#f9f6f2"
|
||||||
DARK_TEXT = "#776e65"
|
DARK_TEXT = "#776e65"
|
||||||
OTHER = "#000000"
|
OTHER = "#000000"
|
||||||
BG = "#bbada0"
|
BG = "#faf8ef"
|
||||||
|
BOARD_BG = "#bbada0"
|
||||||
|
|
||||||
|
|
||||||
class ColorScheme(Enum):
|
class ColorScheme(Enum):
|
||||||
|
|||||||
@ -6,11 +6,11 @@ class Config:
|
|||||||
FONT_SIZE = 32
|
FONT_SIZE = 32
|
||||||
COLORSCHEME = ColorScheme.ORIGINAL.value
|
COLORSCHEME = ColorScheme.ORIGINAL.value
|
||||||
|
|
||||||
BOARD_SIZE = 4
|
BLOCK_SIZE = 75
|
||||||
BLOCK_SIZE = 50
|
BLOCK_BORDER_WIDTH = BLOCK_SIZE // 20
|
||||||
BLOCK_BORDER_WIDTH = 2
|
BLOCK_BORDER_RADIUS = BLOCK_SIZE // 10
|
||||||
BLOCK_BORDER_RADIUS = 5
|
|
||||||
|
|
||||||
|
BOARD_SIZE = 4
|
||||||
BOARD_WIDTH = BOARD_SIZE * BLOCK_SIZE
|
BOARD_WIDTH = BOARD_SIZE * BLOCK_SIZE
|
||||||
BOARD_HEIGHT = BOARD_SIZE * BLOCK_SIZE
|
BOARD_HEIGHT = BOARD_SIZE * BLOCK_SIZE
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user