summaryrefslogtreecommitdiff
path: root/cameltris.py
diff options
context:
space:
mode:
Diffstat (limited to 'cameltris.py')
-rwxr-xr-xcameltris.py87
1 files changed, 75 insertions, 12 deletions
diff --git a/cameltris.py b/cameltris.py
index eefd111..a5fd6e5 100755
--- a/cameltris.py
+++ b/cameltris.py
@@ -1,11 +1,16 @@
#!/usr/bin/env python3
+import contextlib
import sys
import time
import pygame
+class WouldCollide(Exception):
+ pass
+
+
class Piece():
def rotate(self):
pass
@@ -95,6 +100,63 @@ def refresh_screen():
if element is not None:
screen.blit(element, ((col_idx + current_piece_position[1]) * 50 + 1, (row_idx + current_piece_position[0]) * 50 + 1))
+
+def has_collision(y: int, x: int) -> bool:
+ try:
+ for row_id, row in enumerate(current_piece.elements):
+ for col_id, element in enumerate(row):
+ if element is None:
+ continue
+
+ if col_id + x < 0:
+ return True
+
+ if grid[row_id + y][col_id + x] is not None:
+ return True
+
+ except IndexError:
+ return True
+
+ return False
+
+def move_piece_down():
+ if not has_collision(current_piece_position[0] + 1, current_piece_position[1]):
+ current_piece_position[0] += 1
+ else:
+ raise WouldCollide()
+
+def move_piece_up():
+ if not has_collision(current_piece_position[0] - 1, current_piece_position[1]):
+ current_piece_position[0] -= 1
+ else:
+ raise WouldCollide()
+
+def move_piece_left():
+ if not has_collision(current_piece_position[0], current_piece_position[1] - 1):
+ current_piece_position[1] -= 1
+ else:
+ raise WouldCollide()
+
+def move_piece_right():
+ if not has_collision(current_piece_position[0], current_piece_position[1] + 1):
+ current_piece_position[1] += 1
+ else:
+ raise WouldCollide()
+
+def rotate_piece_counter_clockwise():
+ current_piece.rotate_counter_clockwise()
+
+ if has_collision(current_piece_position[0], current_piece_position[1]):
+ current_piece.rotate_clockwise()
+ raise WouldCollide()
+
+def rotate_piece_clockwise():
+ current_piece.rotate_clockwise()
+
+ if has_collision(current_piece_position[0], current_piece_position[1]):
+ current_piece.rotate_counter_clockwise()
+ raise WouldCollide()
+
pygame.init()
black = (0, 0, 0)
@@ -142,18 +204,19 @@ while True:
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_ESCAPE, pygame.K_q):
sys.exit()
- 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
- if event.key == pygame.K_s:
- current_piece.rotate_clockwise()
- if event.key == pygame.K_d:
- current_piece.rotate_counter_clockwise()
+ with contextlib.suppress(WouldCollide):
+ if event.key in (pygame.K_DOWN, pygame.K_j):
+ move_piece_down()
+ if event.key in (pygame.K_UP, pygame.K_k):
+ move_piece_up()
+ if event.key in (pygame.K_RIGHT, pygame.K_l):
+ move_piece_right()
+ if event.key in (pygame.K_LEFT, pygame.K_h):
+ move_piece_left()
+ if event.key == pygame.K_s:
+ rotate_piece_clockwise()
+ if event.key == pygame.K_d:
+ rotate_piece_counter_clockwise()
refresh_screen()
pygame.display.flip()