1 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
5 from pgzero.loaders import images, ResourceLoader
8 from pygame.transform import rotate
16 raise NotImplementedError()
18 return images.load(cls.IMG)
20 class RandomizedTile(Tile):
26 if cls.IMGDIR is None:
27 raise NotImplementedError()
29 imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
30 imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
31 img = images.load(os.path.join(cls.IMGDIR, imgpath))
34 img = rotate(img, 90 * random.randint(0, 3))
38 class Floor(RandomizedTile):
41 class Wall(RandomizedTile):
45 "cwall": Wall, # rename this everywhere
49 class LevelLoader(ResourceLoader):
55 def _load(self, level_path):
56 f = open(level_path, 'r')
57 level_data = json.load(f)
59 self._height = len(level_data['tiles'])
60 self._width = len(level_data['tiles'][0])
61 self._tiles = level_data['tiles']
62 # Consistency check, so we can assume things are correct
63 # in the level renderer
64 for row, row_data in enumerate(self._tiles):
65 if len(row_data) != self._width:
66 raise RuntimeError("Incorrect len for row %d" % row)
67 self._load_tile_images()
70 def _load_tile_images(self):
71 """Load all the tile images"""
72 for row_data in self._tiles:
74 tile['image'] = TILES[tile['base']].image()
77 levels = LevelLoader('levels')