Scene changing.
authorSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 19:27:41 +0000 (21:27 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 19:27:41 +0000 (21:27 +0200)
koperkapel/scenes/base.py
koperkapel/scenes/credits.py [new file with mode: 0644]
koperkapel/scenes/menu.py

index 859f816e6359249e8dfbdc61e36f24b4699fcc44..9bad13cd9f03b9e1d172c9c977ae575002a51c88 100644 (file)
@@ -1,5 +1,16 @@
 """ Scene utilities. """
 
+import functools
+
+
+def apply_events(f):
+    """ Decorator that applies events to an engine. """
+    @functools.wraps(f)
+    def wrap(self, *args, **kw):
+        events = f(self, *args, **kw)
+        self._apply_events(events)
+    return wrap
+
 
 class Engine:
     """ A holder for game state. """
@@ -8,31 +19,73 @@ class Engine:
         self._app = app
         self._scene = scene
 
+    def _apply_events(self, events):
+        if not events:
+            return
+        for ev in events:
+            ev.apply(self)
+
+    def change_scene(self, scene):
+        self._scene.exit()
+        self._scene = scene
+        self._scene.enter()
+
+    @apply_events
     def update(self, dt):
-        self._scene.update(dt)
+        return self._scene.update(dt)
 
     def draw(self):
         self._scene.draw(self._app.screen)
 
+    @apply_events
     def on_mouse_down(self, pos, button):
-        self._scene.on_mouse_down(pos, button)
+        return self._scene.on_mouse_down(pos, button)
 
+    @apply_events
     def on_mouse_up(self, pos, button):
-        self._scene.on_mouse_up(pos, button)
+        return self._scene.on_mouse_up(pos, button)
 
+    @apply_events
     def on_key_down(self, key, mod, unicode):
-        self._scene.on_key_down(key, mod, unicode)
+        return self._scene.on_key_down(key, mod, unicode)
 
+    @apply_events
     def on_key_up(self, key, mod):
-        self._scene.on_key_up(key, mod)
+        return self._scene.on_key_up(key, mod)
 
+    @apply_events
     def on_music_end(self):
-        self._scene.on_music_end()
+        return self._scene.on_music_end()
+
+
+class Event:
+    """ Base class for events. """
+
+    ENGINE_METHOD = "unknown_event"
+
+    def __init__(self, *args, **kw):
+        self._args = args
+        self._kw = kw
+
+    def apply(self, engine):
+        getattr(engine, self.ENGINE_METHOD)(*self._args, **self._kw)
+
+
+class ChangeSceneEvent(Event):
+    """ Change to a new scene. """
+
+    ENGINE_METHOD = "change_scene"
 
 
 class Scene:
     """ Base class for scenes. """
 
+    def enter(self):
+        pass
+
+    def exit(self):
+        pass
+
     def update(self, dt):
         pass
 
diff --git a/koperkapel/scenes/credits.py b/koperkapel/scenes/credits.py
new file mode 100644 (file)
index 0000000..772943e
--- /dev/null
@@ -0,0 +1,17 @@
+""" Credits scene. """
+
+from pgzero.constants import keys
+from .base import Scene, ChangeSceneEvent
+
+
+class CreditsScene(Scene):
+    """ Credits scene. """
+
+    def draw(self, screen):
+        screen.clear()
+        screen.draw.text("Credits", (300, 300))
+
+    def on_key_down(self, key, mod, unicode):
+        if key == keys.ESCAPE:
+            from .menu import MenuScene
+            return [ChangeSceneEvent(MenuScene())]
index 4d3b5cf70219613b396638e34de2b997c86ec000..dac4402bd53643cf06c0a1add5a5be1164316418 100644 (file)
@@ -1,10 +1,17 @@
 """ Main menu scene. """
 
-from .base import Scene
+from pgzero.constants import keys
+from .base import Scene, ChangeSceneEvent
+from .credits import CreditsScene
 
 
 class MenuScene(Scene):
     """ Main menu scene. """
 
     def draw(self, screen):
+        screen.clear()
         screen.draw.text("Main menu", (300, 300))
+
+    def on_key_down(self, key, mod, unicode):
+        if key == keys.C:
+            return [ChangeSceneEvent(CreditsScene())]