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):
47 class Underground(RandomizedTile):
50 class Tunnel(RandomizedTile):
54 "cwall": Wall, # rename this everywhere
57 "underground": Underground,
60 class LevelLoader(ResourceLoader):
66 def _load(self, level_path):
67 f = open(level_path, 'r')
68 level_data = json.load(f)
70 self._height = len(level_data['tiles'])
71 self._width = len(level_data['tiles'][0])
72 self._tiles = level_data['tiles']
73 self._tileset = level_data['tileset']
74 # Consistency check, so we can assume things are correct
75 # in the level renderer
76 for row, row_data in enumerate(self._tiles):
77 if len(row_data) != self._width:
78 raise RuntimeError("Incorrect len for row %d" % row)
79 for tile in TILES.values():
80 tile.TILESET = self._tileset
81 self._load_tile_images()
84 def _load_tile_images(self):
85 """Load all the tile images"""
86 for row_data in self._tiles:
88 for layer in ['floor', 'tunnels']:
89 tile['%s image' % layer] = TILES[tile[layer]['base']].image()
92 levels = LevelLoader('levels')