vendredi 12 juin 2020

How to select a random tmx map to be loaded? Opening a random file in pygame?

Trying to select a random map from my maps folder when certain conditions are met, how would I go about selecting a new map.

MAP = ['map.tmx', 'map2.tmx', 'map3.tmx']

self.map_folder = path.join(game_folder, 'maps')
self.map = TiledMap(path.join(self.map_folder, MAP))

class TiledMap:
    def __init__(self, filename):
        tm = pytmx.load_pygame(filename, pixelalpha=True)
        self.width = tm.width * tm.tilewidth
        self.height = tm.height * tm.tileheight
        self.tmxdata = tm

    def render(self, surface):
        ti = self.tmxdata.get_tile_image_by_gid
        for layer in self.tmxdata.visible_layers:
            if isinstance(layer, pytmx.TiledTileLayer):
                for x, y, gid, in layer:
                    tile = ti(gid)
                    if tile:
                        surface.blit(tile, (x * self.tmxdata.tilewidth,
                                            y * self.tmxdata.tileheight))

    def make_map(self):
        surface = py.Surface((self.width, self.height))
        self.render(surface)
        return surface



Aucun commentaire:

Enregistrer un commentaire