From f8af193c6652cbe89e6ed6a8fd6df838c89d4e18 Mon Sep 17 00:00:00 2001 From: Giovani Rodriguez Date: Mon, 14 Jun 2021 00:33:13 -0400 Subject: [PATCH] feat: add ghost piece --- tetris/entity.py | 34 +++++++++++++++++++++++++++++++--- tetris/game.py | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tetris/entity.py b/tetris/entity.py index 5067a50..7840ac6 100644 --- a/tetris/entity.py +++ b/tetris/entity.py @@ -93,6 +93,7 @@ class Piece(Entity): self.piece_set_sound = mixer.Channel(2) self.previous_points = None self.previous_center = None + self.ghost_points = [] # TODO change name # Gravity self.gravity_time = ConfigurationManager.get("engine", "piece-gravity-time") @@ -119,10 +120,16 @@ class Piece(Entity): self.applying_set = self._apply_set(elapsed_time, game) self.applying_gravity = not self.applying_set - def draw(self, surface: pygame.Surface) -> None: + def draw(self, surface: pygame.Surface, well: Well, stack: "Stack") -> None: + tile_size = ConfigurationManager.get("engine", "tile-size") + + # ghost piece + for square in self._get_ghost_points(well, stack): + pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TDOD change white to yaml + super().draw(surface) - tile_size = ConfigurationManager.get("engine", "tile-size") + # inner border piece for square in self.points: if self.inner_border_color: vertex_one = (square[0][0] + (tile_size // 10), square[0][1] + (tile_size // 10)) @@ -130,6 +137,7 @@ class Piece(Entity): vertex_three = (square[2][0] - (tile_size // 10), square[2][1] - (tile_size // 10)) vertex_four = (square[3][0] + (tile_size // 10), square[3][1] - (tile_size // 10)) new_square = (vertex_one, vertex_two, vertex_three, vertex_four) + pygame.draw.polygon(surface, pygame.Color(self.inner_border_color), new_square, max(tile_size // 6, 1)) def move(self, vector: Tuple) -> None: @@ -178,6 +186,17 @@ class Piece(Entity): return mimic_points + # TODO re work functuon + def _mimic_move_p(self, vector: Tuple, points: Tuple) -> List: + mimic_points = copy.deepcopy(points) + + for square in mimic_points: + for vertex in square: + vertex[0] += vector[0] + vertex[1] += vector[1] + + return mimic_points + def _play_piece_set_sound(self) -> None: piece_set_sound_file = ConfigurationManager.get("sound", "piece-set") self.piece_set_sound.play(mixer.Sound(piece_set_sound_file)) @@ -232,9 +251,18 @@ class Piece(Entity): def _entity_is_below(self, entity: Entity) -> bool: tile_size = ConfigurationManager.get("engine", "tile-size") mimic_points = self._mimic_move((0, tile_size)) - return entity and entity._collide(mimic_points) + # TODO rework function + def _get_ghost_points(self, well: Well, stack: "Stack") -> List: + tile_size = ConfigurationManager.get("engine", "tile-size") + prior_points = [] + current_points = self.points + while not well._collide(current_points) and not stack._collide(current_points): + prior_points = current_points + current_points = self._mimic_move_p((0, tile_size), current_points) + return prior_points + # shape attributes I_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0), (3, 0), (3, 1), (2, 1)), ((3, 0), (4, 0), (4, 1), (3, 1)), (2, 0)) J_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0), (3, 0), (3, 1), (2, 1)), ((2, 1), (3, 1), (3, 2), (2, 2)), (1.5, 0.5)) diff --git a/tetris/game.py b/tetris/game.py index 3fc12f9..50ce935 100644 --- a/tetris/game.py +++ b/tetris/game.py @@ -120,7 +120,7 @@ class Game: if self.stack: self.stack.draw(self.screen) if self.current_piece: - self.current_piece.draw(self.screen) + self.current_piece.draw(self.screen, self.well, self.stack) #TextGenerator.draw("12345678901234567890123456789012345678901234567890", (0, 0), self.screen)