From ca9e19be2a133cd008b64dd8df6f47305568bbf7 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 1 Jan 2024 23:43:39 +0200 Subject: [PATCH] feat(game): add score --- src/py2048/block.py | 12 +++++++----- src/py2048/board.py | 7 +++++-- src/py2048/game.py | 11 ++++++----- src/py2048/screens/header.py | 6 ++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/py2048/block.py b/src/py2048/block.py index c24951b..ac581b4 100644 --- a/src/py2048/block.py +++ b/src/py2048/block.py @@ -63,20 +63,21 @@ class Block(pygame.sprite.Sprite): self.image.blit(block_surface, (0, 0)) - def move(self, direction: Direction) -> None: + def move(self, direction: Direction) -> int: """Move the block by `dx` and `dy`.""" + score = 0 while True: new_x, new_y = self._calc_new_pos(direction) if self._is_out_if_bounds(new_x, new_y): - return + return score if self._has_collision(new_x, new_y): collided_block = self._get_collided_block(new_x, new_y) if collided_block and self._can_merge(collided_block): - self._merge(collided_block) + score += self._merge(collided_block) else: - return + return score self.group.remove(self) self.rect.topleft = new_x, new_y @@ -117,13 +118,14 @@ class Block(pygame.sprite.Sprite): """Check if the block can merge with another block.""" return self.value == other.value - def _merge(self, other: "Block") -> None: + def _merge(self, other: "Block") -> int: """Merge the block with another block.""" self.group.remove(other) self.group.remove(self) self.value += other.value self.update() self.group.add(self) + return self.value def update(self) -> None: """Update the block""" diff --git a/src/py2048/board.py b/src/py2048/board.py index b1beb1a..730d7c4 100644 --- a/src/py2048/board.py +++ b/src/py2048/board.py @@ -43,8 +43,9 @@ class Board(pygame.sprite.Group): border_radius=Config.BLOCK_BORDER_RADIUS, ) # border - def move(self, direction: Direction): + def move(self, direction: Direction) -> int: """Move the blocks in the specified direction.""" + score = 0 blocks = self.sprites() block: Block @@ -59,11 +60,13 @@ class Board(pygame.sprite.Group): blocks.sort(key=lambda block: block.rect.x, reverse=True) for block in blocks: - block.move(direction) + score += block.move(direction) if not self._is_full(): self.generate_random_block() + return score + def generate_initial_blocks(self) -> None: """Generate the initial blocks.""" self.generate_block(Config.INITIAL_BLOCK_COUNT) diff --git a/src/py2048/game.py b/src/py2048/game.py index c6ae1b4..82aca3e 100644 --- a/src/py2048/game.py +++ b/src/py2048/game.py @@ -20,6 +20,7 @@ class Game: pygame.display.set_caption("2048") self.board = Board() self.header = Header() + self.score = 0 def run(self) -> None: """Run the game loop.""" @@ -36,7 +37,7 @@ class Game: """Render the game.""" self.screen.fill(Config.COLORSCHEME.BG) self.board.draw(self.screen) - self.header.draw(self.screen) + self.header.draw(self.screen, self.score) pygame.display.flip() def _hande_events(self) -> None: @@ -57,16 +58,16 @@ class Game: self.exit() def move_up(self) -> None: - self.board.move(Direction.UP) + self.score += self.board.move(Direction.UP) def move_down(self) -> None: - self.board.move(Direction.DOWN) + self.score += self.board.move(Direction.DOWN) def move_left(self) -> None: - self.board.move(Direction.LEFT) + self.score += self.board.move(Direction.LEFT) def move_right(self) -> None: - self.board.move(Direction.RIGHT) + self.score += self.board.move(Direction.RIGHT) def exit(self) -> None: """Exit the game.""" diff --git a/src/py2048/screens/header.py b/src/py2048/screens/header.py index c918d7f..073980c 100644 --- a/src/py2048/screens/header.py +++ b/src/py2048/screens/header.py @@ -7,13 +7,11 @@ from .elements.label import Label class Header: def __init__(self) -> None: self.rect = pygame.Rect(0, 0, Config.HEADER_WIDTH, Config.HEADER_HEIGHT) - self.score = 0 # TODO: Implement score - self.highscore = 0 # TODO: Implement highscore - def draw(self, screen: pygame.Surface) -> None: + def draw(self, screen: pygame.Surface, score: int) -> None: """Draw the header.""" score = Label( - text=f"SCORE\n{self.score}", + text=f"{score}", position=(10, 10), bg_color=Config.COLORSCHEME.BOARD_BG, font_family=Config.FONT_FAMILY,