mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): make some methods private
This commit is contained in:
parent
566f05f67b
commit
c738084ce1
@ -17,14 +17,14 @@ class Block(pygame.sprite.Sprite):
|
|||||||
self.font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE)
|
self.font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE)
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def draw_value(self) -> None:
|
def _draw_value(self) -> None:
|
||||||
"""Draw the value of the block"""
|
"""Draw the value of the block"""
|
||||||
text = self.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`"""
|
"""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:
|
||||||
@ -38,10 +38,10 @@ class Block(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update the block"""
|
"""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:
|
||||||
"""Change the color of the block based on its value"""
|
"""Change the color of the block based on its value"""
|
||||||
color_map = {
|
color_map = {
|
||||||
2: COLORS.BLUE,
|
2: COLORS.BLUE,
|
||||||
|
|||||||
@ -19,26 +19,26 @@ class Game:
|
|||||||
self.screen: pygame.Surface = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT))
|
self.screen: pygame.Surface = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT))
|
||||||
pygame.display.set_caption("2048")
|
pygame.display.set_caption("2048")
|
||||||
self.sprites = pygame.sprite.Group()
|
self.sprites = pygame.sprite.Group()
|
||||||
self.generate_random_block(Config.INITIAL_BLOCK_COUNT)
|
self._generate_random_block(Config.INITIAL_BLOCK_COUNT)
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Run the game loop."""
|
"""Run the game loop."""
|
||||||
while True:
|
while True:
|
||||||
self.hande_events()
|
self._hande_events()
|
||||||
self.update()
|
self._update()
|
||||||
self.render()
|
self._render()
|
||||||
|
|
||||||
def update(self) -> None:
|
def _update(self) -> None:
|
||||||
"""Update the game."""
|
"""Update the game."""
|
||||||
self.sprites.update()
|
self.sprites.update()
|
||||||
|
|
||||||
def render(self) -> None:
|
def _render(self) -> None:
|
||||||
"""Render the game."""
|
"""Render the game."""
|
||||||
self.screen.fill(COLORS.BG)
|
self.screen.fill(COLORS.BG)
|
||||||
self.sprites.draw(self.screen)
|
self.sprites.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def hande_events(self) -> None:
|
def _hande_events(self) -> None:
|
||||||
"""Handle pygame events."""
|
"""Handle pygame events."""
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
@ -90,9 +90,9 @@ class Game:
|
|||||||
block.increase_value()
|
block.increase_value()
|
||||||
self.sprites.remove(other_block)
|
self.sprites.remove(other_block)
|
||||||
moved_blocks.add(block)
|
moved_blocks.add(block)
|
||||||
self.generate_random_block()
|
self._generate_random_block()
|
||||||
|
|
||||||
def generate_random_block(self, count: int = 1) -> None:
|
def _generate_random_block(self, count: int = 1) -> None:
|
||||||
"""Generate `count` number of random blocks."""
|
"""Generate `count` number of random blocks."""
|
||||||
for _ in range(count):
|
for _ in range(count):
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user