使用相对较少的精灵和中等大小的平铺贴图时性能较差?

2024-04-25 07:47:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python cocos2D并尝试显示一个包含许多块的地图。约100x100 32px瓷砖。它工作正常,当我放大,一旦我缩小,以查看更多它变得非常波涛汹涌。你知道吗

我也试着用我自己的精灵,那就更糟了。我不知道为什么这么糟糕,因为我见过pygame处理比这个更复杂的精灵,没问题。平铺地图版本是否与我缩放的方式有关?你知道吗

编辑:我使用批处理成功地极大地提高了性能(我不知道这是一件事,四处寻找才发现)。也许应该在不那么隐晦的地方提一下(除非我太蠢了,错过了它)。在任何情况下,我仍然感兴趣的反馈如何改善这一点(即有多少精灵每批?)如何改进tilemap?你知道吗

class Tile(cocos.sprite.Sprite):
    def __init__(self, image, position):
        super().__init__(image, position=position)

class GridLayer(cocos.layer.ScrollableLayer):
    def __init__(self):
        super().__init__()

        for x in range(50):
            for y in range(50):
                self.add(Tile('tile.png', (x*32, y*32)))



class Scroll(cocos.layer.ScrollingManager):
    is_event_handler = True

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        win_size = Vector2(*cocos.director.director.get_window_size())

        self.set_focus(win_size.x / 2, win_size.y / 2)

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if buttons == 1:
            a, b = self.fx - dx, self.fy - dy
            self.force_focus(a, b)

    def on_mouse_scroll(self, x, y, buttons, dir):
        if self.scale < 1: dir *= self.scale
        self.scale += dir * 0.1


def main():
    cocos.director.director.init(1024, 600, resizable=True)

    scroll = Scroll()
    # This is the time map version:
    grid = cocos.tiles.load('tmp.tmx')['Tile Layer 1']
    scroll.add(grid)

    # This is the custom sprite version:
    # scroll.add(GridLayer())


    main_scene = cocos.scene.Scene(scroll)
    cocos.director.director.run(main_scene)

if __name__ == '__main__':
    main()

Tags: selfaddsizeinitismaindefposition