Python Forum
How to display isometric maps with pytmx?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to display isometric maps with pytmx?
#18
Alright, give this a shot:
import pygame as pg
import pytmx
 
WIDTH = 1024   
HEIGHT = 768
TILESIZE = 16
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE

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):
                # top left corner, is in the middle of the screen, at the top
                x_start = (WIDTH / 2)
                y_start = 0

                x_offset = x_start
                y_offset = y_start
                for x, y, gid, in layer:
                    if x == 0:
                        # each new row resets the y-offset, and we back the x-offset a little more to the left
                        # the map is defined as left to right, then top to bottom
                        y_start += self.tmxdata.tileheight
                        x_start -= (self.tmxdata.tilewidth / 2)
                        y_offset = y_start
                        x_offset = x_start

                    tile = ti(gid)
                    if tile:
                        surface.blit(tile, (x_offset, y_offset))
                    
                    # move the offset for the next tile
                    y_offset += self.tmxdata.tileheight
                    x_offset += (self.tmxdata.tilewidth / 2)

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

if __name__ == "__main__":
    pg.init()
    win = pg.display.set_mode((WIDTH, HEIGHT))
    iso = TiledMap("iso.tmx")
    running = True
    while running:
        for ev in pg.event.get():
            if pg.QUIT == ev.type:
                running = False
        
        iso.render(win)
        pg.display.flip()
The map is defined in a left to right, then top to bottom fashion. So (0, 0) is the "top left" tile, which should be rendered at the very top of the window, center screen. By doing a little calculation to get the center of the window (WIDTH / 2), all that's left is to move the offset by a tile (or half a tile, for the x-axis) to get the position of the next tile, and then reset the offsets for the next "row".
mattwins likes this post
Reply


Messages In This Thread
RE: How to display isometric maps with pytmx? - by nilamo - Feb-19-2020, 07:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Isometric game pygame Tiled howardberger 1 677 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,399 Nov-02-2021, 06:56 AM
Last Post: Josselin
Thumbs Up [PyGame] Simple code for isometric 2D games ThePhi 1 15,400 Nov-17-2020, 12:58 PM
Last Post: mattwins
  [split] How to display isometric maps with pygame? mattwins 6 6,359 Nov-17-2020, 12:54 PM
Last Post: mattwins
  Problems with pytmx Piethon 15 9,579 Jan-05-2020, 10:44 AM
Last Post: Piethon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020