From adf005bd29a395b653df1d990aa72694360a11dc Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 12 Nov 2021 13:32:37 +0100 Subject: Rename the package cameltris and provide __main__.py Signed-off-by: Olivier Gayot --- pycameltris/controller.py | 105 ---------------------------------------------- 1 file changed, 105 deletions(-) delete mode 100644 pycameltris/controller.py (limited to 'pycameltris/controller.py') diff --git a/pycameltris/controller.py b/pycameltris/controller.py deleted file mode 100644 index 86f5bcb..0000000 --- a/pycameltris/controller.py +++ /dev/null @@ -1,105 +0,0 @@ -import abc -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 Controller(abc.ABC): - @abc.abstractmethod - def is_pressed(self, input_: Input): - pass - - @abc.abstractmethod - def get_input_down(self, event): - pass - - @abc.abstractmethod - def get_input_up(self, event): - pass - - -class JoystickController(Controller): - - class PS3Controller(enum.Enum): - CROSS = 0 - CIRCLE = 1 - TRIANGLE = 2 - SQUARE = 3 - START = 9 - 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, - Input.PAUSE: JoystickController.PS3Controller.START, - } - 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(Controller): - 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, - Input.PAUSE: pygame.K_RETURN, - } - 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 -- cgit v1.2.3