Commits

Owen Nelson  committed 82ebb43

got a working implementation without arg passing to the game constructor

  • Participants
  • Parent commits 4401cf3

Comments (0)

Files changed (3)

       setup_requires=['pyglet', ],
       entry_points={
         'gui_scripts': [
-            'golgo = golgui.start_game',
+            'golgo = golgui:start_game',
         ]
     }
 )

File src/golgui/__init__.py

+import sys
+import argparse
 from .runner import Runner
 
 
+parser = argparse.ArgumentParser()
+parser.add_argument("game", help="dotted path to a game class, eg my.game.Game")
+
+
 def start_game():
-    # TODO: parse args
-    args = {}
-    # TODO: build game
-    game = None
-    Runner(game=game)
+    sys.path.insert(0, '.')
+    args = parser.parse_args()
+    game_parts = args.game.split('.')
+    klass = game_parts[-1]
+    del game_parts[-1]
+    module = __import__('.'.join(game_parts))
+    Game = getattr(module, klass)
+    Runner.bind(game=Game())
+    Runner.run()

File src/golgui/runner.py

+import argparse
 import pyglet
 
 window = pyglet.window.Window(resizable=True)
 
 @window.event
 def on_key_press(symbol, modifiers):
-    global grid_w
-    global grid_h
     if symbol == pyglet.window.key.Q:
         window.close()
 
         Runner.grid_w = float(window.width) / Runner.game.state.shape[0]
         Runner.grid_h = float(window.height) / Runner.game.state.shape[1]
 
+    if symbol == pyglet.window.key.SPACE:
+        Runner.reset()
+
 
 @window.event
 def on_resize(width, height):
 
     @staticmethod
     def run():
-        return pyglet.app.run()
+        return pyglet.app.run()
+
+    @classmethod
+    def reset(cls):
+        return getattr(cls.game, 'reset', lambda: None)()
+