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._update_vehicle = False
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._scene.move_screen(offset)
48 return self._scene.update(self._world.proxy(), self, dt)
51 self._scene.draw(self._app.screen)
54 def on_mouse_down(self, pos, button):
55 return self._scene.on_mouse_down(pos, button)
58 def on_mouse_up(self, pos, button):
59 return self._scene.on_mouse_up(pos, button)
62 def on_key_down(self, key, mod, unicode):
63 return self._scene.on_key_down(key, mod, unicode)
66 def on_key_up(self, key, mod):
67 return self._scene.on_key_up(key, mod)
70 def on_music_end(self):
71 return self._scene.on_music_end()
75 """ Base class for events. """
77 ENGINE_METHOD = "unknown_event"
79 def __init__(self, *args, **kw):
83 def apply(self, engine):
84 getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
87 class ChangeSceneEvent(Event):
88 """ Change to a new scene. """
90 ENGINE_METHOD = "change_scene"
93 class WorldEvent(Event):
94 """ Be a hero. Change the world. """
96 ENGINE_METHOD = "change_world"
99 class QuitEvent(Event):
100 """ Quit the game. """
102 ENGINE_METHOD = "quit_game"
105 class MoveViewportEvent(Event):
106 """ Change to a new scene. """
108 ENGINE_METHOD = "move_screen"
112 """ A single layer of actors. """
114 def __init__(self, name):
119 return iter(self.actors)
121 def __getitem__(self, i):
122 return self.actors[i]
125 return len(self.actors)
127 def add(self, actor):
128 self.actors.append(actor)
134 def remove(self, actor):
135 self.actors.remove(actor)
140 """ Layers of actors.
142 Actors may be rendered in different layers. Layers with lower levels
143 are rendered lower than layers with higher ones.
147 self._ordered_layers = []
149 self.add_layer("default", 0)
151 def __getattr__(self, name):
152 return self._layers[name]
154 def add_layer(self, name, level):
155 layer = self._layers[name] = Layer(name)
156 self._ordered_layers.append((level, name))
157 self._ordered_layers.sort()
160 def add(self, actor, layer="default"):
161 return self._layers[layer].add(actor)
163 def remove(self, actor, layer="default"):
164 return self._layers[layer].remove(actor)
166 def draw(self, screen):
167 for lvl, name in self._ordered_layers:
168 for actor in self._layers[name]:
169 # actor.draw doesn't allow blitting to anything other than
171 screen.blit(actor._surf, actor.topleft)
174 def defer_to_update(f):
175 """ Defers a function until the next update run. """
177 def wrapper(self, *args, **kw):
178 self._deferred_updates.append((f, args, kw))
183 """ Base class for scenes. """
186 self.actors = Actors()
187 self.viewport = (0, 0)
188 self._deferred_updates = []
190 def move_screen(self, offset):
191 self.viewport = (self.viewport[0] + offset[0],
192 self.viewport[1] + offset[1])
194 def calc_offset(self, x, y):
195 """ Return a position offset by the viewport. """
196 return x - self.viewport[0], y - self.viewport[1]
198 def enter(self, world):
201 def exit(self, world):
204 def update(self, world, engine, dt):
205 deferred_updates, self._deferred_updates = self._deferred_updates, []
206 for f, args, kw in deferred_updates:
207 f(self, world, *args, **kw)
209 def draw(self, screen):
211 self.actors.draw(screen)
213 def on_mouse_down(self, pos, button):
216 def on_mouse_up(self, pos, button):
219 def on_key_down(self, key, mod, unicode):
222 def on_key_up(self, key, mod):
225 def on_music_end(self):