diff options
Diffstat (limited to 'cameltris.py')
-rwxr-xr-x | cameltris.py | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/cameltris.py b/cameltris.py index b850685..2c82aa9 100755 --- a/cameltris.py +++ b/cameltris.py @@ -3,6 +3,7 @@ import argparse import contextlib import enum +from functools import partial import random import sys import time @@ -16,6 +17,10 @@ class WouldCollide(Exception): pass +class PlayerQuit(Exception): + pass + + class Piece(): def __init__(self): self.square = square_template.copy() @@ -324,10 +329,37 @@ class Player: def rotate_piece_clockwise(self): self.current_piece.rotate_clockwise() - if has_collision(self.current_piece_position[0], self.current_piece_position[1]): + if self.has_collision(self.current_piece_position[0], self.current_piece_position[1]): self.current_piece.rotate_counter_clockwise() raise WouldCollide() + def handle_input_pressed(self, event): + if self.controller.get_input_down(event) == Input.QUIT: + raise PlayerQuit() + + with contextlib.suppress(WouldCollide): + if self.controller.get_input_down(event) == Input.MOVE_RIGHT: + self.move_piece_right() + self.das = 0 + if self.controller.get_input_down(event) == Input.MOVE_LEFT: + self.move_piece_left() + self.das = 0 + if self.controller.get_input_down(event) == Input.ROTATE_CLOCKWISE: + self.rotate_piece_clockwise() + if self.controller.get_input_down(event) == Input.ROTATE_COUNTER_CLOCKWISE: + self.rotate_piece_counter_clockwise() + if self.controller.get_input_down(event) == Input.MOVE_DOWN: + self.piece_drop_frames = 0 + self.pressing_down_countdown = 3 + try: + self.move_piece_down() + except WouldCollide: + self.stick_piece() + + def handle_input_released(self, event): + if self.controller.get_input_up(event) == Input.MOVE_DOWN: + self.pressing_down_countdown = None + PARSER = argparse.ArgumentParser() @@ -379,37 +411,31 @@ frames_per_gridcell = [48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3 clock = pygame.time.Clock() +def handle_input_pressed(instance, event): + if not isinstance(player.controller, instance): + return + + player.handle_input_pressed(event) + +def handle_input_released(instance, event): + if not isinstance(player.controller, instance): + return + + player.handle_input_released(event) + +event_handler = dict() +event_handler[pygame.QUIT] = lambda _: sys.exit() +event_handler[pygame.KEYDOWN] = partial(handle_input_pressed, KeyboardController) +event_handler[pygame.KEYUP] = partial(handle_input_released, KeyboardController) +event_handler[pygame.JOYBUTTONDOWN] = partial(handle_input_pressed, JoystickController) +event_handler[pygame.JOYBUTTONUP] = partial(handle_input_released, JoystickController) while True: player.piece_drop_frames += 1 for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - elif event.type == controller.downevent: - if controller.get_input_down(event) == Input.QUIT: - sys.exit() - with contextlib.suppress(WouldCollide): - if controller.get_input_down(event) == Input.MOVE_RIGHT: - player.move_piece_right() - player.das = 0 - if controller.get_input_down(event) == Input.MOVE_LEFT: - player.move_piece_left() - player.das = 0 - if controller.get_input_down(event) == Input.ROTATE_CLOCKWISE: - player.rotate_piece_clockwise() - if controller.get_input_down(event) == Input.ROTATE_COUNTER_CLOCKWISE: - player.rotate_piece_counter_clockwise() - if controller.get_input_down(event) == Input.MOVE_DOWN: - player.piece_drop_frames = 0 - player.pressing_down_countdown = 3 - try: - player.move_piece_down() - except WouldCollide: - player.stick_piece() - elif event.type == controller.upevent: - if controller.get_input_up(event) == Input.MOVE_DOWN: - player.pressing_down_countdown = None + with contextlib.suppress(KeyError): + event_handler[event.type](event) player.das += 1 if player.das == 16: |