python - 使用 Pytmx 处理图 block 对象

标签 python pygame pytmx

所以我正在尝试构建横向卷轴平台游戏,并使用 Tiled Map Editor 创建了一张 map 。我已经使用我编写的以下类成功地将非平铺对象和平铺加载到我的游戏中:

 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

现在我正在尝试加载我的背景图像,根据 Tile Map Editor 文档,我已经将其制作成一个大的图 block 对象并放入背景层中。但我不知道如何使用 Pytmx 加载平铺对象,我试着查看源代码,它似乎支持平铺对象。我知道这些平铺对象有一个 gid 属性,但我不确定如何使用它加载平铺对象图像。

而且我是 pygame 和 pytmx 的新手,但不一定是 python 的新手。谢谢!

最佳答案

我通过阅读 Pytmx 源代码并进行尝试找到了解决方案。所以这是我用来读取图 block 对象的代码。

    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
                )

本质上,循环遍历你的对象,因为这是一个 tile 对象,它有一个 gid 属性。获取带有 gid 的图像,然后我创建了一个矩形,这样我就可以将我的相机应用于背景(用于视差效果)。然后你 blit 图像和矩形就可以了。

此外,我在渲染我的瓦片 map 时必须包含一个 pg.SRCALPHA 标志,这样它看起来才正确。

关于python - 使用 Pytmx 处理图 block 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865612/

相关文章:

python - 我似乎无法使用控件让我的 Sprite 旋转

python - pygame中Tiled和pytmx的透明图 block 问题

python - 将日期范围与 Python 日期列表中的索引进行匹配的快速方法

python - 如何获取使用 Youtube-dl 下载的文件的文件名

python - Pygame - 将表面存储为图像

python - 收到错误消息说 super 需要 1 个参数,但给出了 0 个参数?

python - 如何在 pygame 中使图 block map 滚动?

python - Pygame 使用 PyTMX 加载 Tilemap

python - 使用 Python 创建 3D 数组

python - Apache 上的 Django 与 mod_wsgi CSRF 验证失败