changeset 14:fb1bd081cc86

Add scene changing.
author Simon Cross <hodgestar@gmail.com>
date Sun, 03 Apr 2011 16:35:18 +0200
parents b83f2db218e6
children 538d15e6cca1
files skaapsteker/__main__.py skaapsteker/engine.py
diffstat 2 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/__main__.py	Sun Apr 03 16:33:27 2011 +0200
+++ b/skaapsteker/__main__.py	Sun Apr 03 16:35:18 2011 +0200
@@ -2,7 +2,7 @@
    """
 
 from constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
-from engine import Engine
+from engine import Engine, Scene
 
 import pygame
 from pygame.locals import SWSURFACE
@@ -47,6 +47,7 @@
     pygame.display.set_caption("Nine Tales")
 
     engine = Engine()
+    engine.change_scene(Scene())
     try:
         engine.run()
     except KeyboardInterrupt:
--- a/skaapsteker/engine.py	Sun Apr 03 16:33:27 2011 +0200
+++ b/skaapsteker/engine.py	Sun Apr 03 16:35:18 2011 +0200
@@ -3,19 +3,82 @@
 import pygame.display
 import pygame.time
 import pygame.event
-from pygame.locals import QUIT
+from pygame.locals import QUIT, USEREVENT
 
 class Engine(object):
 
     def __init__(self):
         self._framerate = 60
+        self._current_scene = None
+
+    def change_scene(self, next_scene):
+        if self._current_scene is not None:
+            self._current_scene.leave()
+        self._current_scene = next_scene
+        self._current_scene.enter()
 
     def run(self):
         """Run the game loop dispatching events as necessary."""
+        assert self._current_scene is not None
         clock = pygame.time.Clock()
+        surface = pygame.display.get_surface()
         while True:
             events = pygame.event.get()
             for ev in events:
                 if ev.type is QUIT:
                     return
+                if ChangeScene.matches(ev):
+                    self.change_scene(ev.next_scene)
+                    break
+                self._current_scene.dispatch(ev)
+            self._current_scene.draw(surface)
+            pygame.display.flip()
             clock.tick(self._framerate)
+
+
+class Scene(object):
+
+    def __init__(self):
+        pass
+
+    def post(self, ev):
+        """Post an event to pygame's event loop."""
+        pygame.event.post(ev)
+
+    def enter(self):
+        """Enter the scene."""
+        pass
+
+    def leave(self):
+        """Exit the scene."""
+        pass
+
+    def dispatch(self, ev):
+        """Dispatch an event."""
+        pass
+
+    def draw(self, surface):
+        """Update the scene surface."""
+        pass
+
+
+class UserEvent(object):
+
+    utype = "UNKNOWN"
+
+    @classmethod
+    def post(cls, **kws):
+        ev = pygame.event.Event(USEREVENT, utype=cls.utype, **kws)
+        pygame.event.post(ev)
+
+    @classmethod
+    def matches(cls, ev):
+        return ev.type is USEREVENT and ev.utype == cls.utype
+
+class ChangeScene(UserEvent):
+
+    utype = "CHANGE_SCENE"
+
+    @classmethod
+    def post(cls, next_scene):
+        super(ChangeScene, cls).post(next_scene=next_scene)