Handling tile objects using Pytmx

1.3k views Asked by At

so I am trying to build a side-scrolling platform game and have created a map using the Tiled Map Editor. I have succesfully loaded in non-tiled objects and tiles into my game using the following class I wrote:

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

    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_bitmap = self.tmxdata.get_tile_image_by_gid(gid)
                    if tile_bitmap:
                        surface.blit(   
                            tile_bitmap,
                            (x * self.tmxdata.tilewidth, y * self.tmxdata.tileheight),
                        )

            # This doesn't work but I tried to do this

            elif isinstance(layer, pytmx.TiledObject):
                for x, y, gid in layer:
                    for objects in self.tmxdata.objects:
                        if objects.name == "Background":
                            img_bitmap = self.tmxdata.get_tile_image_by_gid(gid)

                            surface.blit(img_bitmap, (objects.x, objects.y))

    def make_map(self):
        temp_surface = pg.Surface((self.width, self.height))
        self.render(temp_surface)
        return temp_surface

And now I am trying to load my background image which, per the Tile Map Editor documentation, I have made into a large tile object and put in the background layer. But I don't know how to load a tiled object using Pytmx, I tried looking at the source code and it does seem to have support for tiled objects. I know these tiled objects had a gid property but am not sure how to load the tile object image using that.

And I am new to pygame and pytmx but not necessarily new to python. Thanks!

1

There are 1 answers

0
MBA20 On BEST ANSWER

I found the solution by reading the Pytmx source code and trying things out. So this is code I use to read tile objects.

    for tile_object in self.map.tmxdata.objects:
            if tile_object.name == "Player":
                self.player = Player(self, tile_object.x, tile_object.y)
            if tile_object.name == "Platform":
                TiledPlatform(
                    self,
                    tile_object.x,
                    tile_object.y,
                    tile_object.width,
                    tile_object.height,
                )
            if tile_object.name == "Background":
                self.img_bitmap = self.map.tmxdata.get_tile_image_by_gid(
                    tile_object.gid
                )

                self.temp_rect = pg.Rect(
                    tile_object.x, tile_object.y, tile_object.width, tile_object.height
                )

Essentially, looping through your objects and because this is a tile object, it has a gid property. Get the image with the gid and I created a rectangle so I can apply my camera to the background (for a parallax effect). Then you blit the image, and rectangle and there you go.

Also, I had to include a pg.SRCALPHA flag when rendering my tile map so it looked right.