Snake Game (Pygame) - personal records saving problem

0 Daniel Bogorad · January 13, 2015
Hy there,

So I've been making a snake game (Pygame) recently, it's mostly finished and polished, though I really want to make that one feature.

At the end of the game the player may chose to save his personal score:
By entering a name for his "save", his record - name and score, are written to a file.
now I want to show the player, other results from other sessions so he could compare.

Everything worked great there and just how i want it, though one thing really teases me:
I want to sort this dictionary (actually two zipped lists), so it would show in the order of the highest - lowest scores.
it sorts incorrectly, and i cant find out how to fix this annoying detail.



def save(player_score):

    recordNames = []
    recordScores = []
    recordsDict = {}

    # register player
    player_name = input("What is your name: ")
    
    highScoreFile = open("High Scores.txt", "a")
    highScoreFile.write("\n" + player_name + ": " + str(player_score))
    highScoreFile.close()

    # show high scores file
    hsfReader = open("High Scores.txt", "r")
    temp = hsfReader.read()
    tempSortedList = temp.split()
    
# if the item in the splitted list is on an even position - it's a value (score)
# else - its a key (the name)
    for e in range(len(tempSortedList)):
        if e%2 == 0:
            recordScores.append(tempSortedList[e]) 
        else:
            recordNames.append(tempSortedList[e])

    recordsDict = dict(zip(recordScores, recordNames))
    recordsDict = sorted(zip(recordsDict.values(), recordsDict.keys()))
    recordsDict = reversed(recordsDict)
    
    for key, value in recordsDict:
        print(key, value)

    hsfReader.close()


it would mess up and give something like :
879 gamwe:
6 irea:
4 dac:
3 check:
23 asd:
223 io9:
1 foo:
1 asgasf:
0 nose:
0 nob:

If someone could take a look and relate i'd be glad :)
thanks

Post a Reply

Replies

Oldest  Newest  Rating
0 Chris Nelson · January 18, 2015
looks to me like, it is sorting your dict using the first number only. Assuming that the example you posted is a real example?
  • 1

Python

106,940 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator