我正在为我的游戏创建一个高分功能,但我无法让它工作
这是我的方法:
def game_over(self):
# Game over Screen
keys = pygame.key.get_pressed()
self.gameover = pygame.image.load('resources/screen/game_over.png')
screen.blit(self.gameover,(0,0))
high_filer = open('highscores.txt', 'r')
highscore = high_filer.read()
high_filer.close()
int(highscore)
int(self.score)
print highscore + self.score
if self.score > highscore:
high_filew = open('highscores.txt', 'w')
high_filew.write(str(self.score))
high_filew.close()
if (keys[K_RETURN]):
self.state = 1
它的作用是从.txt文件中读取最新的高分,并检查玩家得分是否高于是否将新的高分写入文件
我使用highscore
将字符串从int(highscore)
转换为int然后在第10行我print highscore + self.score
作为测试但我抛出的错误表示我不能添加一个str和一个int,即使我将highscore
转换为int并且我转换了self.score,因此由于某种原因,其中一个转换不起作用
答案 0 :(得分:7)
int()
返回一个整数,但您丢弃该结果。重新分配:
highscore = int(highscore)
该函数不就地更改变量。如果self.score
也是字符串,则您需要对int(self.score)
执行相同的操作,或者只删除该行。