0

One of the requirements for my PyGame is to implement a basic high score function (showing the name and score). My database for the highscore works perfectly fine, as it stores a "sub-list" into the entire database list: such as [['Bob',10],['Jim',17]]. I also imported itemgetter to help me sort the list based on the indexes within the "sub-lists" (sorted(database, key=itemgetter(1))). This makes the user with the highest score always appear last in this list. This is based on the following code here:

def highscore_data(dodged,database):    
    database.append(high_score("",dodged)) #function that asks the players name
    print "*******"
    return sorted(database, key=itemgetter(1))   

I always want to display the last "sub-list" within the entire database list (since it always includes the highest score) on my game. However, whenever a new high score is entered, it just displays that instead. This is the code that displays the high score:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    gameDisplay.blit(background,(0,0))

    font = pygame.font.Font('freesansbold.ttf',87)
    TextSurf, TextRect = text_objects('HIGH SCORE',font, white)
    TextRect.center = ((display_width/2),(display_height/2-150))
    gameDisplay.blit(TextSurf, TextRect)
    font = pygame.font.SysFont(None, 60)
    text = font.render("TEAM: "+str(database[-1][0]), True, white)
    gameDisplay.blit(text,(display_width/2-95,display_height/2))
    font = pygame.font.SysFont(None, 60)
    text = font.render("SCORE: "+str(database[-1][1]), True, white)
    gameDisplay.blit(text,(display_width/2-95,display_height/2+50))

database[-1][0] and database[-1][1] attempts to get the last "sub-list" of the entire list ([-1]), as well as the player's name [0] and the score [1]. An example would be this: [['Bob',10],['Jim',17]. Jim has the highest score (since it's sorted at the end of the big list), so that's the one I want to display on my screen. Nonetheless, it always shows the newest high score implemented, which would be "Bob" in this case. From my knowledge, I would think that doing a list[-1] will retrieve the final index in a list?

Any suggestions/help will be greatly appreciated.

  • better show how you use `highscore_data()`. Do you assign sorted data to variable ? `database = highscore_data(dodged, database)` ? Or maybe use `sort()` instead of `sorted()` - `databas.sort(key=itemgetter(1))` - and then you don't have to assign result to variable - it sorts `"in-place"`. – furas Jan 21 '17 at 09:34
  • BTW: you can use `reverse=True` in `sort/sorted` to have the highest on the first place. https://wiki.python.org/moin/HowTo/Sorting – furas Jan 21 '17 at 09:41

0 Answers0