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/screens/Pause.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/screens/Pause.py')
-rw-r--r-- | cameltris/screens/Pause.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/cameltris/screens/Pause.py b/cameltris/screens/Pause.py new file mode 100644 index 0000000..766a45c --- /dev/null +++ b/cameltris/screens/Pause.py @@ -0,0 +1,39 @@ +import contextlib +import sys +from typing import Callable, NoReturn + +import pygame + +from .Screen import Screen +from ..controller import Controller, KeyboardController, JoystickController, Input +from ..misc import UnPause + + +def handle_input_pressed(controller: Controller, event: pygame.event.Event): + if controller.get_input_down(event) == Input.PAUSE: + raise UnPause() + + +class Pause(Screen): + def __init__(self, screen: pygame.surface.Surface): + self.event_handler: dict[int, Callable[[pygame.event.Event], None]] = {} + + def exit(_) -> NoReturn: + sys.exit() + + self.event_handler[pygame.QUIT] = exit + self.event_handler[pygame.JOYBUTTONDOWN] = lambda evt: handle_input_pressed(JoystickController(evt.joy), evt) + self.event_handler[pygame.KEYDOWN] = lambda evt: handle_input_pressed(KeyboardController(pygame.key), evt) + + + def refresh(self) -> None: + pass + + def oneframe(self) -> None: + try: + for event in pygame.event.get(): + with contextlib.suppress(KeyError): + self.event_handler[event.type](event) + except UnPause: + pygame.event.clear() + raise |