diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-12-21 23:03:49 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-12-21 23:03:49 +0100 |
commit | 477823b2f5ddcc861545992e7d60cc00043aa218 (patch) | |
tree | 3b84a05ebc98db3208087441278ceb6e160999f1 /cameltris.py | |
parent | 6e848e62a2a2be4a5e9ce6c4664656a383e61368 (diff) |
Handle collisions
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'cameltris.py')
-rwxr-xr-x | cameltris.py | 87 |
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() |