Load level base for simplistic json file
[koperkapel.git] / koperkapel / scenes / level.py
index c29ffc0c94de4f6343c083af9c3511afc10b69d5..6537e5c05e46067306317548feb21ddf95a3c4df 100644 (file)
@@ -1,21 +1,40 @@
 """Render a level"""
 
 import json
+import os
 
 from pgzero.constants import keys
+from pgzero.loaders import images
 from .base import Scene, ChangeSceneEvent
+from .constants import TILE_SIZE, LEVEL_PATH
 
 
 class LevelScene(Scene):
     """ Level scene. """
 
-
     def __init__(self, level_name):
         self._level_name = level_name
+        f = open(os.path.join(LEVEL_PATH, level_name + '.json'))
+        level_data = json.load(f)
+        f.close()
+        self._tiles = level_data['tiles']
+        self._load_tile_images()
 
     def draw(self, screen):
         screen.clear()
-        screen.draw.text("This is level {}".format(self._level_name), (200, 100))
+        #screen.draw.text("This is level {}".format(self._level_name), (200, 100))
+        for tile in self._tiles:
+            pos = tile['pos']
+            pos = [pos[0] * TILE_SIZE, pos[1] * TILE_SIZE]
+            if not 'image' in tile:
+                # Skip broken tiles for now
+                continue
+            screen.blit(tile['image'], pos)
+
+    def _load_tile_images(self):
+        """Load all the tile images"""
+        for tile in self._tiles:
+            tile['image'] = getattr(images, tile['base'])
 
     def on_key_down(self, key, mod, unicode):
         if key == keys.ESCAPE: