feat: add piece color to the stack

This commit is contained in:
Giovani
2021-07-12 19:33:19 -04:00
parent 9cf6c7def5
commit 90c397b093
3 changed files with 68 additions and 15 deletions

View File

@@ -25,8 +25,9 @@ class Entity:
def draw(self, surface: pygame.Surface) -> None:
for square in self._points:
pygame.draw.polygon(surface, pygame.Color(self._color), square, 0)
if self._border_color:
if self._color is not None:
pygame.draw.polygon(surface, pygame.Color(self._color), square, 0)
if self._border_color is not None:
pygame.draw.polygon(surface, pygame.Color(self._border_color), square, max(self._tile_size // 6, 1))
def collide(self, entity: "Entity") -> bool: # TODO figure out how to do type hint for entity param of type Entity
@@ -85,6 +86,7 @@ class Piece(Entity):
def __init__(self, shape: Tuple, position: Tuple, color: str, inner_border_color: str, border_color: str):
super().__init__(Piece._get_points(shape, position), color, border_color)
self._inner_border_color = inner_border_color
self._ghost_piece_color = ConfigurationManager.get("color", "piece-ghost")
self._center = self._get_center(shape, position)
self._previous_points = None
self._previous_center = None
@@ -148,10 +150,10 @@ class Piece(Entity):
def draw(self, surface: pygame.Surface, well: Well = None, stack: "Stack" = None) -> None:
# ghost piece
# ghost piecep
if well and stack:
for square in self._get_ghost_piece_points(well, stack):
pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(self._tile_size // 6, 1)) # TODO add white to the yaml
pygame.draw.polygon(surface, pygame.Color(self._ghost_piece_color), square, max(self._tile_size // 6, 1)) # TODO add white to the yaml
super().draw(surface)
# inner border piece
@@ -278,18 +280,36 @@ class Piece(Entity):
"""
class Stack(Entity):
def __init__(self, color: str, border_color: str):
super().__init__([], color, border_color)
def __init__(self):
super().__init__([], None, None)
self._square_designs = []
self.total_lines = 0
self.lines_completed_last = 0
def update(self, elapsed_time: int) -> None: # TODO remove scene argument
def update(self, elapsed_time: int) -> None:
super().update(elapsed_time)
self.lines_completed_last = self._complete_rows()
self.total_lines += self.lines_completed_last
def draw(self, surface: pygame.Surface) -> None:
for i in range(len(self._points)):
square = self._points[i]
square_design = self._square_designs[i]
if square_design.base_color is not None:
pygame.draw.polygon(surface, pygame.Color(square_design.base_color), square, 0)
if square_design.outer_color is not None:
pygame.draw.polygon(surface, pygame.Color(square_design.outer_color), square, max(self._tile_size // 6, 1))
if square_design.inner_color is not None:
vertex_one = (square[0][0] + (self._tile_size // 10), square[0][1] + (self._tile_size // 10))
vertex_two = (square[1][0] - (self._tile_size // 10), square[1][1] + (self._tile_size // 10))
vertex_three = (square[2][0] - (self._tile_size // 10), square[2][1] - (self._tile_size // 10))
vertex_four = (square[3][0] + (self._tile_size // 10), square[3][1] - (self._tile_size // 10))
new_square = (vertex_one, vertex_two, vertex_three, vertex_four)
pygame.draw.polygon(surface, pygame.Color(square_design.inner_color), new_square, max(self._tile_size // 6, 1))
def add_piece(self, piece: Piece) -> None:
self._points += piece._points
self._square_designs += [_SquareDesign(piece._color, piece._inner_border_color, piece._border_color) for _ in range(len(piece._points))]
# TODO refactor into multiple functions
def _complete_rows(self) -> int:
@@ -317,7 +337,9 @@ class Stack(Entity):
SoundManager.play_line_complete_sfx()
new_points = []
for square in self._points:
new_square_designs = []
for i in range(len(self._points)):
square = self._points[i]
if square not in squares_to_exclude:
for vertex in square:
distance_to_move = sum(
@@ -326,10 +348,21 @@ class Stack(Entity):
)
vertex[1] += self._tile_size * distance_to_move
new_points.append(square)
new_square_designs.append(self._square_designs[i])
self._points = new_points
self._square_designs = new_square_designs
return len(rows_completed)
"""
TODO description
"""
class _SquareDesign:
def __init__(self, base_color: str, inner_color: str, outer_color: str):
self.base_color = base_color
self.inner_color = inner_color
self.outer_color = outer_color
"""
TODO description
"""