changeset 554:99a1420097df pyntnclick

Create GameDescription object.
author Simon Cross <hodgestar+bzr@gmail.com>
date Sat, 11 Feb 2012 14:04:48 +0200
parents ebb2efcb4ea7
children c0474fe18b96
files gamelib/main.py pyntnclick/main.py pyntnclick/state.py
diffstat 3 files changed, 67 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/main.py	Sat Feb 11 13:40:51 2012 +0200
+++ b/gamelib/main.py	Sat Feb 11 14:04:48 2012 +0200
@@ -1,7 +1,14 @@
-from scenes import SCENE_LIST, INITIAL_SCENE
+import scenes
+
+from pyntnclick.main import GameDescription
+
 
-from pyntnclick.main import main as pyntnclick_main
+class SuspendedSentence(GameDescription):
+
+    INITIAL_SCENE = scenes.INITIAL_SCENE
+    SCENE_LIST = scenes.SCENE_LIST
 
 
 def main():
-    return pyntnclick_main(INITIAL_SCENE, SCENE_LIST)
+    ss = SuspendedSentence()
+    return ss.main()
--- a/pyntnclick/main.py	Sat Feb 11 13:40:51 2012 +0200
+++ b/pyntnclick/main.py	Sat Feb 11 14:04:48 2012 +0200
@@ -48,31 +48,61 @@
         self.show_screen(self.menu_screen)
 
 
-def main(initial_scene, scene_list, debug_rects=False):
-    opts = parse_args(sys.argv)
-    pygame.display.init()
-    pygame.font.init()
-    if opts.sound:
+class GameDescriptionError(Exception):
+    """Raised when an GameDescription is invalid."""
+
+
+class GameDescription(object):
+
+    # initial scene for start of game (unless overridden by debug)
+    INITIAL_SCENE = None
+
+    # list of game scenes
+    SCENE_LIST = None
+
+    def __init__(self):
+        if self.INITIAL_SCENE is None:
+            raise GameDescriptionError("A game must have an initial scene.")
+        if not self.SCENE_LIST:
+            raise GameDescriptionError("A game must have a non-empty list"
+                                       " of scenes.")
+        self._initial_scene = self.INITIAL_SCENE
+        self._scene_list = self.SCENE_LIST
+        self._debug_rects = False
+
+    def initial_state(self):
+        """Create a copy of the initial game state."""
+        initial_state = state.GameState()
+        initial_state.set_debug_rects(self._debug_rects)
+        for scene in self._scene_list:
+            initial_state.load_scenes(scene)
+        initial_state.set_current_scene(self._initial_scene)
+        initial_state.set_do_enter_leave()
+        return initial_state
+
+    def main(self):
+        opts = parse_args(sys.argv)
+        pygame.display.init()
+        pygame.font.init()
+        if opts.sound:
+            try:
+                pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
+            except pygame.error, exc:
+                no_sound(exc)
+        else:
+            # Ensure get_sound returns nothing, so everything else just works
+            disable_sound()
+        if DEBUG:
+            if opts.scene is not None:
+                # debug the specified scene
+                self._initial_scene = opts.scene
+            self._debug_rects = opts.rects
+        display = pygame.display.set_mode(SCREEN, SWSURFACE)
+        pygame.display.set_icon(pygame.image.load(
+            data.filepath('icons/suspended_sentence24x24.png')))
+        pygame.display.set_caption("Suspended Sentence")
+        shell = MainShell(display, self.initial_state)
         try:
-            pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
-        except pygame.error, exc:
-            no_sound(exc)
-    else:
-        # Ensure get_sound returns nothing, so everything else just works
-        disable_sound()
-    if DEBUG:
-        if opts.scene is not None:
-            # debug the specified scene
-            initial_scene = opts.scene
-        debug_rects = opts.rects
-    initial_state = state.initial_state_creator(initial_scene, scene_list,
-                                                debug_rects)
-    display = pygame.display.set_mode(SCREEN, SWSURFACE)
-    pygame.display.set_icon(pygame.image.load(
-        data.filepath('icons/suspended_sentence24x24.png')))
-    pygame.display.set_caption("Suspended Sentence")
-    shell = MainShell(display, initial_state)
-    try:
-        shell.run()
-    except KeyboardInterrupt:
-        pass
+            shell.run()
+        except KeyboardInterrupt:
+            pass
--- a/pyntnclick/state.py	Sat Feb 11 13:40:51 2012 +0200
+++ b/pyntnclick/state.py	Sat Feb 11 14:04:48 2012 +0200
@@ -54,19 +54,6 @@
                     res.process(scene_widget)
 
 
-def initial_state_creator(initial_scene, scene_list, debug_rects=False):
-    def initial_state():
-        """Load the initial state."""
-        state = GameState()
-        state.set_debug_rects(debug_rects)
-        for scene in scene_list:
-            state.load_scenes(scene)
-        state.set_current_scene(initial_scene)
-        state.set_do_enter_leave()
-        return state
-    return initial_state
-
-
 class GameState(object):
     """Complete game state.