1 """ Scene utilities. """
7 """ Decorator that applies events to an engine. """
9 def wrap(self, *args, **kw):
10 events = f(self, *args, **kw)
11 self._apply_events(events)
16 """ A holder for game state. """
18 def __init__(self, app, scene):
22 def _apply_events(self, events):
28 def change_scene(self, scene):
34 from pgzero.game import exit
39 return self._scene.update(dt)
42 self._scene.draw(self._app.screen)
45 def on_mouse_down(self, pos, button):
46 return self._scene.on_mouse_down(pos, button)
49 def on_mouse_up(self, pos, button):
50 return self._scene.on_mouse_up(pos, button)
53 def on_key_down(self, key, mod, unicode):
54 return self._scene.on_key_down(key, mod, unicode)
57 def on_key_up(self, key, mod):
58 return self._scene.on_key_up(key, mod)
61 def on_music_end(self):
62 return self._scene.on_music_end()
66 """ Base class for events. """
68 ENGINE_METHOD = "unknown_event"
70 def __init__(self, *args, **kw):
74 def apply(self, engine):
75 getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
78 class ChangeSceneEvent(Event):
79 """ Change to a new scene. """
81 ENGINE_METHOD = "change_scene"
84 class QuitEvent(Event):
85 """ Quit the game. """
87 ENGINE_METHOD = "quit_game"
91 """ A list of actors. """
97 self._actors.append(actor)
100 def remove(self, actor):
101 self._actors.remove(actor)
104 def draw(self, screen):
105 for actor in self._actors:
106 actor.draw() # TODO: allow an option screen to be passed in
110 """ Base class for scenes. """
113 self.actors = Actors()
121 def update(self, dt):
124 def draw(self, screen):
126 self.actors.draw(screen)
128 def on_mouse_down(self, pos, button):
131 def on_mouse_up(self, pos, button):
134 def on_key_down(self, key, mod, unicode):
137 def on_key_up(self, key, mod):
140 def on_music_end(self):