2

I am making a game using pygame and Tiled to design the map. I am able to load the tiles and blit them on to the screen. When i flip the tile horizontally/rotate inside of Tiled and run the game, the tiles are not rotated inside of the game.

So i would want to check if the tile is rotated/flipped before blitting the tile onto the screen and i am not sure how exactly to do this or whether if this is even possible and proceed to use the tiles without any rotations or flips inside of Tiled?

I read that there are flags you can check for, but at this point i am not sure of that either, any help would be great. Heres the class used to make the map

import pytmx

class TiledMap:
    def __init__(self, filename):
        tm = pytmx.load_pygame(filename)
        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:
                        # check for flags first.. ie rotating, flips
                        # do the neccessary transformations... this part i know
                        # blit the transformed tile

                        # if no flags just blit the image
                        surface.blit(tile, (x * self.tmxdata.tilewidth,
                                                    y * self.tmxdata.tileheight))

how the same tile flipped horizontally appears inside of Tiled

how the image gets loaded when i run the game


Okay so after reading Tile flipping, here's what i understood from it.

  • Get the gid

Already have that from this bit of code for x, y, gid, in layer:

  • Check for the flags

The highest three bits of the gid store the flipped states.

Says that 32nd bit is for horizontal flip. So to check for that we do h_flipped = gid & (1<<31) # correct? But the problem here is when i do that it evaluates to a 0. Printing out the gids gives 1 for the unflipped tile and 2 for the flipped so 2 & (1<<31) # false

The pseudo code was a bit confusing and maybe i understood it incorrectly. So either the gids i am getting is incorrect or the checking for the bit is incorrect? or Both? More help appreciated

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ucy
  • 21
  • 2
  • Which TMX library is the code using? Using `pytmx`, the rotation is handled automatically - requesting the tile image returns a pre-rotated version. I've not used "Simple TMX Library", but this has `tmx.object.properties`. Perhaps the rotation is stored in there, it is certainly one of the class initialisation parameters. – Kingsley Jan 20 '20 at 21:33
  • What is the value of `gid`? Tiled encodes the [flipping flags](https://doc.mapeditor.org/en/latest/reference/tmx-map-format/#tile-flipping) in the global tile ID, so maybe they're still in there. – Thorbjørn Lindeijer Jan 21 '20 at 08:23
  • i am using pytmx. And the value of the gid is 1 for not flipped and 2 for flipped – Ucy Jan 21 '20 at 11:23

0 Answers0