2013-06-19 123 views
0

我创建一个排行榜功能为我的比赛,但我不能让它的工作诠释转换工作不

这里是我的方法就可以了:

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使用int(highscore)然后和第10行我做print highscore + self.score为一个测试但是我抛出说,我不能加海峡和一个int即使我转换highscore为int和我转换self.score所以出于某种原因,转换一个没有工作

回答

7

int()返回一个错误整数,但是你放弃了那个结果。重新分配它:

highscore = int(highscore) 

功能并变化就地变量。如果self.score也是一个字符串,则需要对int(self.score)执行相同的操作,或者只删除该行。

+0

以及即时通讯愚蠢我认为'int'改变了变量感谢这么多martijn! – Serial

+1

值得注意的是,如果这些值是整数,那么后面一行'print highscore + self.score'会做出非常不同的事情,如果它们仍然是字符串的话,它们会变得非常不同。我建议使用字符串格式来适当地布置它们,而不是串联。 – Blckknght