Python 3.x 在PyTMX和PyGame中将微小的TileMap缩放到更大的大小
因此,我在PyGame项目中创建、导入和显示了一个16x16的TileMap。 我有一个名为地面的资产层和一个名为对象的对象层 然后,我有一个简单的代码来创建我的TileMap:Python 3.x 在PyTMX和PyGame中将微小的TileMap缩放到更大的大小,python-3.x,pygame,pytmx,Python 3.x,Pygame,Pytmx,因此,我在PyGame项目中创建、导入和显示了一个16x16的TileMap。 我有一个名为地面的资产层和一个名为对象的对象层 然后,我有一个简单的代码来创建我的TileMap: class TiledMap: def __init__(self, filename): tm = pytmx.load_pygame(filename, pixelalpha=True) self.width = tm.width * TILE_SIZE self.height = tm.
class TiledMap:
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.width = tm.width * TILE_SIZE
self.height = tm.height * TILE_SIZE
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:
tile = pg.transform.scale(tile,(TILE_SIZE,TILE_SIZE))
surface.blit(tile, (x * TILE_SIZE,
y * TILE_SIZE))
def make_map(self):
temp_surface = pg.Surface((self.width, self.height), pg.SRCALPHA).convert_alpha()
self.render(temp_surface)
return temp_surface
编辑:我忘了说我的16x16贴图实际上已重新缩放为64x64(平铺大小)图像,但仅针对可见层地面,我还想对对象层执行此操作
这是伟大的工作规模我的“可见层”,这是地面。
但当我绘制碰撞时,您可以看到对象仍然非常小,不适合我的新贴图分辨率:
如您所见,我在TileMap中设置的墙的命中框未正确缩放
所以问题是,如何用pyTMX缩放TileMap的对象层
谢谢大家。所以我找到了一种纠正方法,我不知道这是否是最干净的方法,但下面是解决方案代码:
def new(self):
# Initialization and setup for a new game
self.all_sprites = pg.sprite.LayeredUpdates()
self.walls = pg.sprite.Group()
self.items = pg.sprite.Group()
for tile_object in self.map.tmxdata.objects:
tile_object.x *= int(TILE_SIZE / self.map.tilesize)
tile_object.y *= int(TILE_SIZE / self.map.tilesize)
obj_center = vec(tile_object.x + tile_object.width / 2,
tile_object.y + tile_object.height / 2)
if tile_object.name == 'player':
self.player = Player(self, obj_center.x, obj_center.y)
elif tile_object.name in ['pickaxe']:
Item(self, obj_center, tile_object.name)
else:
tile_object.width *= int(TILE_SIZE / self.map.tilesize)
tile_object.height *= int(TILE_SIZE / self.map.tilesize)
# if tile_object.name == 'zombie':
# Mob(self, tile_object.x, tile_object.y)
if tile_object.name == 'wall':
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
self.camera = Camera(self.map.width, self.map.height)
self.paused = False
self.draw_debug = False
因此,在我的game.new()函数中,我通过解析精灵表来检索玩家的精灵和对象,并且我已经将它们缩放到正确的大小。但对于其他实体的大小和位置,校正只是将值与正确的数字相乘。作为比例因子的数字:16x16到64x64瓷砖组的比例为64/16,即4
所以我只需要将实体x,y,宽度和高度乘以4
希望它能帮助别人