From 4929bdff441158bc287298012fdf8b6b5944f7c0 Mon Sep 17 00:00:00 2001 From: Giovani Date: Thu, 10 Jun 2021 17:45:12 -0400 Subject: [PATCH] feat: add set piece logic --- Tetris.py | 9 +++------ entity/Piece.py | 29 +++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Tetris.py b/Tetris.py index d6a37c1..77fb109 100644 --- a/Tetris.py +++ b/Tetris.py @@ -51,7 +51,9 @@ class Tetris: elapsed_time = self.clock.tick(self.fps) if self.current_piece: - self.current_piece.update(elapsed_time, self.well, self.stack) + self.current_piece.update(elapsed_time, self) + else: + self.__generate_piece((300, 100)) if self.stack: self.stack.update(elapsed_time) @@ -87,11 +89,6 @@ class Tetris: self.current_piece.revert() if self.stack and self.current_piece.collide(self.stack): self.current_piece.revert() - if event.key == pygame.K_z: - if self.current_piece: - self.current_piece.play_piece_set_sound() - self.stack.add_piece(self.current_piece) - self.__generate_piece((300, 100)) def draw(self) -> None: diff --git a/entity/Piece.py b/entity/Piece.py index c964460..ef52a83 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -13,28 +13,42 @@ from util.ConfigurationManager import ConfigurationManager class Piece(Entity): GRAVITY = 300 # A move down every 300 ms + PIECE_SET = 750 def __init__(self, shape: Tuple, position: Tuple, color: str, inner_border_color: str, border_color: str): super().__init__(self.__get_points(shape, position), color, border_color) self.inner_border_color = inner_border_color self.center = self.__get_center(shape, position) self.piece_set_sound = mixer.Channel(2) + self.piece_set_time = 0 self.previous_points = None self.previous_center = None - def update(self, elapsed_time: int, well: Entity, stack: Entity) -> None: + def update(self, elapsed_time: int, tetris) -> None: super().update(elapsed_time) - tile_size = ConfigurationManager.configuration["engine"]["tile-size"] - if self.elapsed_time >= Piece.GRAVITY: + well = tetris.well + stack = tetris.stack + + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + if self.elapsed_time >= Piece.GRAVITY and self.piece_set_time == 0: self.elapsed_time -= Piece.GRAVITY self.move((0, tile_size)) - if well and self.collide(well): - self.revert() - if stack and self.collide(stack): + if well and self.collide(well) or stack and self.collide(stack): self.revert() + self.piece_set_time += elapsed_time + + if self.piece_set_time > 0: + self.piece_set_time += elapsed_time + + if self.piece_set_time >= Piece.PIECE_SET: + self.__play_piece_set_sound() + self.piece_set_time = 0 + + stack.add_piece(self) + tetris.current_piece = None def draw(self, surface: pygame.Surface) -> None: super().draw(surface) @@ -89,8 +103,7 @@ class Piece(Entity): self.points = self.previous_points self.center = self.previous_center - # TODO should be private - def play_piece_set_sound(self) -> None: + def __play_piece_set_sound(self) -> None: piece_set_sound_file = ConfigurationManager.configuration["sound"]["piece-set"] self.piece_set_sound.play(mixer.Sound(piece_set_sound_file))