""" Core initialization module for PygStormGames framework. Provides the main PygStormGames class that serves as the central hub for game functionality. """ import pyglet import time from .config import Config from .display import Display from .menu import Menu from .scoreboard import Scoreboard from .sound import Sound from .speech import Speech class pygstormgames: """Main class that coordinates all game systems.""" def __init__(self, gameTitle): """Initialize the game framework. Args: gameTitle (str): Title of the game """ self.gameTitle = gameTitle self._paused = False # Initialize core systems self.config = Config(gameTitle) self.display = Display(self) self.speech = Speech() self.sound = Sound(self) self.scoreboard = Scoreboard(self) self.menu = Menu(self) # Play intro sound if available try: player = self.sound.play_sound('game-intro') if player: startTime = time.time() duration = player.source.duration # Make sure to give pyglet enough cycles to start playing pyglet.clock.tick() interrupted = self.wait_for_completion( lambda: not player.playing or (time.time() - startTime) >= duration ) if interrupted: player.pause() except: pass # Set up window event handlers after intro is complete self.display.window.push_handlers(self.on_key_press) def on_key_press(self, symbol, modifiers): """Handle global keyboard events. Args: symbol: Pyglet key symbol modifiers: Key modifiers """ if self._paused: if symbol == pyglet.window.key.BACKSPACE: self._paused = False self.sound.resume() self.speech.speak("Game resumed") else: # Global exit handler if symbol == pyglet.window.key.ESCAPE: self.exit_game() # Global pause handler if symbol == pyglet.window.key.BACKSPACE: self.pause_game() def run(self): """Start the game loop.""" pyglet.app.run() def pause_game(self): """Pause all game systems and wait for resume.""" self._paused = True self.sound.pause() self.speech.speak("Game paused, press backspace to resume.") def exit_game(self): """Clean up and exit the game.""" self.sound.cleanup() self.speech.cleanup() pyglet.app.exit() def wait(self, validKeys=None): """Wait for key press(es). Args: validKeys (list, optional): List of pyglet.window.key values to wait for. If None, accepts any key press. Returns: tuple: (key, modifiers) that were pressed """ keyResult = [None, None] # Use list to allow modification in closure def on_key_press(symbol, modifiers): if validKeys is None or symbol in validKeys: keyResult[0] = symbol keyResult[1] = modifiers return pyglet.event.EVENT_HANDLED # Register temporary handler self.display.window.push_handlers(on_key_press=on_key_press) # Wait for valid key press while keyResult[0] is None: self.display.window.dispatch_events() pyglet.clock.tick() # Clean up self.display.window.remove_handlers() return tuple(keyResult) def wait_for_completion(self, condition, validKeys=None): """Wait for either a condition to be met or valid keys to be pressed. Args: condition: Function that returns True when waiting should end validKeys (list, optional): List of pyglet.window.key values that can interrupt If None, uses ESCAPE, RETURN, and SPACE Returns: bool: True if interrupted by key press, False if condition was met """ if validKeys is None: validKeys = [pyglet.window.key.ESCAPE, pyglet.window.key.RETURN, pyglet.window.key.SPACE] interrupted = [False] # Use list to allow modification in closure def on_key_press(symbol, modifiers): if symbol in validKeys: interrupted[0] = True return pyglet.event.EVENT_HANDLED self.display.window.push_handlers(on_key_press=on_key_press) while not condition() and not interrupted[0]: self.display.window.dispatch_events() pyglet.clock.tick() self.display.window.remove_handlers() return interrupted[0]