因此,我设法在我的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.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 (TILE_SIZE)图像,但只有在可见层地面上,我也想用对象层来实现。
这是伟大的工作,以扩大我的“可见层”,这是地面。但当我画出碰撞图时,你可以看到物体仍然很小,不符合我的新地图分辨率:
正如您所看到的,我在我的中设置的的命中框没有正确地缩放。
因此,问题是,如何使用TileMap的对象层与pyTMX进行缩放?
谢谢你们所有人。
发布于 2020-02-07 13:22:54
所以我找到了一种方法来纠正它,我不知道这是否是最干净的方法,但是下面是解决方案代码:
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()函数中,我通过解析一个spritesheet来检索播放器和对象的size,并且我已经将它们缩放到了正确的大小。但是对于其他实体的大小和位置,校正只是将值与正确的数字相乘。数字,它是缩放因子: 16x16到64x64分式,给出64/16,也就是4。
所以我只需要把x,y,宽度和高度乘以4。
希望它能对别人有帮助。
https://stackoverflow.com/questions/60110290
复制相似问题