python - Saving down onto a file and sorting after highest score! Pyhton games -
update: added said
class highscorehandler(object): highscore_file = "highscores.txt" # define store highscores def get_highscores(self, sort=true): # return sorted default open(self.highscore_file, "a+") f: # open highscore file f.seek(0) # rewind file start scores = [int(x) x in f.read().split()] # read scores ints if sort: # sort if required return sorted(scores) # add reverse=true `sorted()` descending sort return scores def save_highscore(self, highscore): highscores = set(self.get_highscores(false)) # current highscore list if highscore in highscores: # highscore exists return # nothing do... highscores.add(highscore) # add highscore set open(self.highscore_file, "w") f: # open highscore file writing f.write(" ".join(str(score) score in highscores)) # store scores
and put save_highscore
and get_highscores
"code" part
# display final score. drawboard(mainboard) scores = getscoreofboard(mainboard) print('x scored %s points. o scored %s points.' % (scores['x'], scores['o'])) if scores[playertile] > scores[computertile]: print('you beat computer %s points! congratulations!' % (scores[playertile] - scores[computertile])) highscore = scores[playertile] - scores[computertile] get_highscores() save_highscore() elif scores[playertile] < scores[computertile]: print('you lost. computer beat %s points.' % (scores[computertile] - scores[playertile])) else: print('the game tie!') if not playagain(): break
but yet try save highscore file tells mename 'get_highscores' not defined
you can create 2 functions - 1 read scores, update them if needed. simplest way store simple set
(to weed out duplicates) , retrieve when needed (and optionally sort if need show scores in sorted manner:
highscore_file = "highscores.txt" # define store highscores def get_highscores(sort=true): # return sorted default open(highscore_file, "a+") f: # open highscore file f.seek(0) # rewind file start scores = [int(x) x in f.read().split()] # read scores , turn them int if sort: # sort if required return sorted(scores) # add reverse=true `sorted()` descending sort return scores def save_highscore(highscore): highscores = set(get_highscores(false)) # current highscore list if highscore in highscores: # highscore exists return # nothing do... highscores.add(highscore) # add highscore set open(highscore_file, "w") f: # open highscore file writing f.write(" ".join(str(score) score in highscores)) # store space separated scores
then can use 2 functions hearts content manage highscores as:
print(get_highscores()) # [], no highscores yet save_highscore(5) # add 5 print(get_highscores()) # [5] save_highscore(12) # add 12 save_highscore(6) # add 6 save_highscore(8) # add 8 print(get_highscores()) # [5, 6, 8, 12] save_highscore(6) # add 6 again print(get_highscores()) # [5, 6, 8, 12], doesn't save duplicate scores
update - can turn these functions class methods adding self
attribute first method and, if keep highscore_file
class property, referring self.highscore_file
, like:
class highscorehandler(object): highscore_file = "highscores.txt" # define store highscores def get_highscores(self, sort=true): # return sorted default open(self.highscore_file, "a+") f: # open highscore file f.seek(0) # rewind file start scores = [int(x) x in f.read().split()] # read scores ints if sort: # sort if required return sorted(scores) # add reverse=true `sorted()` descending sort return scores def save_highscore(self, highscore): highscores = set(self.get_highscores(false)) # current highscore list if highscore in highscores: # highscore exists return # nothing do... highscores.add(highscore) # add highscore set open(self.highscore_file, "w") f: # open highscore file writing f.write(" ".join(str(score) score in highscores)) # store scores
then can run same way referring instance of it:
handler = highscorehandler() print(handler.get_highscores()) # [], no highscores yet handler.save_highscore(5) # add 5 print(handler.get_highscores()) # [5] handler.save_highscore(12) # add 12 handler.save_highscore(6) # add 6 handler.save_highscore(8) # add 8 print(handler.get_highscores()) # [5, 6, 8, 12] handler.save_highscore(6) # add 6 again print(handler.get_highscores()) # [5, 6, 8, 12], doesn't save duplicate scores
Comments
Post a Comment