我正在为我的游戏创建一个高分功能,但我不能让它工作
下面是我的方法:
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文件中读取最新的高分,并检查球员的分数是否更高,如果是,则将新的高分写入文件
我使用int(highscore)将字符串从highscore转换为int,然后在第10行执行print highscore + self.score作为测试,但我抛出了一个错误,指出我无法添加字符串和int,即使我将highscore转换为int,并且我也转换了self.score,因此由于某种原因,其中一个转换不起作用
发布于 2013-06-20 06:20:10
int()返回一个整数,但您会丢弃该结果。重新分配:
highscore = int(highscore)该函数不会就地更改变量。如果self.score也是一个字符串,那么您需要对int(self.score)执行相同的操作,或者只是删除该行。
https://stackoverflow.com/questions/17202363
复制相似问题