From 6f6b6ceb8a1bca2d7ab08528ddb64f49e71b7283 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 13 Mar 2021 12:34:08 +0100 Subject: Allow to pause the game Signed-off-by: Olivier Gayot --- pycameltris/screens/Pause.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pycameltris/screens/Pause.py (limited to 'pycameltris/screens/Pause.py') diff --git a/pycameltris/screens/Pause.py b/pycameltris/screens/Pause.py new file mode 100644 index 0000000..f9f8859 --- /dev/null +++ b/pycameltris/screens/Pause.py @@ -0,0 +1,34 @@ +import contextlib +import sys + +import pygame + +from ..controller import KeyboardController, JoystickController, Input +from ..misc import UnPause + + +def handle_input_pressed(controller, event): + if controller.get_input_down(event) == Input.PAUSE: + raise UnPause() + + +class Pause: + def __init__(self, screen): + self.event_handler = dict() + + self.event_handler[pygame.QUIT] = lambda _: sys.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): + pass + + def oneframe(self): + try: + for event in pygame.event.get(): + with contextlib.suppress(KeyError): + self.event_handler[event.type](event) + except UnPause: + pygame.event.clear() + raise -- cgit v1.2.3