使用嵌套循环绘制时如何提高性能?

时间:2014-05-23 00:09:57

标签: python performance pygame nested-loops

我开发了一个程序,用程序生成一个带有迷宫的地牢:

entities = {}
dungeonSize = 8
currentDungeon = (randint(0,dungeonSize-1),randint(0,dungeonSize-1))
lOR = 16 # Level of Randomness.

def gD(): # Generate Dungeon

    for i in range(dungeonSize):
        for j in range(dungeonSize):
            entities[(j,i)] = {}

    for dungeon in entities:
        r = randint(0,lOR)
        for k in range(16):
            for l in range(16):
                if randint(0,lOR*2) == 0:
                    for m in range(r):
                        entities[dungeon][((((l+m)*brickWidth),(k*brickHeight)))] = {"source":"brick.jpg",
                                            "info":["dungeon wall",u"parede da masmorra"]}     
                elif randint(0,lOR*2) == 1:
                    for m in range(r):
                        entities[dungeon][(((l*brickWidth),(k+m)*brickHeight))] = {"source":"brick.jpg",
                                            "info":["dungeon wall",u"parede da masmorra"]}

碰巧我需要在每个帧(地面的每个瓦片和每个墙壁)绘制当前的屏幕/迷宫。在这种情况下,碰巧真正复杂的是跟踪已经改变的瓦片或类似的东西以节省一些内存。

这是主循环的绘图程序:

def dCD(): # Draw Current Dungeon

    for wall in entities[currentDungeon]:
        gIS("GFX",entities[currentDungeon][wall]["source"],wall[0],wall[1])

地面也在每一帧画出(我不能使用单个图像,因为我会创建随机种类的图块)。

def fWG(): # Fill with Ground

    for i in range(16):
        for j in range(16):
            gIS("GFX",ground,j*brickWidth,i*brickHeight)

有没有办法在Pygame中“释放一个Surface”,或者无论如何都要将所有这些表面加入blit并拥有20+ FPS? (我目前有8个FPS。真的很迟钝!)。如果没有,你建议什么?用C语言编写游戏,思考一个更聪明的算法来神奇地替换和擦除并重新利用每帧的图块?

0 个答案:

没有答案