[Tutor] font/text in pygame

D. Hartley denise.hartley at gmail.com
Wed Apr 27 03:27:47 CEST 2005


Max (et al.):

I tried to do it that way ("score, name, posY" etc) and it told me
"too many values to unpack".  But I did get it to work the following
way:


# Display some text
        x, y = 230, 270
	for score, name in mylist:
            slip = 30 - len(name)
            slip_amt = slip*" "
            font = pygame.font.Font(None, 25)
            text = font.render("%s%s%s" % (name,slip_amt,score), 1,
(10, 10, 10, 0))
            background.blit(text, (x,y))
            y += text.get_height()

I'd rather have it centered on the x (x = 230 is the beginning (left
end) of the string, and my strings are different lengths, so I'd
rather have it centered), but I was just thrilled to get it working in
the first place.   So if anyone knows something quick I can add to
that code to make it centerx rather than beginning x, that'd be great.
But it does at least work for now.

But then I tried to import this (working!) code into my game, and even
tho it executes the function within which it's contained BEFORE
waiting for the "y/n another game" response, it's not displaying the
score then (I just managed to glimpse it in the second after I hit n,
the score flashed on right before the window closed. So I know it's in
there and works! Just not at the right time.)

The code goes like this:

          if lives == 0:
### trying addscore
                gameover.play()
                showGameOver(screen, background_image)                
                pygame.display.flip()                

                def add_score():
                    high_scores = pickle.load(file("scores.pik"))
                    score = total_enemy_hits
                    if score > high_scores[-1][0]:
                        print "Ta da! You got", total_enemy_hits,
"Ranch Delivery Devices!"
                        name = read_string("You made the high score
list! What's your name? ")
                        user_score = (score,name)
                        high_scores.append(user_score)
                        high_scores.sort(reverse=True)
                        del high_scores[-1]
                        pickle.dump(high_scores, file("scores.pik", "w"))
                        x, y = 230, 270
                        for score, name in high_scores:
                            slip = 30 - len(name)
                            slip_amt = slip*" "
                            font = pygame.font.Font(None, 25)
                            text = font.render("%s%s%s" %
(name,slip_amt,score), 1, (255, 255, 255, 0))
                            screen.blit(text, (x,y))
                            y += text.get_height()
                    else:
                        print "Sorry, you only got", total_enemy_hits,
"Ranch Delivery Devices."
                        print "You didn't quite make the high score list!"
                        x, y = 230, 270
                        for score, name in high_scores:
                            slip = 30 - len(name)
                            slip_amt = slip*" "
                            font = pygame.font.Font(None, 25)
                            text = font.render("%s%s%s" %
(name,slip_amt,score), 1, (255, 255, 255, 0))
                            screen.blit(text, (x,y))
                            y += text.get_height()
                        print "Better luck next time!"

#                pdb.set_trace()
                add_score()

                answer = ""
                while not answer in ("y","n"):
                   for event in pygame.event.get():
                      if event.type == KEYDOWN:
                         if event.key == K_n:
                            answer = "n"
                         elif event.key == K_y:
                            answer = "y"
                if answer == "n":
                    running = 0
                else:
                    return 1

    
            #refresh the display
            pygame.event.pump()
            pygame.display.flip()

    #well, nice playing with you...
    screen = pygame.display.set_mode((640, 480))
    return 0


See, it seems to me that I define and execute the add_score function
BEFORE the "do you want to play again" bit. But it doesnt display
until AFTER you hit that key, at which point the game goes onto the
next thing (i.e., either refreshes and starts the game over, or closes
the window).  Like I said I know it's in there, because I saw it flash
on after I hit the n key, right before the window closes.  I tried
moving the showgameover/flip, commenting out the different
flip/pump/etc commands (these are a little out of my league,
experience-wise), and tried rearranging things as best I could. (And
yes, I know defining the add_score function right there is probably
not elegant, but if I dont do it that way I get other errors).  Am I
missing something glaringly obvious? If I define and use the add_score
function before the keystroke commands, why is it displaying after?

Thanks...

~Denise


More information about the Tutor mailing list