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

Re: [pygame] font/text in pygame



On Wednesday 27 Apr 2005 12:26 am, D. Hartley wrote:
> 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.

It's a bit tricky to wrap your head around it at first, so I'll give you some 
examples:

# create a font object with the default typeface and 36 pixels (or is it
# points) tall.
font = pygame.font.Font(None, 36)   

# create a new surface with white, anti-aliased text on it:
text = font.render("Hello world", 1, (255, 255, 255, 0))

# the 'text' object is just a surface with text on. You could get exactly the
# same result by creating an image with text on in your favourite graphics
# software, and then using pygame.image.load. The difference is, this text is
# created on the fly

# blit the text surface to the screen
screen.blit(text, (0, 0))


So to make a high score list, you'd need something like:

x, y = 0, 0
for (name, score) in highscore_list:
	text = font.render("%s - %d" % (name, score), 1, (255, 255, 255, 0))
        screen.blit(text, (x, y))
	y += text.get_height()


Notice that I increase the y by the height of the text each time, putting each 
name and score on a new line.

-- 
James Reeves
http://www.monkeyengines.co.uk/