mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add score
This commit is contained in:
parent
ca8aeafed3
commit
ca9e19be2a
@ -63,20 +63,21 @@ class Block(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
self.image.blit(block_surface, (0, 0))
|
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`."""
|
"""Move the block by `dx` and `dy`."""
|
||||||
|
score = 0
|
||||||
while True:
|
while True:
|
||||||
new_x, new_y = self._calc_new_pos(direction)
|
new_x, new_y = self._calc_new_pos(direction)
|
||||||
|
|
||||||
if self._is_out_if_bounds(new_x, new_y):
|
if self._is_out_if_bounds(new_x, new_y):
|
||||||
return
|
return score
|
||||||
|
|
||||||
if self._has_collision(new_x, new_y):
|
if self._has_collision(new_x, new_y):
|
||||||
collided_block = self._get_collided_block(new_x, new_y)
|
collided_block = self._get_collided_block(new_x, new_y)
|
||||||
if collided_block and self._can_merge(collided_block):
|
if collided_block and self._can_merge(collided_block):
|
||||||
self._merge(collided_block)
|
score += self._merge(collided_block)
|
||||||
else:
|
else:
|
||||||
return
|
return score
|
||||||
|
|
||||||
self.group.remove(self)
|
self.group.remove(self)
|
||||||
self.rect.topleft = new_x, new_y
|
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."""
|
"""Check if the block can merge with another block."""
|
||||||
return self.value == other.value
|
return self.value == other.value
|
||||||
|
|
||||||
def _merge(self, other: "Block") -> None:
|
def _merge(self, other: "Block") -> int:
|
||||||
"""Merge the block with another block."""
|
"""Merge the block with another block."""
|
||||||
self.group.remove(other)
|
self.group.remove(other)
|
||||||
self.group.remove(self)
|
self.group.remove(self)
|
||||||
self.value += other.value
|
self.value += other.value
|
||||||
self.update()
|
self.update()
|
||||||
self.group.add(self)
|
self.group.add(self)
|
||||||
|
return self.value
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update the block"""
|
"""Update the block"""
|
||||||
|
|||||||
@ -43,8 +43,9 @@ class Board(pygame.sprite.Group):
|
|||||||
border_radius=Config.BLOCK_BORDER_RADIUS,
|
border_radius=Config.BLOCK_BORDER_RADIUS,
|
||||||
) # border
|
) # border
|
||||||
|
|
||||||
def move(self, direction: Direction):
|
def move(self, direction: Direction) -> int:
|
||||||
"""Move the blocks in the specified direction."""
|
"""Move the blocks in the specified direction."""
|
||||||
|
score = 0
|
||||||
blocks = self.sprites()
|
blocks = self.sprites()
|
||||||
block: Block
|
block: Block
|
||||||
|
|
||||||
@ -59,11 +60,13 @@ class Board(pygame.sprite.Group):
|
|||||||
blocks.sort(key=lambda block: block.rect.x, reverse=True)
|
blocks.sort(key=lambda block: block.rect.x, reverse=True)
|
||||||
|
|
||||||
for block in blocks:
|
for block in blocks:
|
||||||
block.move(direction)
|
score += block.move(direction)
|
||||||
|
|
||||||
if not self._is_full():
|
if not self._is_full():
|
||||||
self.generate_random_block()
|
self.generate_random_block()
|
||||||
|
|
||||||
|
return score
|
||||||
|
|
||||||
def generate_initial_blocks(self) -> None:
|
def generate_initial_blocks(self) -> None:
|
||||||
"""Generate the initial blocks."""
|
"""Generate the initial blocks."""
|
||||||
self.generate_block(Config.INITIAL_BLOCK_COUNT)
|
self.generate_block(Config.INITIAL_BLOCK_COUNT)
|
||||||
|
|||||||
@ -20,6 +20,7 @@ class Game:
|
|||||||
pygame.display.set_caption("2048")
|
pygame.display.set_caption("2048")
|
||||||
self.board = Board()
|
self.board = Board()
|
||||||
self.header = Header()
|
self.header = Header()
|
||||||
|
self.score = 0
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Run the game loop."""
|
"""Run the game loop."""
|
||||||
@ -36,7 +37,7 @@ class Game:
|
|||||||
"""Render the game."""
|
"""Render the game."""
|
||||||
self.screen.fill(Config.COLORSCHEME.BG)
|
self.screen.fill(Config.COLORSCHEME.BG)
|
||||||
self.board.draw(self.screen)
|
self.board.draw(self.screen)
|
||||||
self.header.draw(self.screen)
|
self.header.draw(self.screen, self.score)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def _hande_events(self) -> None:
|
def _hande_events(self) -> None:
|
||||||
@ -57,16 +58,16 @@ class Game:
|
|||||||
self.exit()
|
self.exit()
|
||||||
|
|
||||||
def move_up(self) -> None:
|
def move_up(self) -> None:
|
||||||
self.board.move(Direction.UP)
|
self.score += self.board.move(Direction.UP)
|
||||||
|
|
||||||
def move_down(self) -> None:
|
def move_down(self) -> None:
|
||||||
self.board.move(Direction.DOWN)
|
self.score += self.board.move(Direction.DOWN)
|
||||||
|
|
||||||
def move_left(self) -> None:
|
def move_left(self) -> None:
|
||||||
self.board.move(Direction.LEFT)
|
self.score += self.board.move(Direction.LEFT)
|
||||||
|
|
||||||
def move_right(self) -> None:
|
def move_right(self) -> None:
|
||||||
self.board.move(Direction.RIGHT)
|
self.score += self.board.move(Direction.RIGHT)
|
||||||
|
|
||||||
def exit(self) -> None:
|
def exit(self) -> None:
|
||||||
"""Exit the game."""
|
"""Exit the game."""
|
||||||
|
|||||||
@ -7,13 +7,11 @@ from .elements.label import Label
|
|||||||
class Header:
|
class Header:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.rect = pygame.Rect(0, 0, Config.HEADER_WIDTH, Config.HEADER_HEIGHT)
|
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."""
|
"""Draw the header."""
|
||||||
score = Label(
|
score = Label(
|
||||||
text=f"SCORE\n{self.score}",
|
text=f"{score}",
|
||||||
position=(10, 10),
|
position=(10, 10),
|
||||||
bg_color=Config.COLORSCHEME.BOARD_BG,
|
bg_color=Config.COLORSCHEME.BOARD_BG,
|
||||||
font_family=Config.FONT_FAMILY,
|
font_family=Config.FONT_FAMILY,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user