制作高分系统时遇到麻烦[关闭]

问题描述 投票:0回答:2
我正在制作我的第一款游戏,我想为其添加一个高分系统。我已经在这里问过这个问题,因为我只是不知道从哪里开始。我得到了很大的帮助,并以为我已经完成了此工作,但是经过测试,我遇到了一个问题。在解释问题之前,我将首先列出我的代码的有用部分:

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:对不起,如果某处英语不好,那不是我的母语...

python pygame
2个回答
2
投票
您正在比较字符串:

content = str(content) if content < str(score):

您可以这样做,但是它的行为方式与您比较数字时不同。例如,如果您在控制台中键入以下内容:

'13' < '7'

它将返回True。比较字符串时,将比较第一个字符的ascii值。当然,这不是您通常想要的。

因此,如果在对分数进行逻辑运算时使用int变量,它应该可以工作。仅在打印分数时将其转换为str。


1
投票
关于您更大的问题。您正在比较两个字符串。即使9 <10,也应为“ 9”>“ 10”。

这可能有用

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()

© www.soinside.com 2019 - 2024. All rights reserved.