From 6dc63f9c214ed0c5ecdb03feb93561a742c58a2e Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 24 Dec 2020 21:16:46 +0100 Subject: Pretend we can have multiple players now Signed-off-by: Olivier Gayot --- cameltris.py | 184 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 94 insertions(+), 90 deletions(-) diff --git a/cameltris.py b/cameltris.py index 2c82aa9..4f4e42e 100755 --- a/cameltris.py +++ b/cameltris.py @@ -135,65 +135,28 @@ class TPiece(Piece): self.elements = (None, None, None), (self.square, self.square, self.square), (None, self.square, None) -def refresh_game_canvas(): - global player - - game_canvas.fill(black) - - for row_idx, row in enumerate(player.grid): - for col_idx, element in enumerate(row): - if element is not None: - game_canvas.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1)) - - # Display the current piece - for row_idx, row in enumerate(player.current_piece.elements): - for col_idx, element in enumerate(row): - if element is not None: - game_canvas.blit(element, ((col_idx + player.current_piece_position[1]) * 50 + 1, (row_idx + player.current_piece_position[0]) * 50 + 1)) - def refresh_right_pane_canvas(): - level_canvas.fill(black) - score_canvas.fill(black) + for player in players: + player.level_canvas.fill(black) + player.score_canvas.fill(black) if pygame.font: score_font = pygame.font.Font(None, 56) - score_canvas.blit(score_font.render(f"{player.score:08d}", True, white), (0, 0)) - level_canvas.blit(score_font.render(f"{player.level:08d}", True, white), (0, 0)) - right_pane_canvas.blit(score_canvas, (2, 10)) - right_pane_canvas.blit(level_canvas, (2, 70)) - - -def refresh_piece_preview_canvas(): - piece_preview_canvas.fill(black) - - non_empty_rows = list() - for row in player.next_piece.elements: - if any(map(lambda element: element is not None, row)): - non_empty_rows.append(row) + player.score_canvas.blit(score_font.render(f"{player.score:08d}", True, white), (0, 0)) + player.level_canvas.blit(score_font.render(f"{player.level:08d}", True, white), (0, 0)) + right_pane_canvas.blit(player.score_canvas, (2, 10)) + right_pane_canvas.blit(player.level_canvas, (2, 70)) - non_empty_cols = set() - for row in player.next_piece.elements: - for col_id, element in enumerate(row): - if element is not None: - non_empty_cols.add(col_id) + right_pane_canvas.blit(player.piece_preview_canvas, (50, 200)) - y_offset = (4 - len(non_empty_rows)) / 2 - x_offset = (4 - len(non_empty_cols)) / 2 - - # Display the next piece - for row_idx, row in enumerate(non_empty_rows): - for col_idx, element in enumerate(row): - if element is not None: - piece_preview_canvas.blit(element, ((col_idx + x_offset) * 50 + 1, (row_idx + y_offset) * 50 + 1)) - - right_pane_canvas.blit(piece_preview_canvas, (50, 200)) def refresh_screen(): - refresh_game_canvas() - screen.blit(game_canvas, (0, 0)) - refresh_right_pane_canvas() - screen.blit(right_pane_canvas, (501, 0)) + for player in players: + player.refresh_grid_canvas() + screen.blit(player.grid_canvas, (0, 0)) + refresh_right_pane_canvas() + screen.blit(right_pane_canvas, (501, 0)) class Player: @@ -214,6 +177,12 @@ class Player: self.piece_drop_frames = 0 + self.grid_canvas = pygame.Surface((500, 1000)) + self.piece_preview_canvas = pygame.Surface((200, 200)) + self.score_canvas = pygame.Surface((296, 50)) + self.level_canvas = pygame.Surface((296, 50)) + + def generate_piece(self): # We may want to make this a function outside the class piece = random.choice((TPiece, SPiece, IPiece, ZPiece, SquarePiece, LPiece, JPiece))() @@ -272,7 +241,7 @@ class Player: self.level += 1 self.current_piece, self.current_piece_position = self.next_piece, self.next_piece_position self.next_piece, self.next_piece_position = self.generate_piece() - refresh_piece_preview_canvas() + self.refresh_piece_preview_canvas() def has_collision(self, y: int, x: int) -> bool: try: @@ -360,6 +329,43 @@ class Player: if self.controller.get_input_up(event) == Input.MOVE_DOWN: self.pressing_down_countdown = None + def refresh_piece_preview_canvas(self): + self.piece_preview_canvas.fill(black) + + non_empty_rows = list() + for row in self.next_piece.elements: + if any(map(lambda element: element is not None, row)): + non_empty_rows.append(row) + + non_empty_cols = set() + for row in self.next_piece.elements: + for col_id, element in enumerate(row): + if element is not None: + non_empty_cols.add(col_id) + + y_offset = (4 - len(non_empty_rows)) / 2 + x_offset = (4 - len(non_empty_cols)) / 2 + + # Display the next piece + for row_idx, row in enumerate(non_empty_rows): + for col_idx, element in enumerate(row): + if element is not None: + self.piece_preview_canvas.blit(element, ((col_idx + x_offset) * 50 + 1, (row_idx + y_offset) * 50 + 1)) + + def refresh_grid_canvas(self): + self.grid_canvas.fill(black) + + for row_idx, row in enumerate(self.grid): + for col_idx, element in enumerate(row): + if element is not None: + self.grid_canvas.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1)) + + # Display the current piece + for row_idx, row in enumerate(self.current_piece.elements): + for col_idx, element in enumerate(row): + if element is not None: + self.grid_canvas.blit(element, ((col_idx + self.current_piece_position[1]) * 50 + 1, (row_idx + self.current_piece_position[0]) * 50 + 1)) + PARSER = argparse.ArgumentParser() @@ -386,14 +392,9 @@ square_template = pygame.Surface((48, 48)) screen = pygame.display.set_mode((801, 1000)) -game_canvas = pygame.Surface((500, 1000)) right_pane_canvas = pygame.Surface((300, 1000)) right_pane_canvas.fill((255, 255, 255)) -piece_preview_canvas = pygame.Surface((200, 200)) -score_canvas = pygame.Surface((296, 50)) -level_canvas = pygame.Surface((296, 50)) - if ARGS["joystick_id"] is not None: joystick = pygame.joystick.Joystick(ARGS["joystick_id"]) @@ -402,9 +403,11 @@ if ARGS["joystick_id"] is not None: else: controller = KeyboardController(pygame.key) -player = Player(controller) +# Just one player +players = [Player(controller)] -refresh_piece_preview_canvas() +for player in players: + player.refresh_piece_preview_canvas() # Number of frames frames_per_gridcell = [48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1] @@ -412,16 +415,15 @@ frames_per_gridcell = [48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3 clock = pygame.time.Clock() def handle_input_pressed(instance, event): - if not isinstance(player.controller, instance): - return + for player in players: + if isinstance(player.controller, instance): + player.handle_input_pressed(event) - player.handle_input_pressed(event) def handle_input_released(instance, event): - if not isinstance(player.controller, instance): - return - - player.handle_input_released(event) + for player in players: + if isinstance(player.controller, instance): + player.handle_input_released(event) event_handler = dict() event_handler[pygame.QUIT] = lambda _: sys.exit() @@ -431,36 +433,38 @@ event_handler[pygame.JOYBUTTONDOWN] = partial(handle_input_pressed, JoystickCont event_handler[pygame.JOYBUTTONUP] = partial(handle_input_released, JoystickController) while True: - player.piece_drop_frames += 1 + for player in players: + player.piece_drop_frames += 1 for event in pygame.event.get(): with contextlib.suppress(KeyError): event_handler[event.type](event) - player.das += 1 - if player.das == 16: - with contextlib.suppress(WouldCollide): - if controller.is_pressed(Input.MOVE_RIGHT): - player.move_piece_right() - if controller.is_pressed(Input.MOVE_LEFT): - player.move_piece_left() - player.das = 10 - - if player.pressing_down_countdown == 0: - try: - player.move_piece_down() - except WouldCollide: - player.stick_piece() - player.pressing_down_countdown = 2 - elif player.pressing_down_countdown is not None: - player.pressing_down_countdown -= 1 - - if player.piece_drop_frames >= frames_per_gridcell[player.level - 1]: - player.piece_drop_frames = 0 - try: - player.move_piece_down() - except WouldCollide: - player.stick_piece() + for player in players: + player.das += 1 + if player.das == 16: + with contextlib.suppress(WouldCollide): + if controller.is_pressed(Input.MOVE_RIGHT): + player.move_piece_right() + if controller.is_pressed(Input.MOVE_LEFT): + player.move_piece_left() + player.das = 10 + + if player.pressing_down_countdown == 0: + try: + player.move_piece_down() + except WouldCollide: + player.stick_piece() + player.pressing_down_countdown = 2 + elif player.pressing_down_countdown is not None: + player.pressing_down_countdown -= 1 + + if player.piece_drop_frames >= frames_per_gridcell[player.level - 1]: + player.piece_drop_frames = 0 + try: + player.move_piece_down() + except WouldCollide: + player.stick_piece() refresh_screen() pygame.display.update() -- cgit v1.2.3