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

Re: [pygame] font/text in pygame



Denise,

Try the following loop:

--

x = 0
y = 0
hiscore_font = pygame.font.Sysfont("arial", 12)

for score, name in high_scores:
  slip = 30 - len(name)
  slip_amt = slip*" "
  prefix = 5*" "
  hiscore_str = "%s%s%s%s" % (prefix,name,slip_amt,score)
  hiscore_image = hiscore_font.render(hiscore_str, 0, (255,255,255))
  screen.blit( hiscore_image, (x, y) )
  y += hiscore_image.get_height()

--

There's probably a few errors in there, but it should get you
started... I hope that helps.

- Atul

On 4/26/05, D. Hartley <denise.hartley@xxxxxxxxx> wrote:
> Bob,
> 
> Thank you for the clarification. I am very new to pygame, and am
> trying to adjust.  I realize that I am not "printing" text to this
> window in the same way I would print text to a console window, I was
> just including the code to show what I had done before (at the moment,
> the high score list is displayed in the console window).
> 
> What I am trying to find out is how (if possible) to get my text high
> score list (a list of tuples with names and scores) to something I can
> render on the game screen. I have images that are constant, which are
> rendered as pre-made gifs (or whatever), such as a fixed "Game Over"
> message, and so on.  But I cannot figure out how to get a dynamically
> changing object like my high score list to be rendered onto the game
> screen.
> 
> ~Denise
> 
> On 4/26/05, Bob the Hamster <Bob-pygame-users@xxxxxxxxxxxxxxxxxxx> wrote:
> > On Tue, Apr 26, 2005 at 03:57:51PM -0700, D. Hartley wrote:
> > > So with pygames, if I want to display text on the graphics window, I
> > > can set the font and size and so on, and set what I want my text to
> > > be.  But if I wanted python to grab that text from a list, say, how
> > > would I do that? I can do it in the print command, as follows:
> > >
> > >                         for score, name in high_scores:
> > >                             slip = 30 - len(name)
> > >                             slip_amt = slip*" "
> > >                             prefix = 5*" "
> > >                             print prefix,name,slip_amt,score
> > >
> > > (slip et al. is spacing stuff - not the prettiest code to do it, but it works)
> > >
> > > But in any case, font/text will only take strings - i cant pass in a
> > > list, or an index to an item in a list (which is, in this case, a
> > > tuple), and since the items in the list will be changed and updated
> > > obviously i cant just type in the items as strings myself.
> > >
> > > any ideas? does this question even make sense the way it's worded?
> > > It's a last-ditch effort to get my high score list onto the graphics
> > > window before i have to abandon it :)
> > >
> > > Thanks!
> > > ~Denise
> >
> > You are going about this wrong. What you need to understand about
> > displaying text in pygame is that it works nothing at all like
> > displaying text in a console. In a console, you print text, and it goes
> > to the cursor position, but within a pygame window, you render text to a
> > surface, which you then draw on the screen. You aren't really working
> > with text, you are working with a picture of text.
> >
> > ---
> > Bob the Hamster
> >
>