1 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
6 from pgzero.loaders import images, ResourceLoader
9 from pygame.transform import rotate
17 raise NotImplementedError()
19 return images.load(cls.IMG)
21 class RandomizedTile(Tile):
27 if cls.IMGDIR is None:
28 raise NotImplementedError()
30 imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
31 imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
32 img = images.load(os.path.join(cls.IMGDIR, imgpath))
35 img = rotate(img, 90 * random.randint(0, 3))
39 class Floor(RandomizedTile):
42 class Wall(RandomizedTile):
46 "cwall": Wall, # rename this everywhere
50 class LevelLoader(ResourceLoader):
56 def _load(self, level_path):
57 f = open(level_path, 'r')
58 level_data = json.load(f)
60 self._height = len(level_data['tiles'])
61 self._width = len(level_data['tiles'][0])
62 self._tiles = level_data['tiles']
63 self._tileset = level_data['tileset']
64 # Consistency check, so we can assume things are correct
65 # in the level renderer
66 for row, row_data in enumerate(self._tiles):
67 if len(row_data) != self._width:
68 raise RuntimeError("Incorrect len for row %d" % row)
69 self._load_tile_images()
72 def _load_tile_images(self):
73 """Load all the tile images"""
74 for row_data in self._tiles:
76 tile['image'] = TILES[tile['base']].image()
79 levels = LevelLoader('levels')