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