我正在使用pytmx加载我的地图与pygame,但一些项目需要设置为精灵并显示在表面上,所以分层不会停留,我将向你展示两个例子,一个是玩家显示在装饰物上(点层在装饰物下),另一个是门显示在墙上。
我尝试用我的播放器和门对象刷新我的地图,通过重写make_map()和render(表面)函数从渲染器,但不是传递对象,我迭代抛出它们到blit它是一个,它有显示,但仍然没有分层,也一半每秒的速度下降。
如果任何人知道如何能够保持分层,即使与播放器和其他对象用于渲染动画精灵,我将很高兴得到您的帮助。




这是我每次尝试使用分层刷新地图的代码(它是显示的,但它的结果完全相同,就像我只绘制表面,然后在上面绘制精灵一样):
def draw(self, display, camera = None):
self.surface = self.make_map()
self.rect = self.surface.get_rect()
if not camera:
display.blit(self.surface, self.rect)
else:
display.blit(self.surface, (self.rect.x - camera.offset.x, self.rect.y - camera.offset.y))
def render(self, surface):
tw = self.tmx_data.tilewidth
th = self.tmx_data.tileheight
if self.tmx_data.background_color:
surface.fill(self.tmx_data.background_color)
else:
surface.fill((0, 0, 0))
for layer in self.tmx_data.layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, image in layer.tiles():
if image:
surface.blit(image.convert_alpha() , (x * tw, y * th))
elif isinstance(layer, pytmx.TiledObjectGroup):
for tile_object in layer:
if tile_object.name == 'player':
surface.blit(self.level.game.player.image.convert_alpha(), (self.level.game.player.rect.x, self.level.game.player.rect.y))
if tile_object.name == 'door':
for door in self.level.game.doors:
if door.type == tile_object.type:
surface.blit(door.image.convert_alpha(), (door.rect.x, door.rect.y))
elif isinstance(layer, pytmx.TiledImageLayer):
if image:
surface.blit(image , (0, 0))
def make_map(self):
temp_surface = pygame.Surface(self.renderer.size)
self.render(temp_surface)
return temp_surface发布于 2021-07-17 17:33:55
所以我找到了一种方法来获得我想要的,我已经使用了pyagme精灵组layeredUpdates,我添加到我的原始地图只有地板,我创建了分层精灵与我的其他图层(墙,装饰),然后显示与图层
地图渲染
def render(self, surface):
tw = self.tmx_data.tilewidth
th = self.tmx_data.tileheight
if self.tmx_data.background_color:
surface.fill(self.tmx_data.background_color)
else:
surface.fill((0, 0, 0))
for layer in self.tmx_data.layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, image in layer.tiles():
if image:
if layer.name == 'walls' or 'wall' in layer.name:
Wall(self.game, x * tw, y * th, image.convert_alpha())
elif 'decoration' in layer.name:
Decoration(self.game, x * tw, y * th, image.convert_alpha())
else:
surface.blit(image.convert_alpha() , (x * tw, y * th))
elif isinstance(layer, pytmx.TiledObjectGroup):
pass
elif isinstance(layer, pytmx.TiledImageLayer):
if image:
surface.blit(image , (0, 0))
def make_map(self):
temp_surface = pygame.Surface(self.size)
self.render(temp_surface)
return temp_surface对象:
class Decoration(pygame.sprite.Sprite):
def __init__(self, game, x, y, image, layer = DECORATION_TOP_LAYER):
self.groups = game.all_sprites, game.decorations
self._layer = layer
pygame.sprite.Sprite.__init__(self, self.groups)
class Wall(pygame.sprite.Sprite):
def __init__(self, game, x, y, image):
self.groups = game.all_sprites, game.walls
self._layer = WALL_LAYER
pygame.sprite.Sprite.__init__(self, self.groups)
class Door(pygame.sprite.Sprite):
def __init__(self, game, x, y, w, h, open_actions, animated_image, type):
self._layer = DOOR_LAYER
self.groups = game.all_sprites, game.doors
pygame.sprite.Sprite.__init__(self, self.groups)
class NPC(pygame.sprite.Sprite):
def __init__(self, game, x, y, w, h, animated_image):
self.groups = game.all_sprites, game.NPCs
self._layer = NPC_LAYER
pygame.sprite.Sprite.__init__(self, self.groups)而让所有东西协同工作的原因很简单:
self.all_sprites = pygame.sprite.LayeredUpdates()https://stackoverflow.com/questions/68385209
复制相似问题