summaryrefslogtreecommitdiff
path: root/cameltris.py
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-12-21 20:00:02 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-12-21 20:00:02 +0100
commitb3ac202ff7ebf11e9c72d51ab3a732a325986777 (patch)
tree14dcf62a72970a13bf767aa898d269a02095cc08 /cameltris.py
parent64363205f7d93e845a7579c503039501e6d7778a (diff)
Allow to move a piece on the screen
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'cameltris.py')
-rwxr-xr-xcameltris.py39
1 files changed, 34 insertions, 5 deletions
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)