Merge branch 'master' of ctpug.org.za:koperkapel
[koperkapel.git] / koperkapel / scenes / level.py
1 """Render a level"""
2
3 import json
4 import os
5
6 from pgzero.constants import keys
7 from pgzero.loaders import images
8 from .base import Scene, ChangeSceneEvent
9 from .constants import TILE_SIZE, LEVEL_PATH
10
11
12 class LevelScene(Scene):
13     """ Level scene. """
14
15     def __init__(self, level_name):
16         self._level_name = level_name
17         f = open(os.path.join(LEVEL_PATH, level_name + '.json'))
18         level_data = json.load(f)
19         f.close()
20         self._tiles = level_data['tiles']
21         self._load_tile_images()
22
23     def draw(self, screen):
24         screen.clear()
25         #screen.draw.text("This is level {}".format(self._level_name), (200, 100))
26         for tile in self._tiles:
27             pos = tile['pos']
28             pos = [pos[0] * TILE_SIZE, pos[1] * TILE_SIZE]
29             if not 'image' in tile:
30                 # Skip broken tiles for now
31                 continue
32             screen.blit(tile['image'], pos)
33
34     def _load_tile_images(self):
35         """Load all the tile images"""
36         for tile in self._tiles:
37             tile['image'] = getattr(images, tile['base'])
38
39     def on_key_down(self, key, mod, unicode):
40         if key == keys.ESCAPE:
41             from .menu import MenuScene
42             return [ChangeSceneEvent(MenuScene())]