0

我正在使用 pytmx 用 pygame 加载我的地​​图,但是有些项目需要设置为精灵并显示在表面上方,所以分层不会留下来,我将向您展示 2 个示例,一个是玩家显示在装饰物的顶部(在装饰下的点层)另一个是显示在墙壁上方的门。

我试图通过重写渲染器中的 make_map() 和 render(surface) 函数来用我的播放器和门对象刷新我的地图,但是我没有传递对象,而是将它们扔到了原来的 blit 上,它有显示,但仍然没有分层,还有一半的 FPS 下降。

如果有人知道如何在播放器和其他用于渲染动画精灵的对象的情况下保持分层,我很乐意得到您的帮助。

玩家在装饰品上 在我的地图编辑器中分层 我的游戏中的门 地图编辑器中的门

这是我尝试使用分层每次刷新地图的代码(它正在显示,但它具有完全相同的结果,就好像我只绘制表面然后在上面绘制精灵一样):


    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
    
4

1 回答 1

0

所以我找到了一种方法来获得我想要的东西,我使用了 pyagme sprites groups 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()

于 2021-07-17T09:33:54.663 回答