From e32619cfdf5103625e91a67b3b5e175cef43f662 Mon Sep 17 00:00:00 2001 From: Zoltan Szabatin Date: Sun, 2 Mar 2025 17:32:55 -0800 Subject: refactor: Move serial and Pygame definitions up in hierarchy --- src/splitscreen_duo/menu.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'src/splitscreen_duo/menu.py') diff --git a/src/splitscreen_duo/menu.py b/src/splitscreen_duo/menu.py index d552dab..8962c5d 100644 --- a/src/splitscreen_duo/menu.py +++ b/src/splitscreen_duo/menu.py @@ -2,33 +2,31 @@ import sys import pygame import os from .input import Input -from .serial import Serial import logging import json -INSTANCE = os.getenv("INSTANCE", "Unknown") - -pygame.init() -pygame.display.set_caption(f"SplitScreen Duo Menu ({INSTANCE})") - -WIDTH, HEIGHT = ( - pygame.display.Info().current_w // 2, - pygame.display.Info().current_h // 2, -) WHITE = (255, 255, 255) BLACK = (0, 0, 0) -FONT = pygame.font.Font(None, 36) IS_DEVELOPMENT_MODE = os.getenv("DEVELOPMENT", "").lower() != "" OPTIONS = ["Breakout", "Pong v.s. Computer", "Snake", "Quit"] -selected_index = 0 -screen = pygame.display.set_mode( - (WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0), - 0 if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN, -) -serial = Serial(os.getenv("SERIAL_DEVICE", "/dev/null"), 115200) logger = logging.getLogger(__name__) +def init_display(): + global WIDTH, HEIGHT, FONT, screen, selected_index + + WIDTH, HEIGHT = ( + pygame.display.Info().current_w // 2, + pygame.display.Info().current_h // 2, + ) + FONT = pygame.font.Font(None, 36) + screen = pygame.display.set_mode( + (WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0), + 0 if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN, + ) + selected_index = 0 + + def draw_menu(): screen.fill(WHITE) @@ -40,7 +38,9 @@ def draw_menu(): pygame.display.flip() -def main_loop(): +def main_loop(serial, instance): + init_display() + global selected_index is_running = True @@ -49,7 +49,7 @@ def main_loop(): while is_running: draw_menu() - if INSTANCE == "secondary": + if instance == "secondary": if serial.in_waiting() > 0: logger.debug(serial.readline().decode("utf-8").strip()) @@ -69,7 +69,6 @@ def main_loop(): pygame.quit() sys.exit() else: - print(OPTIONS[selected_index]) serial.write( json.dumps({"command": 0, "action": action}).encode("utf-8") ) -- cgit v1.2.3