Hook up display code to game
authorNeil <neil@dip.sun.ac.za>
Sun, 14 Apr 2013 18:46:24 +0000 (20:46 +0200)
committerNeil <neil@dip.sun.ac.za>
Sun, 14 Apr 2013 18:46:24 +0000 (20:46 +0200)
erdslangetjie/__main__.py

index c6bc969936b6ac889226a0f69988453e7dbe5321..08d7154a09b356b98be3981aeb96468cfab17621 100644 (file)
@@ -1,19 +1,63 @@
+import sys
+import os
 import kivy
 
 kivy.require('1.6.0')
 
 from kivy.app import App
 from kivy.uix.widget import Widget
+from kivy.uix.floatlayout import FloatLayout
+from kivy.graphics import Color, Rectangle
 
+from erdslangetjie.data import filepath, load
+from erdslangetjie.level import Level
 
-class GameWindow(Widget):
-    pass
+
+class GameWindow(FloatLayout):
+
+    def __init__(self, level):
+        super(GameWindow, self).__init__(size=(960, 960))
+        if os.path.exists(filepath(level)):
+            level_data = load(level)
+            self.level = level
+        elif os.path.exists(filepath('levels/' + level)):
+            level_data = load('levels/' + level)
+            self.level = 'levels/' + level
+        else:
+            print 'Unable to find %s - treating this as a new level' % level
+            sys.exit(1)
+        self.level_obj = Level()
+        self.level_obj.load(level_data)
+        level_data.close()
+
+    def build(self):
+        self.clear_widgets()
+        tiles = self.level_obj.get_tiles()
+        tile_size = 24
+        bx, by = 0, 0
+        for tile_line in tiles:
+            bx = 0
+            for tile in tile_line:
+                node = Widget(size=(tile_size, tile_size), pos=(bx, by))
+                with node.canvas:
+                    Color(1, 1, 1)
+                    Rectangle(pos=node.pos, size=node.size,
+                            texture=tile.texture)
+                self.add_widget(node)
+                bx += tile_size
+            by += tile_size
 
 
 class GameApp(App):
 
+    def __init__(self):
+        self.level = 'level1.txt'
+        super(GameApp, self).__init__()
+
     def build(self):
-        return GameWindow()
+        game = GameWindow(self.level)
+        game.build()
+        return game
 
 
 def main():