diff options
Diffstat (limited to 'src/splitscreen_duo/__init__.py')
| -rw-r--r-- | src/splitscreen_duo/__init__.py | 104 |
1 files changed, 82 insertions, 22 deletions
diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py index 309135c..defba37 100644 --- a/src/splitscreen_duo/__init__.py +++ b/src/splitscreen_duo/__init__.py @@ -19,8 +19,9 @@ def main() -> int: serial = Serial(os.getenv("SERIAL_DEVICE", "/dev/null"), 115200) INSTANCE = os.getenv("INSTANCE", "Unknown") + is_joint_mode = [False] - pygame.display.set_caption(f"SplitScreen Duo Menu ({INSTANCE})") + pygame.display.set_caption(f"SplitScreen Duo ({INSTANCE})") logging.basicConfig( level=(logging.DEBUG if os.getenv("DEVELOPMENT", "").lower() else logging.INFO) ) @@ -28,6 +29,10 @@ def main() -> int: logger.info(f"running as {os.getenv('INSTANCE', 'unknown')}") menu.init_display() + font = pygame.font.Font(None, 36) + BLACK = (0, 0, 0) + WHITE = (255, 255, 255) + score = 0 game_handlers = { Game.BENCHMARK.value: ( "benchmark", @@ -35,15 +40,17 @@ def main() -> int: ), Game.SNAKE.value: ( "snake", - lambda: Snake(menu.screen, serial, INSTANCE).main_loop(), + lambda: Snake(menu.screen, serial, INSTANCE, is_joint_mode[0]).main_loop(), ), Game.BREAKOUT.value: ( "breakout", - lambda: Breakout(menu.screen, serial, INSTANCE).main_loop(), + lambda: Breakout( + menu.screen, serial, INSTANCE, is_joint_mode[0] + ).main_loop(), ), Game.PONG.value: ( "pong", - lambda: Pong(menu.screen, serial, INSTANCE).main_loop(), + lambda: Pong(menu.screen, serial, INSTANCE, is_joint_mode[0]).main_loop(), ), } @@ -59,6 +66,11 @@ def main() -> int: game_result = game_func() + if is_joint_mode[0] and INSTANCE == "primary": + serial.write( + json.dumps({"command": Command.GAME_ENDED.value}).encode("utf-8") + ) + if game_result and game_result.get("command") == Command.QUIT.value: logger.info(f"{game_name} game returned QUIT, shutting down") pygame.quit() @@ -70,20 +82,49 @@ def main() -> int: is_running = True while is_running: - if INSTANCE == "secondary" and serial.in_waiting() > 0: + if serial.in_waiting() > 0: + data = serial.readline().decode("utf-8").strip() + try: - data = serial.readline().decode("utf-8").strip() + message = json.loads(data) + command = message.get("command") - logger.debug(f"received serial data: {data}") + if command == Command.ENTER_JOINT_MODE.value: + is_joint_mode[0] = True - message = json.loads(data) + logger.info("secondary entering joint mode") + elif command == Command.EXIT_JOINT_MODE.value: + is_joint_mode[0] = False - if message.get("command") == Command.QUIT.value: - logger.info("received quit command from primary, shutting down") + logger.info("secondary exiting joint mode") + elif command == Command.QUIT.value: pygame.quit() return 0 - elif message.get("command") == Command.SELECT_GAME.value: + elif INSTANCE == "secondary" and is_joint_mode[0]: + if command == Command.STATS.value: + score = message.get("value", 0) + elif command == Command.GAME_ENDED.value: + menu.screen.fill(BLACK) + + text = font.render("Game Ended", True, WHITE) + + menu.screen.blit( + text, + ( + menu.screen.get_width() // 2 - text.get_width() // 2, + menu.screen.get_height() // 2, + ), + ) + pygame.display.flip() + pygame.time.wait(2000) + + score = 0 + elif ( + INSTANCE == "secondary" + and not is_joint_mode[0] + and command == Command.SELECT_GAME.value + ): result = handle_game_selection(message, is_secondary=True) if result == 0: @@ -95,24 +136,43 @@ def main() -> int: except Exception as e: logger.error(f"error processing serial data: {e}") - serial_command = menu.process_events(serial, INSTANCE) + if INSTANCE == "secondary" and is_joint_mode[0]: + menu.screen.fill(BLACK) - if serial_command: - serial.write(json.dumps(serial_command).encode("utf-8")) + text = font.render(f"Score: {score}", True, WHITE) - if serial_command.get("command") == Command.QUIT.value: - logger.info("primary sent quit, shutting down") - pygame.quit() + menu.screen.blit( + text, + ( + menu.screen.get_width() // 2 - text.get_width() // 2, + menu.screen.get_height() // 2, + ), + ) + pygame.display.flip() + pygame.time.delay(100) + else: + serial_command = menu.process_events(serial, INSTANCE, is_joint_mode) - return 0 - elif serial_command.get("command") == Command.SELECT_GAME.value: - result = handle_game_selection(serial_command) + if serial_command: + if not is_joint_mode[0] or INSTANCE == "primary": + serial.write(json.dumps(serial_command).encode("utf-8")) + + if serial_command.get("command") == Command.QUIT.value: + pygame.quit() - if result == 0: return 0 + elif serial_command.get("command") == Command.SELECT_GAME.value: + result = handle_game_selection(serial_command) - menu.draw_menu() + if result == 0: + return 0 + + menu.draw_menu() pygame.quit() return 0 + + +if __name__ == "__main__": + main() |