aboutsummaryrefslogtreecommitdiff
path: root/src/splitscreen_duo/__init__.py
diff options
context:
space:
mode:
authorZoltan Szabatin <[email protected]>2025-03-02 19:59:57 -0800
committerZoltan Szabatin <[email protected]>2025-03-02 19:59:57 -0800
commit4d941814d9c6f124c63fff406a9fa2964ffe1d14 (patch)
treeba2463242db36d1dff7e1c2849d83cf30ae33405 /src/splitscreen_duo/__init__.py
parentfeat: Add pong game (diff)
downloadsplitscreen-duo-4d941814d9c6f124c63fff406a9fa2964ffe1d14.tar.xz
splitscreen-duo-4d941814d9c6f124c63fff406a9fa2964ffe1d14.zip
refactor: Generic game handling
Diffstat (limited to 'src/splitscreen_duo/__init__.py')
-rw-r--r--src/splitscreen_duo/__init__.py133
1 files changed, 45 insertions, 88 deletions
diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py
index 9689db4..309135c 100644
--- a/src/splitscreen_duo/__init__.py
+++ b/src/splitscreen_duo/__init__.py
@@ -28,6 +28,45 @@ def main() -> int:
logger.info(f"running as {os.getenv('INSTANCE', 'unknown')}")
menu.init_display()
+ game_handlers = {
+ Game.BENCHMARK.value: (
+ "benchmark",
+ lambda: benchmark.main_loop(menu.screen, serial),
+ ),
+ Game.SNAKE.value: (
+ "snake",
+ lambda: Snake(menu.screen, serial, INSTANCE).main_loop(),
+ ),
+ Game.BREAKOUT.value: (
+ "breakout",
+ lambda: Breakout(menu.screen, serial, INSTANCE).main_loop(),
+ ),
+ Game.PONG.value: (
+ "pong",
+ lambda: Pong(menu.screen, serial, INSTANCE).main_loop(),
+ ),
+ }
+
+ def handle_game_selection(message_or_command, is_secondary=False):
+ game_value = message_or_command.get("value")
+
+ if game_value in game_handlers:
+ game_name, game_func = game_handlers[game_value]
+
+ logger.info(
+ f"{'received' if is_secondary else 'starting'} {game_name} game selection from {'primary' if is_secondary else 'menu'}"
+ )
+
+ game_result = game_func()
+
+ if game_result and game_result.get("command") == Command.QUIT.value:
+ logger.info(f"{game_name} game returned QUIT, shutting down")
+ pygame.quit()
+
+ return 0
+
+ return None
+
is_running = True
while is_running:
@@ -45,57 +84,10 @@ def main() -> int:
return 0
elif message.get("command") == Command.SELECT_GAME.value:
- if message.get("value") == Game.BENCHMARK.value:
- logger.info("received benchmark game selection from primary")
-
- game_result = benchmark.main_loop(menu.screen, serial)
-
- if (
- game_result
- and game_result.get("command") == Command.QUIT.value
- ):
- pygame.quit()
-
- return 0
- elif message.get("value") == Game.SNAKE.value:
- logger.info("received snake game selection from primary")
-
- snake_game = Snake(menu.screen, serial, INSTANCE)
- game_result = snake_game.main_loop()
+ result = handle_game_selection(message, is_secondary=True)
- if (
- game_result
- and game_result.get("command") == Command.QUIT.value
- ):
- pygame.quit()
-
- return 0
- elif message.get("value") == Game.BREAKOUT.value:
- logger.info("received breakout game selection from primary")
-
- breakout_game = Breakout(menu.screen, serial, INSTANCE)
- game_result = breakout_game.main_loop()
-
- if (
- game_result
- and game_result.get("command") == Command.QUIT.value
- ):
- pygame.quit()
-
- return 0
- elif message.get("value") == Game.PONG.value:
- logger.info("received pong game selection from primary")
-
- pong_game = Pong(menu.screen, serial, INSTANCE)
- game_result = pong_game.main_loop()
-
- if (
- game_result
- and game_result.get("command") == Command.QUIT.value
- ):
- pygame.quit()
-
- return 0
+ if result == 0:
+ return 0
except json.JSONDecodeError:
logger.error("failed to decode serial message")
@@ -114,45 +106,10 @@ def main() -> int:
return 0
elif serial_command.get("command") == Command.SELECT_GAME.value:
- if serial_command.get("value") == Game.BENCHMARK.value:
- logger.info("starting benchmark game")
+ result = handle_game_selection(serial_command)
- game_result = benchmark.main_loop(menu.screen, serial)
-
- if game_result and game_result.get("command") == Command.QUIT.value:
- pygame.quit()
-
- return 0
- elif serial_command.get("value") == Game.SNAKE.value:
- logger.info("starting snake game")
-
- snake_game = Snake(menu.screen, serial, INSTANCE)
- game_result = snake_game.main_loop()
-
- if game_result and game_result.get("command") == Command.QUIT.value:
- pygame.quit()
-
- return 0
- elif serial_command.get("value") == Game.BREAKOUT.value:
- logger.info("starting breakout game")
-
- breakout_game = Breakout(menu.screen, serial, INSTANCE)
- game_result = breakout_game.main_loop()
-
- if game_result and game_result.get("command") == Command.QUIT.value:
- pygame.quit()
-
- return 0
- elif serial_command.get("value") == Game.PONG.value:
- logger.info("starting pong game")
-
- pong_game = Pong(menu.screen, serial, INSTANCE)
- game_result = pong_game.main_loop()
-
- if game_result and game_result.get("command") == Command.QUIT.value:
- pygame.quit()
-
- return 0
+ if result == 0:
+ return 0
menu.draw_menu()