Pygame,玩家击败高分会导致游戏不显示死亡屏幕



我正在尝试从pygame中的应用程序商店复制以进行练习,我已经设置了一个系统,如果您击败了当前的高分,它会将其写入文件中,以便可以保存以供以后使用,以便用户可以尝试击败他们的分数。

def player_death(player_score):  # What happens when the player dies
end_font = pygame.font.Font('freesansbold.ttf', 50)
print("You have died")  # For debugging
score_file = open('data/score.txt', 'a')  # Don't think this is related
score_file.write(str(player_score))       # Don't think this is related
score_file.close()                        # Don't think this is related 
b_score_file = open('data/score_best.txt', 'r')
score_read = int(b_score_file.read())
b_score_file.close()
if player_score >= score_read:
b_score_file = open('data/score_best.txt', 'w')
b_score_file.write(str(player_score))
b_score_file.close()
print("New best score!")  # For debugging
print(score_read)  # For debugging
return best_score2

每次玩家与 Rect 碰撞时,它都会增加分数,在主游戏循环中,然后作为"player_score"传递以用于死亡屏幕,函数中有一个循环和其他东西,但我想我已经把它缩小到这部分。如果需要更多代码,我可以添加。问题是,如果score_best.txt中保存的高分高于您刚刚获得的分数,它完全可以正常工作。在代码的主循环中,我有它,所以如果你与屏幕顶部或底部的矩形发生碰撞,它将运行上面的函数。如果需要更多信息,请随时询问,因为我的想法已经用完了。

当玩家点击运行此功能的任一部分时,它仍然会检测到它并运行"您已死亡"打印语句,但随后通过此功能绘制的实际屏幕(未显示在粘贴的代码中(不显示,只是使玩家撞到地面并反弹。

我删除了该行

return best_score2

这解决了问题。

最新更新