feat(game): add "dunder" methods for Block

This commit is contained in:
Kristofers Solo 2023-12-28 16:47:59 +02:00
parent 678b955309
commit 50145ae6a6

View File

@ -8,21 +8,23 @@ from .config import Config
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self, x: int, y: int): def __init__(self, x: int, y: int):
"""Initialize a block"""
super().__init__() super().__init__()
self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE)) self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE))
self.image.fill(COLORS.ERROR)
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.topleft = (x, y) self.rect.topleft = (x, y)
self.value: int = 2 if random.random() <= Config.BLOCK_VALUE_PROBABILITY else 4 self.value: int = 2 if random.random() <= Config.BLOCK_VALUE_PROBABILITY else 4
self.draw_value() self.font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE)
self.update()
def draw_value(self) -> None: def draw_value(self) -> None:
font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE) """Draw the value of the block"""
text = font.render(str(self.value), True, COLORS.FG) text = self.font.render(str(self.value), True, COLORS.FG)
text_rect = text.get_rect(center=self.image.get_rect().center) text_rect = text.get_rect(center=self.image.get_rect().center)
self.image.blit(text, text_rect) self.image.blit(text, text_rect)
def move(self, dx: int, dy: int) -> None: def move(self, dx: int, dy: int) -> None:
"""Move the block by `dx` and `dy`"""
new_x = self.rect.x + dx new_x = self.rect.x + dx
new_y = self.rect.y + dy new_y = self.rect.y + dy
if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE: if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE:
@ -35,30 +37,49 @@ class Block(pygame.sprite.Sprite):
self.update() self.update()
def update(self) -> None: def update(self) -> None:
"""Update the block"""
self.change_color() self.change_color()
self.draw_value() self.draw_value()
def change_color(self) -> None: def change_color(self) -> None:
match self.value: """Change the color of the block based on its value"""
case 2: color_map = {
self.image.fill(COLORS.BLUE) 2: COLORS.BLUE,
case 4: 4: COLORS.BLUE0,
self.image.fill(COLORS.BLUE0) 8: COLORS.BLUE1,
case 8: 16: COLORS.BLUE2,
self.image.fill(COLORS.BLUE1) 32: COLORS.DARK3,
case 16: 64: COLORS.BORDER_HIGHLIGHT,
self.image.fill(COLORS.BLUE2) 128: COLORS.BLUE5,
case 32: 256: COLORS.BLUE6,
self.image.fill(COLORS.DARK3) 512: COLORS.BLUE7,
case 64: 1024: COLORS.ORANGE,
self.image.fill(COLORS.BORDER_HIGHLIGHT) 2048: COLORS.RED,
case 128: }
self.image.fill(COLORS.BLUE5) self.image.fill(color_map.get(self.value, COLORS.ERROR))
case 256:
self.image.fill(COLORS.BLUE6) def __add__(self, other: "Block") -> "Block":
case 512: """Add the value of two blocks and return a new block"""
self.image.fill(COLORS.BLUE7) new_block = Block(self.rect.x, self.rect.y)
case 1024: new_block.value = self.value + other.value
self.image.fill(COLORS.ORANGE) return new_block
case 2048:
self.image.fill(COLORS.RED) def __iadd__(self, other: "Block") -> "Block":
"""Add the value of two blocks and return a new block"""
return self + other
def __eq__(self, other: "Block") -> bool:
"""Check if two block values are equal"""
return self.value == other.value
def __repr__(self) -> str:
"""Return a string representation of the block"""
return f"Block({self.rect.x}, {self.rect.y})"
def __str__(self) -> str:
"""Return a string representation of the block"""
self.__repr__()
def __hash__(self) -> int:
"""Return a hash of the block"""
return hash((self.rect.x, self.rect.y, self.value))