feat: add player two functionality
This commit is contained in:
19
config.yaml
19
config.yaml
@@ -48,7 +48,8 @@ position:
|
|||||||
next-piece-player-1: [360, 160]
|
next-piece-player-1: [360, 160]
|
||||||
next-label-player-2: [340, 420]
|
next-label-player-2: [340, 420]
|
||||||
next-piece-player-2: [360, 460]
|
next-piece-player-2: [360, 460]
|
||||||
spawn-piece-player-1: [-260, -60]
|
spawn-piece-player-1: [-200, -60]
|
||||||
|
spawn-piece-player-2: [200, -360]
|
||||||
|
|
||||||
engine:
|
engine:
|
||||||
fps: 60
|
fps: 60
|
||||||
@@ -71,13 +72,17 @@ color:
|
|||||||
# title screen
|
# title screen
|
||||||
cursor: "#FFFFFF"
|
cursor: "#FFFFFF"
|
||||||
# in game
|
# in game
|
||||||
piece-1: "#1F37EC"
|
|
||||||
piece-2: "#5BDB57"
|
|
||||||
piece-3: "#FFFFFF"
|
|
||||||
piece-ghost: "#9BFCF0"
|
|
||||||
piece-inner-border-1: "#1F37EC"
|
|
||||||
piece-outer-border-1: "#000000"
|
|
||||||
well-1: "#000000"
|
well-1: "#000000"
|
||||||
well-border-1: "#9BFCF0"
|
well-border-1: "#9BFCF0"
|
||||||
well-2: "#9BFCF0"
|
well-2: "#9BFCF0"
|
||||||
well-border-2: "#000000"
|
well-border-2: "#000000"
|
||||||
|
piece-1-player-1: "#1F37EC"
|
||||||
|
piece-2-player-1: "#5BDB57"
|
||||||
|
piece-3-player-1: "#FFFFFF"
|
||||||
|
piece-inner-border-1-player-1: "#1F37EC"
|
||||||
|
piece-1-player-2: "#F83801"
|
||||||
|
piece-2-player-2: "#7F7F7F"
|
||||||
|
piece-3-player-2: "#FFFFFF"
|
||||||
|
piece-inner-border-1-player-2: "#F83801"
|
||||||
|
piece-outer-border-1: "#000000"
|
||||||
|
piece-ghost: "#9BFCF0"
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ class Piece(Entity):
|
|||||||
|
|
||||||
|
|
||||||
def draw(self, surface: pygame.Surface, well: Well = None, stack: "Stack" = None) -> None:
|
def draw(self, surface: pygame.Surface, well: Well = None, stack: "Stack" = None) -> None:
|
||||||
# ghost piecep
|
# ghost piece
|
||||||
if well and stack:
|
if well and stack:
|
||||||
for square in self._get_ghost_piece_points(well, stack):
|
for square in self._get_ghost_piece_points(well, stack):
|
||||||
pygame.draw.polygon(surface, pygame.Color(self._ghost_piece_color), 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
|
||||||
@@ -165,6 +165,10 @@ class Piece(Entity):
|
|||||||
vertex_four = (square[3][0] + (self._tile_size // 10), square[3][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)
|
new_square = (vertex_one, vertex_two, vertex_three, vertex_four)
|
||||||
pygame.draw.polygon(surface, pygame.Color(self._inner_border_color), new_square, max(self._tile_size // 6, 1))
|
pygame.draw.polygon(surface, pygame.Color(self._inner_border_color), new_square, max(self._tile_size // 6, 1))
|
||||||
|
surface.set_at((square[0][0]+3, square[0][1]+3), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+4, square[0][1]+4), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+4, square[0][1]+5), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+5, square[0][1]+4), pygame.Color(255, 255, 255))
|
||||||
|
|
||||||
def move(self, vector: Tuple) -> None:
|
def move(self, vector: Tuple) -> None:
|
||||||
self._previous_points = copy.deepcopy(self._points)
|
self._previous_points = copy.deepcopy(self._points)
|
||||||
@@ -306,6 +310,10 @@ class Stack(Entity):
|
|||||||
vertex_four = (square[3][0] + (self._tile_size // 10), square[3][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)
|
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))
|
pygame.draw.polygon(surface, pygame.Color(square_design.inner_color), new_square, max(self._tile_size // 6, 1))
|
||||||
|
surface.set_at((square[0][0]+3, square[0][1]+3), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+4, square[0][1]+4), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+4, square[0][1]+5), pygame.Color(255, 255, 255))
|
||||||
|
surface.set_at((square[0][0]+5, square[0][1]+4), pygame.Color(255, 255, 255))
|
||||||
|
|
||||||
def add_piece(self, piece: Piece) -> None:
|
def add_piece(self, piece: Piece) -> None:
|
||||||
self._points += piece._points
|
self._points += piece._points
|
||||||
@@ -372,12 +380,12 @@ class PieceGenerator:
|
|||||||
_bucket_two = []
|
_bucket_two = []
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_piece(cls, position: Tuple, player_two: bool = False) -> Piece:
|
def get_piece(cls, position: Tuple, is_player_two: bool = False) -> Piece:
|
||||||
bucket = cls._bucket_one if not player_two else cls._bucket_two
|
bucket = cls._bucket_one if not is_player_two else cls._bucket_two
|
||||||
if len(bucket) == 0:
|
if len(bucket) == 0:
|
||||||
cls._generate_bucket(bucket)
|
cls._generate_bucket(bucket)
|
||||||
|
|
||||||
base_color, inner_border_color, outer_border_color = cls._get_piece_color()
|
base_color, inner_border_color, outer_border_color = cls._get_piece_color(is_player_two)
|
||||||
return Piece(cls._get_piece_shape(bucket.pop()), position, base_color, inner_border_color, outer_border_color)
|
return Piece(cls._get_piece_shape(bucket.pop()), position, base_color, inner_border_color, outer_border_color)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -405,11 +413,12 @@ class PieceGenerator:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _get_piece_color() -> Tuple:
|
def _get_piece_color(is_player_two: bool) -> Tuple:
|
||||||
random_number = random.randint(1, 3)
|
random_number = random.randint(1, 3)
|
||||||
|
player_mod = "player-1" if not is_player_two else "player-2"
|
||||||
|
|
||||||
base_color = ConfigurationManager.get("color", "piece-" + str(random_number))
|
base_color = ConfigurationManager.get("color", "piece-" + str(random_number) + "-" + player_mod)
|
||||||
inner_border_color = None if random_number != 3 else ConfigurationManager.get("color", "piece-inner-border-1")
|
inner_border_color = None if random_number != 3 else ConfigurationManager.get("color", "piece-inner-border-1" + "-" + player_mod)
|
||||||
outer_border_color = ConfigurationManager.get("color", "piece-outer-border-1")
|
outer_border_color = ConfigurationManager.get("color", "piece-outer-border-1")
|
||||||
|
|
||||||
return (base_color, inner_border_color, outer_border_color)
|
return (base_color, inner_border_color, outer_border_color)
|
||||||
@@ -72,7 +72,7 @@ class TitleScene(Scene):
|
|||||||
self._cursor_off = not self._cursor_off
|
self._cursor_off = not self._cursor_off
|
||||||
|
|
||||||
if Controller.key_pressed(pygame.K_RETURN):
|
if Controller.key_pressed(pygame.K_RETURN):
|
||||||
self._change_scence(SinglePlayerScene(self._change_scence) if not self._is_multiplayer else MultiPlayerScene(self._change_scence))
|
self._change_scence(SinglePlayerScene(self._change_scence) if not self._is_multiplayer else MultiplayerScene(self._change_scence))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
TODO
|
TODO
|
||||||
@@ -165,7 +165,7 @@ class SinglePlayerScene(Scene):
|
|||||||
"""
|
"""
|
||||||
TODO
|
TODO
|
||||||
"""
|
"""
|
||||||
class MultiPlayerScene(Scene):
|
class MultiplayerScene(Scene):
|
||||||
|
|
||||||
def __init__(self, change_scene: FunctionType) -> None:
|
def __init__(self, change_scene: FunctionType) -> None:
|
||||||
self._tile_size = ConfigurationManager.get("engine", "tile-size")
|
self._tile_size = ConfigurationManager.get("engine", "tile-size")
|
||||||
@@ -202,10 +202,11 @@ class MultiPlayerScene(Scene):
|
|||||||
self._next_piece_player_one_pos = ConfigurationManager.get("position", "next-piece-player-1")
|
self._next_piece_player_one_pos = ConfigurationManager.get("position", "next-piece-player-1")
|
||||||
self._next_piece_player_two_pos = ConfigurationManager.get("position", "next-piece-player-2")
|
self._next_piece_player_two_pos = ConfigurationManager.get("position", "next-piece-player-2")
|
||||||
self._spawn_piece_shift_player_one = ConfigurationManager.get("position", "spawn-piece-player-1")
|
self._spawn_piece_shift_player_one = ConfigurationManager.get("position", "spawn-piece-player-1")
|
||||||
|
self._spawn_piece_shift_player_two = ConfigurationManager.get("position", "spawn-piece-player-2")
|
||||||
|
|
||||||
# entities
|
# entities
|
||||||
self._next_piece_player_one = PieceGenerator.get_piece(self._next_piece_player_one_pos)
|
self._next_piece_player_one = PieceGenerator.get_piece(self._next_piece_player_one_pos)
|
||||||
self._next_piece_player_two = PieceGenerator.get_piece(self._next_piece_player_two_pos)
|
self._next_piece_player_two = PieceGenerator.get_piece(self._next_piece_player_two_pos, True)
|
||||||
self._current_piece_player_one = None
|
self._current_piece_player_one = None
|
||||||
self._current_piece_player_two = None
|
self._current_piece_player_two = None
|
||||||
|
|
||||||
@@ -223,9 +224,9 @@ class MultiPlayerScene(Scene):
|
|||||||
|
|
||||||
# pieces
|
# pieces
|
||||||
if self._current_piece_player_one is not None:
|
if self._current_piece_player_one is not None:
|
||||||
self._current_piece_player_one.draw(surface)
|
self._current_piece_player_one.draw(surface, self._well_player_one, self._stack_player_one)
|
||||||
if self._current_piece_player_two is not None:
|
if self._current_piece_player_two is not None:
|
||||||
self._current_piece_player_two.draw(surface)
|
self._current_piece_player_two.draw(surface, self._well_player_two, self._stack_player_two)
|
||||||
if self._next_piece_player_one is not None:
|
if self._next_piece_player_one is not None:
|
||||||
self._next_piece_player_one.draw(surface)
|
self._next_piece_player_one.draw(surface)
|
||||||
if self._next_piece_player_two is not None:
|
if self._next_piece_player_two is not None:
|
||||||
@@ -252,12 +253,19 @@ class MultiPlayerScene(Scene):
|
|||||||
TextGenerator.draw("P2 NXT", self._next_label_player_two_pos, surface)
|
TextGenerator.draw("P2 NXT", self._next_label_player_two_pos, surface)
|
||||||
|
|
||||||
def update(self, elapsed_time: int) -> None:
|
def update(self, elapsed_time: int) -> None:
|
||||||
|
self._update_piece_player_one(elapsed_time)
|
||||||
|
self._update_piece_player_two(elapsed_time)
|
||||||
|
|
||||||
|
if self._stack_player_one:
|
||||||
|
self._stack_player_one.update(elapsed_time)
|
||||||
|
|
||||||
|
def _update_piece_player_one(self, elapsed_time: int) -> None:
|
||||||
if self._current_piece_player_one is not None:
|
if self._current_piece_player_one is not None:
|
||||||
self._current_piece_player_one.update(elapsed_time,\
|
self._current_piece_player_one.update(elapsed_time,\
|
||||||
self._well_player_one,\
|
self._well_player_one,\
|
||||||
self._stack_player_one,\
|
self._stack_player_one,\
|
||||||
self._get_level(),\
|
self._get_level_player_one(),\
|
||||||
self._clear_current_piece)
|
self._clear_current_piece_player_one)
|
||||||
else:
|
else:
|
||||||
self._current_piece_player_one = self._next_piece_player_one
|
self._current_piece_player_one = self._next_piece_player_one
|
||||||
self._current_piece_player_one.move(self._spawn_piece_shift_player_one)
|
self._current_piece_player_one.move(self._spawn_piece_shift_player_one)
|
||||||
@@ -269,8 +277,32 @@ class MultiPlayerScene(Scene):
|
|||||||
MultiplayerService.quit()
|
MultiplayerService.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def _get_level(self) -> int:
|
def _update_piece_player_two(self, elapsed_time: int) -> None:
|
||||||
|
if self._current_piece_player_two is not None:
|
||||||
|
self._current_piece_player_two.update(elapsed_time,\
|
||||||
|
self._well_player_two,\
|
||||||
|
self._stack_player_two,\
|
||||||
|
self._get_level_player_two(),\
|
||||||
|
self._clear_current_piece_player_two)
|
||||||
|
else:
|
||||||
|
self._current_piece_player_two = self._next_piece_player_two
|
||||||
|
self._current_piece_player_two.move(self._spawn_piece_shift_player_two)
|
||||||
|
self._next_piece_player_two = PieceGenerator.get_piece(self._next_piece_player_two_pos, True)
|
||||||
|
|
||||||
|
# TODO create game over scene
|
||||||
|
if self._stack_player_two and self._current_piece_player_two.collide(self._stack_player_two):
|
||||||
|
pygame.quit()
|
||||||
|
MultiplayerService.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def _get_level_player_one(self) -> int:
|
||||||
return 0 if self._stack_player_one is None else self._stack_player_one.total_lines // self._lines_per_level
|
return 0 if self._stack_player_one is None else self._stack_player_one.total_lines // self._lines_per_level
|
||||||
|
|
||||||
def _clear_current_piece(self) -> None:
|
def _get_level_player_two(self) -> int:
|
||||||
|
return 0 if self._stack_player_two is None else self._stack_player_two.total_lines // self._lines_per_level
|
||||||
|
|
||||||
|
def _clear_current_piece_player_one(self) -> None:
|
||||||
self._current_piece_player_one = None
|
self._current_piece_player_one = None
|
||||||
|
|
||||||
|
def _clear_current_piece_player_two(self) -> None:
|
||||||
|
self._current_piece_player_two = None
|
||||||
|
|||||||
Reference in New Issue
Block a user