Rework scenes and allow the user to quit.
authorSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 13:53:54 +0000 (15:53 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 13:53:54 +0000 (15:53 +0200)
tabakrolletjie/engine.py
tabakrolletjie/gamestate.py
tabakrolletjie/main.py
tabakrolletjie/scenes/base.py [new file with mode: 0644]
tabakrolletjie/scenes/menu.py

index eee88b761dcc168c07c1d8fe85731af1b00a7962..8ee5fc96a43c5a8746c62fa653893a5d9ca34c15 100644 (file)
@@ -11,9 +11,16 @@ from .constants import FPS
 
 
 class Engine(object):
-    def __init__(self, screen, scene):
+    def __init__(self, screen, gamestate):
         self._screen = screen
+        self._gamestate = gamestate
+        self._scene = None
+
+    def set_scene(self, scene):
+        if self._scene is not None:
+            self._scene.exit(self._gamestate)
         self._scene = scene
+        self._scene.enter(self._gamestate)
 
     def run(self):
         clock = pygame.time.Clock()
@@ -24,9 +31,9 @@ class Engine(object):
                 if ev.type == pgl.QUIT:
                     return
                 else:
-                    pass  # TODO: send to scene
+                    self._scene.event(ev, self._gamestate)
 
-            # TODO: render scene
+            self._scene.render(self._screen, self._gamestate)
             pygame.display.flip()
 
             clock.tick(FPS)
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..6f7f5c45afe62ac586e7fae86e78876e8177e350 100644 (file)
@@ -0,0 +1,4 @@
+""" The game state. """
+
+class GameState(object):
+    pass
index 77af827f3af56a3d1dbdb3edc8994b55ebdc613a..f09d6811c875636d38ea9d3d3a84c16ef0899806 100644 (file)
@@ -6,6 +6,7 @@ import pygame.locals as pgl
 
 from .constants import SCREEN_SIZE, TITLE
 from .engine import Engine
+from .gamestate import GameState
 from .scenes.menu import MenuScene
 
 
@@ -18,7 +19,8 @@ def main():
     # TODO: set an icon
 
     screen = pygame.display.get_surface()
-    scene = MenuScene()
-    engine = Engine(screen, scene)
+    gamestate = GameState()
+    engine = Engine(screen, gamestate)
+    engine.set_scene(MenuScene())
 
     engine.run()
diff --git a/tabakrolletjie/scenes/base.py b/tabakrolletjie/scenes/base.py
new file mode 100644 (file)
index 0000000..3b0f60a
--- /dev/null
@@ -0,0 +1,14 @@
+""" Base scene class. """
+
+class BaseScene(object):
+    def enter(self, gamestate):
+        """ Enter the scene. """
+
+    def exit(self, gamestate):
+        """ Leave the scene. """
+
+    def event(self, ev, gamestate):
+        """ Handle an event. """
+
+    def render(self, screen, gamestate):
+        """ Render the scene. """
index e7092d58637889e3941e939841cfad24e1a6d52b..ca9e9b5b20650d85f0e882f230bcbf72544c80d2 100644 (file)
@@ -1,4 +1,13 @@
 """ Menu scene. """
 
-class MenuScene(object):
-    pass
+import pygame.event
+import pygame.locals as pgl
+
+from .base import BaseScene
+
+
+class MenuScene(BaseScene):
+    def event(self, ev, gamestate):
+        if ev.type == pgl.KEYDOWN:
+            if ev.key in (pgl.K_q, pgl.K_ESCAPE):
+                pygame.event.post(pygame.event.Event(pgl.QUIT))