Pygame 运行非常慢
我在用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)
2 个回答
0
接着alexpinho98说的内容,你不一定要用颜色键(colorkeys)来处理带透明度的表面。你可以用surface.convert_alpha()来代替。
使用下面的代码可以帮你节省一些时间,避免你需要反复输入.convert_alpha():
def loadify(imgname):
return pygame.image.load(imagename).convert_alpha()
0
第一部分中唯一不重复的内容是:
window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])
如果你不使用透明度,建议在创建表面时使用surface.convert()。
如果你使用透明度,可以使用颜色键,因为它们比透明表面快得多。
为了加快计算速度,我推荐使用Psyco。