我在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)
答案 0 :(得分:0)
第一部分中唯一不重复的行是:
window.blit(coverso, winrect)
window.blit(texts['complete'][0], texts['complete'][1])
window.blit(stuff[0], stuff[1])
如果您不使用alpha ,建议您在创建曲面时使用 surface.convert()。
如果你使用alpha ,如果使用colorkeys ,因为它们快得多那个alpha表面。
为了加快计算速度,我建议使用 Psyco 。
答案 1 :(得分:0)
添加到alexpinho98所说的内容,您不必使用带有alpha表面的colorkey。您可以使用surface.convert_alpha()代替。
使用以下代码可以节省一些时间,让您不必重复输入.convert_alpha():
def loadify(imgname):
return pygame.image.load(imagename).convert_alpha()