1

So I managed to create, import and display a 16x16 TileMap in my PyGame project. I have my asset layer called ground and an Objects layer originally called objects.

Tile software screenshot with my layers

Then I've got this simple code to create my TileMap :

class TiledMap: def __init__(self, filename): tm = pytmx.load_pygame(filename, pixelalpha=True) self.width = tm.width * TILE_SIZE self.height = tm.height * TILE_SIZE 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: tile = pg.transform.scale(tile,(TILE_SIZE,TILE_SIZE)) surface.blit(tile, (x * TILE_SIZE, y * TILE_SIZE)) def make_map(self): temp_surface = pg.Surface((self.width, self.height), pg.SRCALPHA).convert_alpha() self.render(temp_surface) return temp_surface 

EDIT: I've forgot to say that my 16x16 map is actually rescaled to a 64x64 (TILE_SIZE) image, but only for the visible layer ground, I want to do it with the objects layer too.

This is working great to scale my "visible layer" which is ground. But when I draw the collisions you can see that the objects are still really small and don't fit my new map resolution :

Game screenshot with in Blue:Player hit box and in Yellow:Colliders

As you can see the hit boxes of the walls I've set in my TileMap are not correctly scaled.

So question is, how to scale the Objects layer of a TileMap with pyTMX ?

Thank you all.

1 Answer 1

1

So I found a way to correct it, I don't know if it's the cleanest way to do it but here is the solution code :

def new(self): # Initialization and setup for a new game self.all_sprites = pg.sprite.LayeredUpdates() self.walls = pg.sprite.Group() self.items = pg.sprite.Group() for tile_object in self.map.tmxdata.objects: tile_object.x *= int(TILE_SIZE / self.map.tilesize) tile_object.y *= int(TILE_SIZE / self.map.tilesize) obj_center = vec(tile_object.x + tile_object.width / 2, tile_object.y + tile_object.height / 2) if tile_object.name == 'player': self.player = Player(self, obj_center.x, obj_center.y) elif tile_object.name in ['pickaxe']: Item(self, obj_center, tile_object.name) else: tile_object.width *= int(TILE_SIZE / self.map.tilesize) tile_object.height *= int(TILE_SIZE / self.map.tilesize) # if tile_object.name == 'zombie': # Mob(self, tile_object.x, tile_object.y) if tile_object.name == 'wall': Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height) self.camera = Camera(self.map.width, self.map.height) self.paused = False self.draw_debug = False 

So in my game.new() function I'm retrieving the sprites of the player and the objects by parsing a spritesheet and I already scale them to the correct size. But for the other entities size and for position the correction is simply to multiply the values with the correct number. Number which is the scaling factor : 16x16 to a 64x64 tilesets gives 64/16 which is 4.

So I just had to multiply the entities x, y, width and height by 4.

Hope it will help someone anyway.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.