From 4148e59db19b747aca172b73e10e7a4bc2ed1cd5 Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Sat, 19 Apr 2025 19:51:05 +0200 Subject: [PATCH] ADD global context for each game state --- main.py | 23 ++++++++++++++++++----- states/game.py | 7 +++---- states/game_state.py | 13 +++++++++++-- states/start_menu.py | 12 ++++++------ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 2dc4842..0ca2ff3 100644 --- a/main.py +++ b/main.py @@ -2,26 +2,39 @@ import pygame from states.game import Game from states.start_menu import StartMenu - def main(): pygame.init() screen = pygame.display.set_mode((720, 720)) clock = pygame.time.Clock() states = {} + context = { + "match": None, + "screen": screen, + "font": pygame.font.Font(None, 36), + "small_font": pygame.font.Font(None, 15), + "options": { + "show_yaw": True + } + } current_state = None - def switch_state(state_name, data=None): + def switch_state(state_name): nonlocal current_state if state_name == "game": - match = data.get("match") - current_state = Game(switch_state, screen, match) + # Initialize Game state here + try: + match = context["match"] + except KeyError: + raise ValueError("Match object is required to initialize Game state.") + + current_state = Game(switch_state, context) states[state_name] = current_state current_state = states[state_name] # Initialize states - states["start_menu"] = StartMenu(switch_state, screen) + states["start_menu"] = StartMenu(switch_state, context) switch_state("start_menu") running = True diff --git a/states/game.py b/states/game.py index 0add090..294e6f7 100644 --- a/states/game.py +++ b/states/game.py @@ -4,11 +4,10 @@ from controllers.player_controller import PlayerController import pygame class Game(GameState): - def __init__(self, switch_state_callback, screen, match): - super().__init__(switch_state_callback, screen) - self.match = match + def __init__(self, switch_state_callback, context): + super().__init__(switch_state_callback, context) - self.renderer = Renderer(self.match, screen) + self.renderer = Renderer(self.match, self.screen) self.player_controller = PlayerController(self.renderer.player_render, self.match) def handle_events(self, events): diff --git a/states/game_state.py b/states/game_state.py index 3822fb4..8336092 100644 --- a/states/game_state.py +++ b/states/game_state.py @@ -1,7 +1,16 @@ +import pygame + class GameState: - def __init__(self, switch_state_callback, screen): + def __init__(self, switch_state_callback, context: dict): self.switch_state = switch_state_callback - self.screen = screen + self.context = context + self.screen = self.context.get("screen") + self.match = self.context.get("match", None) + self.font = self.context.get("font", pygame.font.Font(None, 36)) + self.small_font = self.context.get("small_font", pygame.font.Font(None, 15)) + self.options = self.context.get("options", { + "show_yaw": True + }) def handle_events(self, events): pass diff --git a/states/start_menu.py b/states/start_menu.py index 3a5751e..0981fd5 100644 --- a/states/start_menu.py +++ b/states/start_menu.py @@ -9,13 +9,12 @@ import pygame import pygame_gui class StartMenu(GameState): - def __init__(self, switch_state_callback, screen): - super().__init__(switch_state_callback, screen) - self.screen = screen - self.manager = pygame_gui.UIManager((screen.get_width(), screen.get_height())) + def __init__(self, switch_state_callback, context): + super().__init__(switch_state_callback, context) + self.manager = pygame_gui.UIManager((self.screen.get_width(), self.screen.get_height())) self.default_button_start_x = self.screen.get_width() * 0.1 - self.default_button_width = screen.get_width() * 0.8 + self.default_button_width = self.screen.get_width() * 0.8 # buttons self.button = Button(self.default_button_start_x, @@ -43,7 +42,8 @@ class StartMenu(GameState): def _start_game_callback(self): """Starts the game.""" match = self._setup_game(self.demo_file) - self.switch_state("game", {"match": match}) + self.context["match"] = match + self.switch_state("game") def _setup_game(self, demo_file: str) -> Match: demo_parser = demoparser2.DemoParser(demo_file)