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?
#12
(Feb-10-2020, 07:23 PM)nilamo Wrote: Could you share the Tiled map file?
It looks like they're being rendered in the right order, there's just a missing offset, and the number of tiles per row is off. I'm not sure if that info is available in the file itself, or if it needs to be calculated, and I'd like to play around with it.

Although it is already in my first post in this thread, here you go, in it's full version. (Not meant mean! Smile )
NOTE: Some of the information in there is not necessary for the map to load (I think):

import pygame as pg
import pytmx
from settings import *

def collide_hit_rect(one, two):
    return one.hit_rect.colliderect(two.rect)

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):
        temp_surface = pg.Surface((self.width, self.height))
        self.render(temp_surface)
        return temp_surface

class Camera:
    def __init__(self, width, height):
        self.camera = pg.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        return entity.rect.move(self.camera.topleft)

    def apply_rect(self, rect):
        return rect.move(self.camera.topleft)

    def update(self, target):
        x = -target.rect.centerx + int(WIDTH / 2)
        y = -target.rect.centery + int(HEIGHT / 2)

        # limit scrolling to map size
        x = min(0, x)  # left
        y = min(0, y)  # top
        x = max(-(self.width - WIDTH), x)  # right
        y = max(-(self.height - HEIGHT), y)  # bottom
        self.camera = pg.Rect(x, y, self.width, self.height)
Hope you can figure out my problem now. Thanks for your help! Smile
Reply


Messages In This Thread
RE: How to display isometric maps with pytmx? - by Piethon - Feb-11-2020, 06:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Isometric game pygame Tiled howardberger 1 1,001 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,513 Nov-02-2021, 06:56 AM
Last Post: Josselin
Thumbs Up [PyGame] Simple code for isometric 2D games ThePhi 1 15,642 Nov-17-2020, 12:58 PM
Last Post: mattwins
  [split] How to display isometric maps with pygame? mattwins 6 6,624 Nov-17-2020, 12:54 PM
Last Post: mattwins
  Problems with pytmx Piethon 15 9,913 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