1 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
5 from pgzero.loaders import images, ResourceLoader
15 raise NotImplementedError()
17 return images.load(cls.IMG)
19 class RandomizedTile(Tile):
24 if cls.IMGDIR is None:
25 raise NotImplementedError()
27 imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
28 img = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
30 return images.load(os.path.join(cls.IMGDIR, img))
32 class Floor(RandomizedTile):
35 class Wall(RandomizedTile):
39 "cwall": Wall, # rename this everywhere
43 class LevelLoader(ResourceLoader):
49 def _load(self, level_path):
50 f = open(level_path, 'r')
51 level_data = json.load(f)
53 self._height = len(level_data['tiles'])
54 self._width = len(level_data['tiles'][0])
55 self._tiles = level_data['tiles']
56 # Consistency check, so we can assume things are correct
57 # in the level renderer
58 for row, row_data in enumerate(self._tiles):
59 if len(row_data) != self._width:
60 raise RuntimeError("Incorrect len for row %d" % row)
61 self._load_tile_images()
64 def _load_tile_images(self):
65 """Load all the tile images"""
66 for row_data in self._tiles:
68 tile['image'] = TILES[tile['base']].image()
71 levels = LevelLoader('levels')