feat: implement line completion logic
This commit is contained in:
@@ -45,6 +45,9 @@ class Tetris:
|
|||||||
if self.current_piece:
|
if self.current_piece:
|
||||||
self.current_piece.update(elapsed_time, self.well, self.stack)
|
self.current_piece.update(elapsed_time, self.well, self.stack)
|
||||||
|
|
||||||
|
if self.stack:
|
||||||
|
self.stack.update(elapsed_time)
|
||||||
|
|
||||||
# TODO create control utility class
|
# TODO create control utility class
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from util.ConfigurationManager import ConfigurationManager
|
|||||||
'''
|
'''
|
||||||
class Piece(Entity):
|
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):
|
def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str):
|
||||||
super().__init__(self.__get_points(shape, position), color, border_color)
|
super().__init__(self.__get_points(shape, position), color, border_color)
|
||||||
@@ -19,7 +19,7 @@ class Piece(Entity):
|
|||||||
self.previous_points = None
|
self.previous_points = None
|
||||||
self.previous_center = 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)
|
super().update(elapsed_time)
|
||||||
|
|
||||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||||
|
|||||||
@@ -1,10 +1,51 @@
|
|||||||
|
from util.ConfigurationManager import ConfigurationManager
|
||||||
from entity.Piece import Piece
|
from entity.Piece import Piece
|
||||||
|
from entity.Well import Well
|
||||||
from entity.Entity import Entity
|
from entity.Entity import Entity
|
||||||
|
|
||||||
class Stack(Entity):
|
class Stack(Entity):
|
||||||
|
|
||||||
def __init__(self, color: str, border_color: str):
|
def __init__(self, color: str, border_color: str):
|
||||||
super().__init__([], color, border_color)
|
super().__init__([], color, border_color)
|
||||||
|
self.rows_completed_count = 0
|
||||||
|
|
||||||
def add_piece(self, piece: Piece):
|
def update(self, elapsed_time) -> None:
|
||||||
self.points += piece.points
|
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)
|
||||||
Reference in New Issue
Block a user