Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 游戏真的很慢_Python_Pygame_Lag - Fatal编程技术网

Python 游戏真的很慢

Python 游戏真的很慢,python,pygame,lag,Python,Pygame,Lag,我有一个pygame程序。我有许多不同的部分,其中两部分如下所示。在第二个游戏中,游戏以正常的速度进行,但在第一个游戏中,它非常滞后,尽管它的滴答声速度相同。有什么我遗漏的吗?顺便说一下,每个游戏循环周期只执行其中一个。请注意,以结尾的行在这两行之间重复 for event in pygame.event.get():# if event.type==pygame.QUIT:# pygame.quit()# sys.exit()# if event

我有一个pygame程序。我有许多不同的部分,其中两部分如下所示。在第二个游戏中,游戏以正常的速度进行,但在第一个游戏中,它非常滞后,尽管它的滴答声速度相同。有什么我遗漏的吗?顺便说一下,每个游戏循环周期只执行其中一个。请注意,以结尾的行在这两行之间重复

for event in pygame.event.get():#
    if event.type==pygame.QUIT:#
        pygame.quit()#
        sys.exit()#
    if event.type==pygame.KEYDOWN:#
        if event.key==pygame.K_ESCAPE:#
            pygame.quit()#
            sys.exit()#
for ball in balls:#
    ball.update(winrect, walls)#
window.fill(WHITE)#
for box in boxes:#
    pygame.draw.rect(window, box[1], box[0])#
for wall in walls:#
    if wall.orientation==0:#
        pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.ttopleft, wall.height))#
        pygame.draw.rect(window, BLACK, (wall.bbottomright, wall.top, wall.right-wall.bbottomright, wall.height))#
    else:#
        pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.width, wall.ttopleft))#
        pygame.draw.rect(window, BLACK, (wall.left, wall.bbottomright, wall.width, wall.bottom-wall.holesize))#
for ball in balls:#
    pygame.draw.circle(window, ball.color, ball.center, round(ball.width/2))#
    pygame.draw.circle(window, BLACK, ball.center, round(ball.width/2), 2)#
window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])
pygame.display.update()#
pygame.time.Clock().tick(100)#
第二个:

    #event loop
    for event in pygame.event.get():#
        if event.type==pygame.QUIT:#
            pygame.quit()#
            sys.exit()#
        if event.type==pygame.KEYDOWN:#
            if event.key==pygame.K_ESCAPE:#
                mode='pause'#
    #updates
    updates=[]
    for wall in walls:
        wall.update()
    for ball in balls:#
        updates.append(ball.update(winrect, walls))#similar
    #Seeing if won
    won=True
    for update in updates:
        if not update:
            won=False
    if won:
        if levels[loadinglevel][4]==0:
            levels[loadinglevel][4]=1
        levels[loadinglevel-1][4]=2
        mode='complete'
        stuff=getcomplete(loadinglevel, coins, bigfont, texts['complete'][1].bottom+100, winrect.centerx)
        for wall in walls:
            wall.bbottomright=100000
            wall.ttopleft=90000
        coins+=loadinglevel
    #blitting
    window.fill(WHITE)#
    for box in boxes:#
        pygame.draw.rect(window, box[1], box[0])#
    for wall in walls:#
        if wall.orientation==0:#
            pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.ttopleft, wall.height))#
            pygame.draw.rect(window, BLACK, (wall.bbottomright, wall.top, wall.right-wall.bbottomright, wall.height))#
        else:#
            pygame.draw.rect(window, BLACK, (wall.left, wall.top, wall.width, wall.ttopleft))#
            pygame.draw.rect(window, BLACK, (wall.left, wall.bbottomright, wall.width, wall.bottom-wall.holesize))#
    for ball in balls:#
        pygame.draw.circle(window, ball.color, ball.center, round(ball.width/2))#
        pygame.draw.circle(window, BLACK, ball.center, round(ball.width/2), 2)#
    pygame.display.update()#
    pygame.time.Clock().tick(100)#
    if mode=='pause':
        window.blit(coverso, winrect)

第一部分中唯一不重复的行是:

window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])
如果不使用alpha,建议在创建曲面时使用surface.convert

如果您使用alpha If,则可以使用颜色关键点,因为它们比alpha曲面快得多

为了加快计算速度,我建议使用Psyco


除了alexpinho98所说的,您不必对alpha曲面使用颜色键。您可以改用surface.convert_alpha

使用以下代码可以节省一些时间并避免重复键入。转换\u alpha:

def loadify(imgname):
    return pygame.image.load(imagename).convert_alpha()

你试过运行cProfile来确定问题的功能吗?@Mike,我以前从未听说过。你能把我要做的事情放到其中一个节目里吗?谢谢。不知道发生了什么事。查看并找到瓶颈。结果是,我无意中同时打印了一个非常大的rect。因为尺寸太大,花了很长时间。谢谢你。