feat: add piece color to the stack

This commit is contained in:
2021-07-12 19:33:19 -04:00
parent a9527473a6
commit 406941d9d0
3 changed files with 68 additions and 15 deletions

View File

@@ -35,6 +35,9 @@ position:
well: [280, 80]
next-piece: [620, 160]
spawn-piece: [-260, -60]
# multi player game screen
well-player-1: [80, 80]
well-player-2: [480, 80]
engine:
fps: 60
@@ -58,11 +61,12 @@ color:
cursor: "#FFFFFF"
# in game
piece-1: "#1F37EC"
piece-2: "#3DBBFC"
piece-2: "#5BDB57"
piece-3: "#FFFFFF"
piece-ghost: "#9BFCF0"
piece-inner-border-1: "#1F37EC"
piece-outer-border-1: "#000000"
well-1: "#9BFCF0"
well-border-1: "#000000"
stack-1: "#747474"
stack-border-1: "#000000"
well-1: "#000000"
well-border-1: "#9BFCF0"
well-2: "#9BFCF0"
well-border-2: "#000000"

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
"""

View File

@@ -102,8 +102,7 @@ class SinglePlayerScene(Scene):
self._well = Well(ConfigurationManager.get("position", "well"),\
ConfigurationManager.get("color", "well-1"),\
ConfigurationManager.get("color", "well-border-1"))
self._stack = Stack(ConfigurationManager.get("color", "stack-1"),\
ConfigurationManager.get("color", "stack-border-1"))
self._stack = Stack()
self._change_scence = change_scene
SoundManager.play_theme_music()
@@ -171,10 +170,27 @@ class MultiPlayerScene(Scene):
def __init__(self, change_scene: FunctionType) -> None:
self._tile_size = ConfigurationManager.get("engine", "tile-size")
self._background_color = pygame.Color(ConfigurationManager.get("color", "window-bg"))
self._well_player_one = Well(ConfigurationManager.get("position", "well-player-1"),\
ConfigurationManager.get("color", "well-1"),\
ConfigurationManager.get("color", "well-border-1"))
self._well_player_two = Well(ConfigurationManager.get("position", "well-player-2"),\
ConfigurationManager.get("color", "well-1"),\
ConfigurationManager.get("color", "well-border-1"))
self._stack = Stack()
MultiplayerService.init()
def draw(self, surface: pygame.Surface) -> None:
surface.fill(self._background_color)
if self._well_player_one:
self._well_player_one.draw(surface)
if self._well_player_two:
self._well_player_two.draw(surface)
if self._stack:
self._stack.draw(surface)
def update(self, elapsed_time: int) -> None:
pass