From 9b90e26a36c2e754a94cc688b78e2373030e282e Mon Sep 17 00:00:00 2001 From: Giovani Date: Thu, 10 Jun 2021 14:01:26 -0400 Subject: [PATCH] feat: implement line completion logic --- Tetris.py | 3 +++ entity/Piece.py | 4 ++-- entity/Stack.py | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Tetris.py b/Tetris.py index f6cc2df..b05305d 100644 --- a/Tetris.py +++ b/Tetris.py @@ -45,6 +45,9 @@ class Tetris: if self.current_piece: self.current_piece.update(elapsed_time, self.well, self.stack) + if self.stack: + self.stack.update(elapsed_time) + # TODO create control utility class for event in pygame.event.get(): if event.type == pygame.QUIT: diff --git a/entity/Piece.py b/entity/Piece.py index 9c621b3..b57c7b0 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -10,7 +10,7 @@ from util.ConfigurationManager import ConfigurationManager ''' class Piece(Entity): - GRAVITY = 800 # A move down every 800 ms + GRAVITY = 300 # A move down every 600 ms def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str): super().__init__(self.__get_points(shape, position), color, border_color) @@ -19,7 +19,7 @@ class Piece(Entity): self.previous_points = None self.previous_center = None - def update(self, elapsed_time: int, well: Entity, stack: Entity): + def update(self, elapsed_time: int, well: Entity, stack: Entity) -> None: super().update(elapsed_time) tile_size = ConfigurationManager.configuration["engine"]["tile-size"] diff --git a/entity/Stack.py b/entity/Stack.py index ee060d5..18ba45b 100644 --- a/entity/Stack.py +++ b/entity/Stack.py @@ -1,10 +1,51 @@ +from util.ConfigurationManager import ConfigurationManager from entity.Piece import Piece +from entity.Well import Well from entity.Entity import Entity class Stack(Entity): def __init__(self, color: str, border_color: str): super().__init__([], color, border_color) + self.rows_completed_count = 0 - def add_piece(self, piece: Piece): - self.points += piece.points \ No newline at end of file + def update(self, elapsed_time) -> None: + super().update(elapsed_time) + self.rows_completed_count += self.__complete_rows() + + def add_piece(self, piece: Piece) -> None: + self.points += piece.points + + def __complete_rows(self) -> int: + squares_by_row = {} + for square in self.points: + top_left_vertex = square[0] + if top_left_vertex[1] not in squares_by_row: + squares_by_row[top_left_vertex[1]] = [] + if square not in squares_by_row[top_left_vertex[1]]: + squares_by_row[top_left_vertex[1]].append(square) + + squares_to_exclude = [] + rows_completed = [] + for key in squares_by_row: + if len(squares_by_row[key]) == Well.WIDTH: + squares_to_exclude += squares_by_row[key] + rows_completed.append(key) + + if len(squares_to_exclude) == 0: + return 0 + + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + new_points = [] + for square in self.points: + if square not in squares_to_exclude: + for vertex in square: + distance_to_move = 0 + for row_completed in rows_completed: + if vertex[1] <= row_completed: + distance_to_move += 1 + vertex[1] += tile_size * distance_to_move + new_points.append(square) + self.points = new_points + + return len(rows_completed) \ No newline at end of file