我将生命条移到屏幕上,似乎是将屏幕上的图像移到屏幕上导致了问题,我删除了for循环,每当我让玩家跳跃时,似乎性能非常非常糟糕,当我删除整个功能时,玩家移动得很快,跳得很快,而不是很慢。是什么导致了这个问题?/(请忘记屏幕[0]。(这与问题无关)
代码:
def lives(lives_image1, lives_image2, x,y,lives_amount,lives_amount2, draw_lives):
if len(screens) >= 1:
if draw_lives:
for i in range(lives_amount2):
screens[0].blit(pygame.image.load(lives_image2),(x + pygame.image.load(lives_image2).get_width() * i,y))
for i in range(lives_amount):
screens[0].blit(pygame.image.load(lives_image1), (x + pygame.image.load(lives_image1).get_width() * i,y))
if lives_amount <= 0:
lives_amount = 0
return lives_amount
pass
您的应用程序很慢,因为您每帧都从文件加载图像。加载图像应用程序循环,但blit
他们每一帧:
def load_lives(lives_image)
return pygame.image.load(lives_image);
def lives(lives1_surf, lives2_surf, x, y, lives_amount, lives_amount2, draw_lives):
if len(screens) >= 1:
if draw_lives:
for i in range(lives_amount2):
screens[0].blit(lives2_surf,(x + lives2_surf.get_width() * i, y))
for i in range(lives_amount):
screens[0].blit(lives1_surf, (x + lives1_surf.get_width() * i, y))
if lives_amount <= 0:
lives_amount = 0
return lives_amount
lives1_surf = load_lives(lives_image1);
lives2_surf = load_lives(lives_image2);
while run:
# [...]
lives(lives1_surf, lives2_surf, ......)
# [...]
注意,pygame.image.load
是一个非常昂贵的操作。这个函数需要从磁盘加载图像文件,最后需要解码图像。