pygstormgames/__init__.py

98 lines
3.2 KiB
Python
Raw Normal View History

"""
Core initialization module for PygStormGames framework.
Provides the main PygStormGames class that serves as the central hub for game functionality.
"""
from .config import Config
from .display import Display
from .menu import Menu
from .scoreboard import Scoreboard
from .sound import Sound
from .speech import Speech
import pyglet
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(gameTitle)
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:
# Wait for completion or skip input
@self.display.window.event
def on_key_press(symbol, modifiers):
if symbol in (pyglet.window.key.ESCAPE,
pyglet.window.key.RETURN,
pyglet.window.key.SPACE):
player.pause()
# Remove the temporary event handler
self.display.window.remove_handler('on_key_press', on_key_press)
return True
# Wait for sound to finish or user to skip
while player.playing:
self.display.window.dispatch_events()
# Remove the temporary event handler if not already removed
self.display.window.remove_handler('on_key_press', on_key_press)
except:
pass
# Set up window event handlers
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()