From b3ac202ff7ebf11e9c72d51ab3a732a325986777 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 21 Dec 2020 20:00:02 +0100 Subject: Allow to move a piece on the screen Signed-off-by: Olivier Gayot --- cameltris.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'cameltris.py') diff --git a/cameltris.py b/cameltris.py index fc9672f..cac81f2 100755 --- a/cameltris.py +++ b/cameltris.py @@ -6,29 +6,58 @@ import time import pygame +def zpiece(): + return ((blue_square, blue_square, None), (None, blue_square, blue_square)) + + def refresh_screen(): + screen.fill(black) + for row_idx, row in enumerate(grid): for col_idx, element in enumerate(row): if element is not None: - screen.blit(white_piece, (col_idx * 50 + 1, row_idx * 50 + 1)) + screen.blit(element, (col_idx * 50 + 1, row_idx * 50 + 1)) + + # Display the current piece + for row_idx, row in enumerate(current_piece): + 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)) pygame.init() black = (0, 0, 0) white = (0xff, 0xff, 0xff) +blue = (0x10, 0x20, 0xbb) + +white_square = pygame.Surface((48, 48)) +white_square.fill(white) -white_piece = pygame.Surface((48, 48)) -white_piece.fill(white) +blue_square = pygame.Surface((48, 48)) +blue_square.fill(blue) screen = pygame.display.set_mode((500, 1000)) -grid = [[True for _ in range(10)] for _ in range(20)] +grid = [[None for _ in range(10)] for _ in range(20)] + +# Let's start with a Z piece +current_piece = zpiece() +current_piece_position = [0, 4] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key in (pygame.K_DOWN, pygame.K_j): + current_piece_position[0] += 1 + if event.key in (pygame.K_UP, pygame.K_k): + current_piece_position[0] -= 1 + if event.key in (pygame.K_RIGHT, pygame.K_l): + current_piece_position[1] += 1 + if event.key in (pygame.K_LEFT, pygame.K_h): + current_piece_position[1] -= 1 - time.sleep(.1) refresh_screen() pygame.display.flip() + time.sleep(.1) -- cgit v1.2.3