"""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: