[Tutor] font/text in pygame

Max Noel maxnoel_fr at yahoo.fr
Wed Apr 27 02:59:39 CEST 2005


On Apr 27, 2005, at 01:36, D. Hartley wrote:

> I haven't programmed in C (python is my first language!), but I *have*
> done something like this before, only with the print command:
>
> def displaybalance():
>     for score, name in mylist:
>         slip = 30 - len(name)
>         slip_amt = slip*" "
>         print "%s%s%s" % (name,slip_amt,score)
>
> (I did this with the print command to make sure it would produce what
> I wanted, three strings for the three sample scores I put in this
> dummy list).

	Yup, you did that. And actually, what happened was that string 
formatting was first used to create a string, and then this string was 
sent to the print command, which displayed it. Your last line is 
equivalent to:

foo = "%s%s%s" % (name,slip_amt,score)
print foo

	The only "magic" thing you can only do with print is the "print a, b, 
c" syntax.

> So I went and found some sample code to just create a graphics window
> I could try out my new stuff in, and I inserted it as follows. The
> only problem is, it's printing all three lines right on top of each
> other! The newline command wont work (i'm not printing), and I tried
> to do something like text.pos = text.pos + 20 for the y, but no matter
> where I put it in the loop, it was in the wrong place (cant reference
> it before I create "text", can't make a function out of the whole text
> part outside of the main loop....etc).
>
> I know at this point it's just an indentation
> problem/where-it-goes-in-the-loop problem. But I've tried and retried
> it a hundred times and I cant seem to get it to work.  But if I can
> make it work on this sample, maybe I can insert it into my program.
> (Then the only thing I'll have to do is get user input for the new
> name, which I'll worry about second, if I can get this first part to
> work).
>
> I know it's a lot to ask, but can you find the error here, how to make
> these lines print one under the other and not on top of each other?
>
> Ideally I want it to print several lines:
>
> (1)   High Scores
> (2)   Denise                         23   (etc., one for each %s item)
>
> Here's the sample render-font-onto-pygame-window code:
>

[SNIP some code]

> 	# Display some text
> 	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))
>             textpos = text.get_rect()
>             textpos.centerx = background.get_rect().centerx
>             textpos.centery = 270
			^^^^^^^^^^^^^^^^^^^^^

	This, I believe, is the line you have to modify. All your text goes to 
the same place because you affect it the same position. (text.centery, 
text.centerx) is where the "text" object is rendered on the screen. The 
renderer doesn't know (nor should even care) about the text you may 
have rendered earlier.
	Therefore, you need to render each line of text at a unique position. 
Ideally, the second line should be below the first, etc.

	So that means textpos.centery must be slightly different for each line 
of text you create. Let's say, 270 for the first one, 300 for the 
second one, etc.

	You could start by modifying your for loop so that it reads like this:

for score, name, posY in mylist, range(270, 540, 30):

	The rest shouldn't be hard to figure out. ;)

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"



More information about the Tutor mailing list