import json
from pgzero.loaders import images, ResourceLoader
+import os
+import random
+from pygame.transform import rotate
+class Tile:
+ IMG = None
+
+ @classmethod
+ def image(cls):
+ if cls.IMG is None:
+ raise NotImplementedError()
+
+ return images.load(cls.IMG)
+
+class RandomizedTile(Tile):
+ IMGDIR = None
+ ROTATE = True
+
+ @classmethod
+ def image(cls):
+ if cls.IMGDIR is None:
+ raise NotImplementedError()
+
+ imgdir = os.path.join(os.path.dirname(__file__), '..', 'images', cls.IMGDIR)
+ imgpath = os.path.splitext(random.choice(os.listdir(imgdir)))[0]
+ img = images.load(os.path.join(cls.IMGDIR, imgpath))
+
+ if cls.ROTATE:
+ img = rotate(img, 90 * random.randint(0, 3))
+
+ return img
+
+class Floor(RandomizedTile):
+ IMGDIR = "floor"
+
+class Wall(RandomizedTile):
+ IMGDIR = "wall"
+
+TILES = {
+ "cwall": Wall, # rename this everywhere
+ "floor": Floor,
+}
class LevelLoader(ResourceLoader):
""" Level loader. """
"""Load all the tile images"""
for row_data in self._tiles:
for tile in row_data:
- tile['image'] = images.load(tile['base'])
+ tile['image'] = TILES[tile['base']].image()
levels = LevelLoader('levels')