ADD global context for each game state

This commit is contained in:
2025-04-19 19:51:05 +02:00
parent f96b0d579f
commit 4148e59db1
4 changed files with 38 additions and 17 deletions

23
main.py
View File

@@ -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