[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] font/text in pygame - code in, but display at wrong time?



All,

Well I'm still working on the centering part, but it is IN, and it is
working.  Well, it was working in the sample program.  But it's not
displaying on the graphics window at the right time, as follows below.
 I think this might be the simple case of something being out of
order, but I just can't get it right.  Could someone take a quick look
and see if they see my (probably glaring) misplacement?

So 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 (or clicking outside the window and
clicking back, see below), 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?

P.S.  I should also add that the "print" commands embedded within the
add_score function ARE displaying at the appropriate time (i.e.,
before I have to hit y/n).  Of course they're in the text/console
window, but it does tell me that it's executing that add_score
function at the right point in time.

So why is it waiting until after the y/n keystroke to display the text
I'm rendering on the game window?

It will also display if I click to some other window and then click
back to the game window.  I was having this problem with a different
program in jython last night, and it ended up being a case of things
in the wrong order (and I was successful in fixing it). But for the
life of me I can't figure out what's in the wrong order that's causing
it in *this* program.

Thanks for any help you could offer, and for all the suggestions
provided so far!


---------- Forwarded message ----------
From: Sami Hangaslammi <sami.hangaslammi@xxxxxxxxx>
Date: Apr 27, 2005 3:53 AM
Subject: Re: [pygame] font/text in pygame
To: pygame-users@xxxxxxxx


On 4/27/05, Daniel Bickett <dbickett@xxxxxxxxx> wrote:
> I wrote the following function recently. It places the image perfectly
> in the center of the screen, using the rect of the image and the
> screen.
>
> def centerImgOnScreen( image_rect , screen_rect ):
>     x = ( screen_rect.right / 2 ) - ( image_rect.right / 2 )
>     y = ( screen_rect.bottom / 2 ) - ( image_rect.bottom / 2 )
>     return x,y

Let the library do the work for you. ;)

image_rect.center = screen_rect.center

--
Sami Hangaslammi