diff options
-rwxr-xr-x | cameltris.py | 53 | ||||
-rw-r--r-- | pycameltris/controller.py | 87 |
2 files changed, 104 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 diff --git a/pycameltris/controller.py b/pycameltris/controller.py new file mode 100644 index 0000000..3532fd9 --- /dev/null +++ b/pycameltris/controller.py @@ -0,0 +1,87 @@ +import enum + +import pygame + + +class Input(enum.Enum): + ROTATE_CLOCKWISE = 0 + ROTATE_COUNTER_CLOCKWISE = 1 + MOVE_LEFT = 2 + MOVE_RIGHT = 3 + MOVE_DOWN = 4 + PAUSE = 5 + QUIT = 6 + + +class JoystickController: + + class PS3Controller(enum.Enum): + CROSS = 0 + CIRCLE = 1 + TRIANGLE = 2 + SQUARE = 3 + DOWN = 14 + LEFT = 15 + RIGHT = 16 + + def __init__(self, joystick): + self.joystick = joystick + self.mapping = { + Input.ROTATE_CLOCKWISE: JoystickController.PS3Controller.CROSS, + Input.ROTATE_COUNTER_CLOCKWISE: JoystickController.PS3Controller.CIRCLE, + Input.MOVE_LEFT: JoystickController.PS3Controller.LEFT, + Input.MOVE_RIGHT: JoystickController.PS3Controller.RIGHT, + Input.MOVE_DOWN: JoystickController.PS3Controller.DOWN, + } + self.downevent = pygame.JOYBUTTONDOWN + self.upevent = pygame.JOYBUTTONUP + + def is_pressed(self, input_: Input): + return self.joystick.get_button(self.mapping[input_].value) + + + def get_input_down(self, event): + for key, value in self.mapping.items(): + if value.value == event.button: + return key + + return None + + def get_input_up(self, event): + for key, value in self.mapping.items(): + if value.value == event.button: + return key + + return None + + +class KeyboardController: + def __init__(self, keyboard): + self.keyboard = keyboard + self.mapping = { + Input.ROTATE_CLOCKWISE: pygame.K_f, + Input.ROTATE_COUNTER_CLOCKWISE: pygame.K_d, + Input.MOVE_LEFT: pygame.K_h, + Input.MOVE_RIGHT: pygame.K_l, + Input.MOVE_DOWN: pygame.K_j, + Input.QUIT: pygame.K_q, + } + self.downevent = pygame.KEYDOWN + self.upevent = pygame.KEYUP + + def is_pressed(self, input_: Input): + return self.keyboard.get_pressed()[self.mapping[input_]] + + def get_input_down(self, event): + for key, value in self.mapping.items(): + if value == event.key: + return key + + return None + + def get_input_up(self, event): + for key, value in self.mapping.items(): + if value == event.key: + return key + + return None |