1

I am using python cocos2D and am trying to display a map of many tiles. About 100x100 32px tiles. It works alright when I am zoomed in by as soon as I zoom out to view more it gets very choppy.

I also tried using my own sprites and that was even worse. I am not sure why it so bad considering I have seen pygame handle more (and far more complex) sprites than this no problem. Does the tile map version have something to do with the way that I scale?

EDIT: I managed to hugely increase performance using a batch (which I had no idea was a thing and took some looking around to find). That should probably be mentioned somewhere less obscure (unless I am just stupid and missed it). In any case I am still interested in feedback about how to improve this (i.e how many sprites per batch?) and how to improve 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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
John O'Neil
  • 355
  • 1
  • 5
  • 15

0 Answers0