diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-11-12 13:32:37 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-11-12 13:32:37 +0100 |
commit | adf005bd29a395b653df1d990aa72694360a11dc (patch) | |
tree | d3cf034b95fc38c0e2d0217581d5e5f7f3689b43 /cameltris/controller.py | |
parent | 5bbc2d3008910bb9c1f9eb07d7dde5bda8aa52f4 (diff) |
Rename the package cameltris and provide __main__.py
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'cameltris/controller.py')
-rw-r--r-- | cameltris/controller.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/cameltris/controller.py b/cameltris/controller.py new file mode 100644 index 0000000..86f5bcb --- /dev/null +++ b/cameltris/controller.py @@ -0,0 +1,105 @@ +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 |