python 在pygame中绘制TMX图块并将其保存为图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在pygame中绘制TMX图块并将其保存为图像相关的知识,希望对你有一定的参考价值。

import pygame, pytmx, argparse

def draw_tmx(tmx, window):
  # For each layer
    for l in xrange(0, len(tmx.tilelayers)):
        # For each y tile coordinate
        for y in xrange(0, tmx.height):
            # For each x tile coordinate
            for x in xrange(0, tmx.width):
                # try not strictly necessary, but a hangover from some old bad code
                try:
                    tile = tmx.getTileImage(x, y, l)
                except Exception, e:
                    print e
                    continue
                # If there is no tile to draw, tile will be 0
                if tile:
                    x_pos = x * tmx.tilewidth
                    # pygame renders tiles from top left and Tiled renders them
                    # from bottom left. This means that if the tile image is
                    # taller than the tile height, pygame will draw it downwards
                    # instead.
                    y_pos = (y * tmx.tileheight) - tile.get_height() + tmx.tileheight
                    window.blit(tile, (x_pos, y_pos))

if __name__ == "__main__":
    p = argparse.ArgumentParser()
    p.add_argument("tmx_file")
    p.add_argument("output_file")
    args = p.parse_args()

    pygame.init()
    # We can't load_pygame yet because we haven't set our display up and we
    # can't do that until we've figured out what size it needs to be..
    tmx = pytmx.tmxloader.load_tmx(args.tmx_file)
    window = pygame.display.set_mode((
      tmx.width * tmx.tilewidth, tmx.height * tmx.tileheight))
    tmx = pytmx.tmxloader.load_pygame(args.tmx_file, pixelalpha=True)
    draw_tmx(tmx, window)
    pygame.display.flip()
    pygame.image.save(window, args.output_file)

以上是关于python 在pygame中绘制TMX图块并将其保存为图像的主要内容,如果未能解决你的问题,请参考以下文章

小记——关于TileMap输出图块索引结构

为啥我的python用pygame做图片移动会留下轨迹?

如何在Word周围绘制边界框并将其保存在文件夹opencv python中

如何使用 Nokogiri 在 TMX 中搜索道具元素

在 C# 中使用 TextureBrush 重复采样图像

在 OpenGL 中重用纹理和顶点