From b7167bdd4581951e124c850829d7500a6eaf2ad7 Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 28 Feb 2016 22:28:33 +0200 Subject: [PATCH] Load level base for simplistic json file --- koperkapel/levels/level1.json | 855 +++++++++++++++++++++++++++++++++ koperkapel/scenes/constants.py | 8 + koperkapel/scenes/level.py | 23 +- 3 files changed, 884 insertions(+), 2 deletions(-) create mode 100644 koperkapel/levels/level1.json create mode 100644 koperkapel/scenes/constants.py diff --git a/koperkapel/levels/level1.json b/koperkapel/levels/level1.json new file mode 100644 index 0000000..42e9782 --- /dev/null +++ b/koperkapel/levels/level1.json @@ -0,0 +1,855 @@ +{ + "tiles": [ + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 5 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 0, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 5 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 1, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 3 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 2, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 5 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 2, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 3 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 3, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 5 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 3, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 3, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 4, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 4, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 4, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 4, + 3 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 4 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 5 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 6 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 7 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 8 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 4, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 5 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 5, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 5, + 7 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 5, + 8 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 5, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 5 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 6, + 6 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 6, + 7 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 6, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 6, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 5 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 7, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 7 + ] + }, + { + "behaviour": [ + "walk", + "fly" + ], + "base": "floor", + "pos": [ + 7, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 7, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 5 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 8, + 9 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 0 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 1 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 2 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 3 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 4 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 5 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 6 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 7 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 8 + ] + }, + { + "behaviour": [], + "base": "cwall", + "pos": [ + 9, + 9 + ] + } + ] +} diff --git a/koperkapel/scenes/constants.py b/koperkapel/scenes/constants.py new file mode 100644 index 0000000..cc6f1cb --- /dev/null +++ b/koperkapel/scenes/constants.py @@ -0,0 +1,8 @@ +# Various constants + +import os + +TILE_SIZE = 64 + +# FIXME: Do betterer +LEVEL_PATH = os.path.join(os.path.dirname(__file__), '..', 'levels') diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index c29ffc0..6537e5c 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -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: -- 2.34.1