score = 0
def update_obstacle_positions(obstacle_list, score):
for idx, obstacle_location in enumerate(obstacle_list):
if obstacle_location[0] >= 0 and obstacle_location[0] < width:
obstacle_location[0] -= speed
else:
obstacle_list.pop(idx)
score += 1
return score
def game_over():
while game_over:
screen.fill(black)
text = "Game Over, Press R to restart"
label = myFont.render(text, 1, white)
screen.blit(label, (350, 450))
end_score = "Score:" + str(score)
label_2 = myFont.render(end_score, 1, white)
screen.blit(label_2, (350, 250))
file = open("highscore.txt", "r")
content = file.read()
content = str(content)
if content < str(score):
file = open("highscore.txt", "w")
file.write(str(score))
hs = "You got a new highscore!"
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 350))
pygame.display.update()
else:
hs = "Highscore: " + content
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 350))
pygame.display.update()
file.close()
这是我能最全面地使用它的方法,但是他有2个问题(1个相当大)首先,当您超过最高分时,您不会收到“您获得了新的最高分!”的消息,它只是说“分数:(您的分数)”,然后说“分数:(与分数相同) )“
这并没有真正打扰我,但是大问题确实困扰了我。
最大的问题是以下内容:该代码记住了高分,并在您每次打败它时对其进行更新,直到您获得10分以上的分数。这样就可以了,直到您获得2位数的分数为止。这显然是不正确的。我也不知道如何解决这个问题。系统运行(几乎)完美无缺,直到出现两位数,这对我来说毫无意义。
我希望解决方案不要太复杂,有人可以帮助我。我还是编程新手。因此,请尽可能简化说明simple
PS:对不起,如果某处英语不好,那不是我的母语...
content = str(content)
if content < str(score):
您可以这样做,但是它的行为方式与您比较数字时不同。例如,如果您在控制台中键入以下内容:
'13' < '7'
它将返回True。比较字符串时,将比较第一个字符的ascii值。当然,这不是您通常想要的。因此,如果在对分数进行逻辑运算时使用int变量,它应该可以工作。仅在打印分数时将其转换为str。
这可能有用
def game_over():
while game_over:
screen.fill(black)
text = "Game Over, Press R to restart"
label = myFont.render(text, 1, white)
screen.blit(label, (350, 450))
end_score = "Score:" + str(score)
label_2 = myFont.render(end_score, 1, white)
screen.blit(label_2, (350, 250))
file = open("highscore.txt", "r")
content = file.read()
content = int(content)
if content < int(score):
file = open("highscore.txt", "w")
file.write(str(score))
hs = "You got a new highscore!"
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 350))
pygame.display.update()
else:
hs = "Highscore: " + content
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 350))
pygame.display.update()
file.close()