diff options
Diffstat (limited to 'src/splitscreen_duo')
| -rw-r--r-- | src/splitscreen_duo/games/breakout.py | 5 | ||||
| -rw-r--r-- | src/splitscreen_duo/games/game_base.py | 8 | ||||
| -rw-r--r-- | src/splitscreen_duo/games/pong.py | 5 | ||||
| -rw-r--r-- | src/splitscreen_duo/games/snake.py | 9 |
4 files changed, 18 insertions, 9 deletions
diff --git a/src/splitscreen_duo/games/breakout.py b/src/splitscreen_duo/games/breakout.py index 950adf5..734b394 100644 --- a/src/splitscreen_duo/games/breakout.py +++ b/src/splitscreen_duo/games/breakout.py @@ -117,13 +117,14 @@ class Breakout(GameBase): if not (self.waiting or self.score_display_time): for event in pygame.event.get(): result = self.handle_common_events(event) + action = self.input_handler.get_input(event) if result is not None: return result elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: + if action == "LEFT": self.move_paddle(-20) - elif event.key == pygame.K_RIGHT: + elif action == "RIGHT": self.move_paddle(20) self.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2 diff --git a/src/splitscreen_duo/games/game_base.py b/src/splitscreen_duo/games/game_base.py index 87cf65f..547b27e 100644 --- a/src/splitscreen_duo/games/game_base.py +++ b/src/splitscreen_duo/games/game_base.py @@ -2,6 +2,8 @@ import pygame import json from ..command import Command import logging +from ..input import Input +import os logger = logging.getLogger(__name__) @@ -20,11 +22,14 @@ class GameBase: self.score_display_time = 0 self.BLACK = (0, 0, 0) self.WHITE = (255, 255, 255) + self.input_handler = Input(debug=os.getenv("DEVELOPMENT", "").lower() != "") def handle_common_events(self, event): + action = self.input_handler.get_input(event) + if event.type == pygame.QUIT: return {"command": Command.QUIT.value, "action": None, "value": None} - elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: + elif event.type == pygame.KEYDOWN and action == "QUIT": return None return None @@ -44,6 +49,7 @@ class GameBase: return True elif message.get("command") == Command.QUIT.value: return {"command": Command.QUIT.value, "action": None, "value": None} + return False def end_game(self, score): diff --git a/src/splitscreen_duo/games/pong.py b/src/splitscreen_duo/games/pong.py index aa3d8a3..7f0d4e9 100644 --- a/src/splitscreen_duo/games/pong.py +++ b/src/splitscreen_duo/games/pong.py @@ -178,13 +178,14 @@ class Pong(GameBase): if not self.score_display_time: for event in pygame.event.get(): result = self.handle_common_events(event) + action = self.input_handler.get_input(event) if result is not None: return result elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: + if action == "LEFT": self.move_player_paddle(-PADDLE_SPEED) - elif event.key == pygame.K_RIGHT: + elif action == "RIGHT": self.move_player_paddle(PADDLE_SPEED) keys = pygame.key.get_pressed() diff --git a/src/splitscreen_duo/games/snake.py b/src/splitscreen_duo/games/snake.py index ac624e0..51c0bcc 100644 --- a/src/splitscreen_duo/games/snake.py +++ b/src/splitscreen_duo/games/snake.py @@ -100,17 +100,18 @@ class Snake(GameBase): if not (self.waiting or self.score_display_time): for event in pygame.event.get(): result = self.handle_common_events(event) + action = self.input_handler.get_input(event) if result is not None: return result elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_UP: + if action == "UP": self.change_to = "UP" - elif event.key == pygame.K_DOWN: + elif action == "DOWN": self.change_to = "DOWN" - elif event.key == pygame.K_LEFT: + elif action == "LEFT": self.change_to = "LEFT" - elif event.key == pygame.K_RIGHT: + elif action == "RIGHT": self.change_to = "RIGHT" self.check_serial() |