From 2abe0a6658a07260b17e1693ed7902be2f2e87f4 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Tue, 22 Dec 2020 18:14:14 +0100 Subject: Show the next piece Signed-off-by: Olivier Gayot --- cameltris.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) (limited to 'cameltris.py') diff --git a/cameltris.py b/cameltris.py index f17c872..959ef37 100755 --- a/cameltris.py +++ b/cameltris.py @@ -87,19 +87,49 @@ class TPiece(Piece): self.elements[1][0], self.elements[0][1], self.elements[1][2], self.elements[2][1] = self.elements[0][1], self.elements[1][2], self.elements[2][1], self.elements[1][0] -def refresh_screen(): - screen.fill(black) +def refresh_game_canvas(): + game_canvas.fill(black) for row_idx, row in enumerate(grid): for col_idx, element in enumerate(row): if element is not None: - screen.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1)) + game_canvas.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1)) # Display the current piece for row_idx, row in enumerate(current_piece.elements): for col_idx, element in enumerate(row): if element is not None: - screen.blit(element, ((col_idx + current_piece_position[1]) * 50 + 1, (row_idx + current_piece_position[0]) * 50 + 1)) + game_canvas.blit(element, ((col_idx + current_piece_position[1]) * 50 + 1, (row_idx + current_piece_position[0]) * 50 + 1)) + +def refresh_piece_preview_canvas(): + piece_preview_canvas.fill(black) + + non_empty_rows = list() + for row in 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 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: + 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)) + screen.blit(right_pane_canvas, (501, 0)) def has_collision(y: int, x: int) -> bool: @@ -188,7 +218,7 @@ def burn_rows(): def stick_piece(): - global current_piece, current_piece_position + global current_piece, current_piece_position, next_piece, next_piece_position for row_id, row in enumerate(current_piece.elements): for col_id, element in enumerate(row): @@ -197,7 +227,9 @@ def stick_piece(): grid[row_id + current_piece_position[0]][col_id + current_piece_position[1]] = element burn_rows() - current_piece, current_piece_position = generate_piece() + current_piece, current_piece_position = next_piece, next_piece_position + next_piece, next_piece_position = generate_piece() + refresh_piece_preview_canvas() pygame.init() @@ -223,11 +255,18 @@ red_square.fill(red) yellow_square = pygame.Surface((48, 48)) yellow_square.fill(yellow) -screen = pygame.display.set_mode((500, 1000)) +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)) grid = [[None for _ in range(10)] for _ in range(20)] current_piece, current_piece_position = generate_piece() +next_piece, next_piece_position = generate_piece() drop_rate = 100 -- cgit v1.2.3