diff options
Diffstat (limited to 'cameltris.py')
-rwxr-xr-x | cameltris.py | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/cameltris.py b/cameltris.py index 8ece28d..542e852 100755 --- a/cameltris.py +++ b/cameltris.py @@ -9,6 +9,8 @@ import time import pygame +from pycameltris.controller import KeyboardController, JoystickController, Input + class PS3Controller(enum.Enum): CROSS = 0 CIRCLE = 1 @@ -377,6 +379,10 @@ pressing_down_countdown = None if ARGS["joystick_id"] is not None: joystick = pygame.joystick.Joystick(ARGS["joystick_id"]) joystick.init() + controller = JoystickController(joystick) +else: + controller = KeyboardController(pygame.key) + while True: piece_drop_frames += 1 @@ -384,62 +390,37 @@ 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_ESCAPE, pygame.K_q): + elif event.type == controller.downevent: + if controller.get_input_down(event) == Input.QUIT: sys.exit() with contextlib.suppress(WouldCollide): - if event.key in (pygame.K_RIGHT, pygame.K_l): - move_piece_right() - das = 0 - if event.key in (pygame.K_LEFT, pygame.K_h): - move_piece_left() - das = 0 - if event.key == pygame.K_s: - rotate_piece_clockwise() - if event.key == pygame.K_d: - rotate_piece_counter_clockwise() - if event.key in (pygame.K_DOWN, pygame.K_j): - piece_drop_frames = 0 - pressing_down_countdown = 3 - try: - move_piece_down() - except WouldCollide: - stick_piece() - elif event.type == pygame.KEYUP: - if event.key in (pygame.K_DOWN, pygame.K_j): - pressing_down_countdown = None - - elif event.type == pygame.JOYBUTTONDOWN: - with contextlib.suppress(WouldCollide): - if event.button == PS3Controller.RIGHT.value: + if controller.get_input_down(event) == Input.MOVE_RIGHT: move_piece_right() das = 0 - if event.button == PS3Controller.LEFT.value: + if controller.get_input_down(event) == Input.MOVE_LEFT: move_piece_left() das = 0 - if event.button == PS3Controller.CROSS.value: + if controller.get_input_down(event) == Input.ROTATE_CLOCKWISE: rotate_piece_clockwise() - if event.button == PS3Controller.CIRCLE.value: + if controller.get_input_down(event) == Input.ROTATE_COUNTER_CLOCKWISE: rotate_piece_counter_clockwise() - if event.button == PS3Controller.DOWN.value: + if controller.get_input_down(event) == Input.MOVE_DOWN: piece_drop_frames = 0 pressing_down_countdown = 3 try: move_piece_down() except WouldCollide: stick_piece() - elif event.type == pygame.JOYBUTTONUP: - if event.button == PS3Controller.DOWN.value: + elif event.type == controller.upevent: + if controller.get_input_up(event) == Input.MOVE_DOWN: pressing_down_countdown = None das += 1 if das == 16: - pressed_keys = pygame.key.get_pressed() - pressed_buttons = [joystick.get_button(x) for x in range(joystick.get_numbuttons())] with contextlib.suppress(WouldCollide): - if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_l] or pressed_buttons[PS3Controller.RIGHT.value]: + if controller.is_pressed(Input.MOVE_RIGHT): move_piece_right() - if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_h] or pressed_buttons[PS3Controller.LEFT.value]: + if controller.is_pressed(Input.MOVE_LEFT): move_piece_left() das = 10 |