【问题标题】:Pygame Load Tilemap With PyTMXPygame 使用 PyTMX 加载 Tilemap
【发布时间】:2018-02-09 23:48:55
【问题描述】:

我正在尝试了解 pygame 中的 tilemaps,并学习如何使用 Tiled Map Editor,但我无法成功在 pygame 中加载地图。 代码如下:

import pygame
import pytmx

pygame.init()

display = pygame.display.set_mode((600,400))
clock = pygame.time.Clock()

gameMap = pytmx.TiledMap("map.tmx")

while(True):

    clock.tick(60)
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            quit()

    if(keys[pygame.K_ESCAPE]):
        quit()

    for layer in gameMap.visible_layers:
            for x, y, gid, in layer:
                tile = gameMap.get_tile_image_by_gid(gid)
                if(tile != None):
                    display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))

    pygame.display.update()

它一直给我这个错误:

Traceback(最近一次调用最后一次): 文件“main.py”,第 27 行,在 display.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight)) TypeError: 参数 1 必须是 pygame.Surface,而不是元组

我知道当我在控制台中打印磁贴时,这就是我得到的

(一个元组): ('img/NES - Super Mario Bros - Tileset.png', (0, 144, 16, 16), TileFlags(flipped_horizontally=False, flipped_vertically=False, flipped_diagonally=False))

在pygame中成功加载tilemap的最简单方法是什么?我做错了什么?

【问题讨论】:

  • 我可以确认您的错误。 displaySurface 对象,tile 不是。该错误告诉您将错误类型的对象作为 arg 传递给 display.blittile<class 'tuple'>: ('Tiled Images/background.png', (0, 0, 160, 96), TileFlags(flipped_horizontally=False, flipped_vertically=False, flipped_diagonally=False)) 如果您将 tile 替换为 display,您将获得一些要筛选的活动。
  • 谢谢。那么如何让它发挥作用呢?
  • 我以前从未使用过这个。我现在正在看。如果您在display.blit 之后输入time.sleep(6),您将看到pygame 窗口渲染到您的屏幕上......
  • 出现窗口,但我想在窗口中渲染地图的瓦片。
  • 我知道。给我一点时间......我正在学习这个......

标签: python python-3.x pygame tile pytmx


【解决方案1】:

使用:

gameMap = pytmx.load_pygame("map.tmx")

代替:

gameMap = pytmx.TiledMap("map.tmx")

【讨论】:

    【解决方案2】:

    我认为以下内容涵盖了使用 updateloadMap 渲染 mapSurface 并在其上绘制内容的大部分机制。我没有任何真正的 TMX 文件可供测试。 ymmv。祝你好运。

    import time
    import pygame
    import pytmx
    
    
    class TiledMap():
    """ This is creating the surface on which you make the draw updates """
        def __init__(self):
            self.gameMap = pytmx.load_pygame("map.tmx", pixelalpha=True)
            self.mapwidth = self.gameMap.tilewidth * self.gameMap.width
            self.mapheight = self.gameMap.tileheight * self.gameMap.height
    
        def render(self, surface):
            for layer in self.gameMap.visible_layers:
                if isinstance(layer, pytmx.TiledTileLayer):
                    for x, y, gid in layer:
                        tile = self.gameMap.get_tile_image_by_gid(gid)
                        if tile:
                            surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))
    
        def make_map(self):
            mapSurface = pygame.Surface((self.mapwidth, self.mapheight))
            self.render(mapSurface)
            return mapSurface
    
    pygame.init()
    
    
    class Display():
    """ This is the class that makes the changes that you want to display. You would add most of your changes here. """
    
        def __init__(self):
    
            self.displayRunning = True
            self.displayWindow = pygame.display.set_mode((600, 400))
            self.clock = pygame.time.Clock()
    
        def update(self):
    
            pygame.display.set_caption("{:.2f}".format(self.clock.get_fps()))
            pygame.display.update()
    
        def loadMap(self):
    
            self.map = TiledMap()
            self.map_img = self.map.make_map()
            self.map_rect = self.map_img.get_rect()
    
        def displayLoop(self):
    
            self.clock.tick()
            self.update()
            self.loadMap()
    
    # Here is the start of the main driver
    
    
    runDisplay = Display()
    
    runDisplay.update()
    runDisplay.loadMap()
    time.sleep(60)
    

    如果您希望它在循环中运行,那么您可以将底部驱动程序块更改为:

    runDisplay = Display()
    while runDisplay.displayRunning is True:
        runDisplay.displayLoop()
    

    【讨论】:

    • 哇。谢谢你。现在它正在工作。它加载地图。非常感谢你。我真的认为它不会起作用。
    • 但是你知道为什么在我的代码中,tile 不被认为是 pygame.Surface 而在你的例子中它是?
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2021-06-21
    • 2019-09-24
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多