我在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如果可以,请使用颜色键,因为它们比alpha曲面快。
为了加快计算速度,我建议使用Psyco。
http://www.psyco.sourceforge.net/
除了alexpinho98所说的,您不必对alpha曲面使用颜色键。您可以改用surface.convert_alpha()。
使用以下代码可以节省一些时间,并避免重复键入.convert_alpha():
def loadify(imgname):
return pygame.image.load(imagename).convert_alpha()