[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] font/text in pygame
On Wednesday 27 Apr 2005 1:45 am, D. Hartley wrote:
> I cannot thank you enough. That was exactly what I needed. I now
> have a working sample (just a box with a displayed high score list,
> not in my program yet but at least it's working!) - and it was in
> language I could actually understand. My only question remains
> regarding the "x" - it sets where the string begins (the left side of
> it), and since I have a series of strings of different lengths, I'd
> like to center them - how can i do this within the framework you
> provided?
It's not too hard; think about it. The rendered text are just a bunch of
surfaces, and surfaces are rectangles with a width and a height. The
horizontal centre of any rectangle is half its width.
So work out half the width of the surface, and take it away from the x
coordinate. Then the x-coordinate will represent the position of the *middle*
of the text, not the upper left corner.
x, y = 100, 0
for (name, score) in highscore_list:
text = font.render("%s - %d" % (name, score), 1, (255, 255, 255, 0))
w = text.get_width() / 2.0 # w = centre of text
screen.blit(text, (x - w, y)) # (x, y) becomes (x - w, y)
y += text.get_height()
If you want all the high-score's in the middle of the page, you need to work
out the centre of the screen. Again, just divide the width by 2:
x, y = screen.get_width() / 2.0, 0
--
James Reeves
http://www.monkeyengines.co.uk/