1

我在 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)
4

2 回答 2

0

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

window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])

如果您不使用 alpha,建议您在创建表面时使用surface.convert() 。

如果您使用 alpha,则可以使用颜色键,因为它们比 alpha 表面要快得多

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

http://www.psyco.sourceforge.net/

于 2013-04-02T16:59:53.657 回答
0

除了 alexpinho98 所说的,您不必使用带有 alpha 表面的颜色键。您可以改用 surface.convert_alpha() 。

使用以下代码可以节省一些时间,并且不必重复输入 .convert_alpha() :

def loadify(imgname):
    return pygame.image.load(imagename).convert_alpha()
于 2016-01-05T19:36:30.927 回答