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 & scene management.
19 def __init__(self, app, scene, world):
23 self._viewport = (0, 0)
25 def _apply_events(self, events):
31 def change_scene(self, scene):
32 self._apply_events(self._scene.exit(self._world.proxy()))
34 self._apply_events(self._scene.enter(self._world.proxy()))
36 def change_world(self, *args, **kw):
37 self._world.apply_event(*args, **kw)
40 from pgzero.game import exit
43 def move_screen(self, offset):
44 self._viewport = (self._viewport[0] + offset[0],
45 self._viewport[1] + offset[1])
49 return self._scene.update(self._world.proxy(), dt)
52 self._scene.draw(self._app.screen, self._viewport)
55 def on_mouse_down(self, pos, button):
56 return self._scene.on_mouse_down(pos, button)
59 def on_mouse_up(self, pos, button):
60 return self._scene.on_mouse_up(pos, button)
63 def on_key_down(self, key, mod, unicode):
64 return self._scene.on_key_down(key, mod, unicode)
67 def on_key_up(self, key, mod):
68 return self._scene.on_key_up(key, mod)
71 def on_music_end(self):
72 return self._scene.on_music_end()
76 """ Base class for events. """
78 ENGINE_METHOD = "unknown_event"
80 def __init__(self, *args, **kw):
84 def apply(self, engine):
85 getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
88 class ChangeSceneEvent(Event):
89 """ Change to a new scene. """
91 ENGINE_METHOD = "change_scene"
94 class WorldEvent(Event):
95 """ Be a hero. Change the world. """
97 ENGINE_METHOD = "change_world"
100 class QuitEvent(Event):
101 """ Quit the game. """
103 ENGINE_METHOD = "quit_game"
106 class MoveViewportEvent(Event):
107 """ Change to a new scene. """
109 ENGINE_METHOD = "move_screen"
113 """ A list of actors. """
118 def add(self, actor):
119 self._actors.append(actor)
122 def remove(self, actor):
123 self._actors.remove(actor)
126 def draw(self, screen):
127 for actor in self._actors:
128 actor.draw() # TODO: allow an option screen to be passed in
132 """ Base class for scenes. """
135 self.actors = Actors()
137 def enter(self, world):
140 def exit(self, world):
143 def update(self, world, dt):
146 def draw(self, screen, viewport):
148 self.actors.draw(screen)
150 def on_mouse_down(self, pos, button):
153 def on_mouse_up(self, pos, button):
156 def on_key_down(self, key, mod, unicode):
159 def on_key_up(self, key, mod):
162 def on_music_end(self):