[英]Pygame Being Really Slow
我在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 。
除了alexpinho98所說的,您不必在alpha曲面上使用色鍵。 您可以改用surface.convert_alpha()。
使用以下代碼可以節省一些時間,並使您不必重復鍵入.convert_alpha():
def loadify(imgname):
return pygame.image.load(imagename).convert_alpha()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.