1 """Loader a level, using the pygame-zero ResourceLoader infrastructure"""
6 from pgzero.loaders import images, ResourceLoader
9 from pygame.transform import rotate
17 if cls.IMG is None or cls.TILESET is None:
18 raise NotImplementedError()
19 return images.load(os.path.join(cls.TILESET, cls.IMG))
21 class RandomizedTile(Tile):
28 if cls.IMGDIR is None or cls.TILESET is None:
29 raise NotImplementedError()
31 imgdir = os.path.join(os.path.dirname(__file__), '..', 'images',
32 cls.TILESET, cls.IMGDIR)
33 imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
34 img = images.load(os.path.join(cls.TILESET, cls.IMGDIR, imgpath))
37 img = rotate(img, 90 * random.randint(0, 3))
41 class Floor(RandomizedTile):
44 class Wall(RandomizedTile):
48 "cwall": Wall, # rename this everywhere
52 class LevelLoader(ResourceLoader):
58 def _load(self, level_path):
59 f = open(level_path, 'r')
60 level_data = json.load(f)
62 self._height = len(level_data['tiles'])
63 self._width = len(level_data['tiles'][0])
64 self._tiles = level_data['tiles']
65 self._tileset = level_data['tileset']
66 # Consistency check, so we can assume things are correct
67 # in the level renderer
68 for row, row_data in enumerate(self._tiles):
69 if len(row_data) != self._width:
70 raise RuntimeError("Incorrect len for row %d" % row)
71 for tile in TILES.values():
72 tile.TILESET = self._tileset
73 self._load_tile_images()
76 def _load_tile_images(self):
77 """Load all the tile images"""
78 for row_data in self._tiles:
80 for layer in ['floor', 'tunnels']:
81 tile['%s image' % layer] = TILES[tile[layer]['base']].image()
84 levels = LevelLoader('levels')