Python Pytmx从平铺贴图渲染动画平铺
所以我已经在一个项目上工作了一段时间了,我真的很想在游戏中加入动画瓷砖。我正在用pygame创建一个2d像素艺术风格的游戏,我正在使用名为Tiled的编辑器创建地图。“平铺”生成用于渲染贴图的.tmx文件和.tsx文件。我已经得到了地图渲染没有任何问题。渲染动画分幅时会出现问题。他们只是没有生气。我了解动画的基本原理。我只需要获得动画的第一个图像,等待帧之间的持续时间,然后渲染下一帧。但我就是不知道如何让它工作。pytmx及其如何从平铺文件读取动画的文档很少 这是.tmx文件:Python Pytmx从平铺贴图渲染动画平铺,python,pygame,tiled,pytmx,Python,Pygame,Tiled,Pytmx,所以我已经在一个项目上工作了一段时间了,我真的很想在游戏中加入动画瓷砖。我正在用pygame创建一个2d像素艺术风格的游戏,我正在使用名为Tiled的编辑器创建地图。“平铺”生成用于渲染贴图的.tmx文件和.tsx文件。我已经得到了地图渲染没有任何问题。渲染动画分幅时会出现问题。他们只是没有生气。我了解动画的基本原理。我只需要获得动画的第一个图像,等待帧之间的持续时间,然后渲染下一帧。但我就是不知道如何让它工作。pytmx及其如何从平铺文件读取动画的文档很少 这是.tmx文件: <?xml
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.4" orientation="orthogonal" renderorder="right-down" width="32" height="32" tilewidth="96" tileheight="96" infinite="0" nextlayerid="5" nextobjectid="5">
<tileset firstgid="1" source="Bigger-Textures(96x96).tsx"/>
<layer id="1" name="Tile Layer 1" width="32" height="32">
<data encoding="csv">
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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5,5,6,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,22,23,23,23,23,23,23,23,23,23,23,24,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
<objectgroup id="4" name="Obstacles">
<object id="1" name="wall" x="0" y="-96" width="3168" height="96"/>
<object id="2" name="wall" x="3072" y="0" width="96" height="3168"/>
<object id="3" name="wall" x="-96" y="3072" width="3168" height="96"/>
<object id="4" name="wall" x="-96" y="-96" width="96" height="3168"/>
</objectgroup>
</map>
这是它在Pytmx github上所说的:
# just iterate over animated tiles and demo them
# tmx_map is a TiledMap object
# tile_properties is a dictionary of all tile properties
# iterate over the tile properties
for gid, props in tmx_map.tile_properties.items():
# iterate over the frames of the animation
# if there is no animation, this list will be empty
for animation_frame in props['frames']:
# do something with the gid and duration of the frame
# this may change in the future, as it is a little awkward now
image = tmx_map.get_tile_image_by_gid(gid)
duration = animation_frame.duration
...
非常感谢您的帮助!
这是一个关于它是否有用的项目:D我今天找这个很开心。其实也没那么复杂 请注意已设置动画的水砖 现在看下面的代码:
def update(self, frame):
if frame in getFrequencyList(6):
self.current_anim_index += 1
if self.current_anim_index == 4:
self.current_anim_index = 0
def getSurface(self):
for layer in self.tmx_data.visible_layers:
for x, y, image in layer.tiles():
for gid, props in self.tmx_data.tile_properties.items():
if image == self.tmx_data.get_tile_image_by_gid(props['frames'][0].gid):
image = self.tmx_data.get_tile_image_by_gid(props['frames'][self.current_anim_index].gid)
self.surface.blit(image, (x * 16, y * 16))
else:
self.surface.blit(image, ((x * 16) + layer.offsetx, (y * 16) + layer.offsety))
return super().getSurface()
按层浏览平铺列表时,可以按位置(x和y)恢复图像。
其原理是,在显示当前图像(平铺)之前,我们检查它实际上不是动画。
为此,您必须获得所有动画瓷砖
我们也是
for gid, props in self.tmx_data.tile_properties.items():
所有动画瓷砖都在道具['frames']中
如果查看tsx文件,可以看到如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.6.0" name="Overworld (Light)"
tilewidth="16" tileheight="16" tilecount="1664" columns="52">
<image source="Overworld (Light).png" trans="ff00ff" width="832" height="512"/>
<tile id="30">
<animation>
<frame tileid="30" duration="250"/>
<frame tileid="82" duration="250"/>
<frame tileid="134" duration="250"/>
<frame tileid="186" duration="250"/>
</animation>
</tile>
...
表示当前图像是动画平铺。在这种情况下,你需要做的就是在你的道具['frame']表中blit一块瓷砖。如您所见,我创建了一个当前动画索引属性。我在更新方法中改变了它。
我确保这在我的游戏循环中被调用。帧参数在0到60之间变化(是的,每秒60帧)。和getFrequencyList(6)返回一个类似于[0,10,20,30,40,50]的表
for gid, props in self.tmx_data.tile_properties.items():
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.6.0" name="Overworld (Light)"
tilewidth="16" tileheight="16" tilecount="1664" columns="52">
<image source="Overworld (Light).png" trans="ff00ff" width="832" height="512"/>
<tile id="30">
<animation>
<frame tileid="30" duration="250"/>
<frame tileid="82" duration="250"/>
<frame tileid="134" duration="250"/>
<frame tileid="186" duration="250"/>
</animation>
</tile>
...
if image == self.tmx_data.get_tile_image_by_gid(props['frames'][0].gid):