Fwd: [Tutor] font/text in pygame

Max Noel maxnoel_fr at yahoo.fr
Wed Apr 27 02:47:42 CEST 2005


[Forwarding to the list. Please use "Reply to all", not "Reply", if you 
want your reply to a message to go to the list]

Begin forwarded message:

> From: "D. Hartley" <denise.hartley at gmail.com>
> Date: April 27, 2005 01:36:26 BST
> To: Max Noel <maxnoel_fr at yahoo.fr>
> Subject: Re: [Tutor] font/text in pygame
> Reply-To: "D. Hartley" <denise.hartley at gmail.com>
>
> Max,
>
> Thank you for your helpful answer.  I am particularly interested in 
> this part:
>
>> string formatting. You ever programmed in C? It's basically >the same
>> thing as printf, only better. Example:
>> "Hi, my name is %s. I am %s years old. I come from %s." >% ("Max", 21,
>> "France")
>> returns "Hi, my name is Max. I am 21 years old. I come >from France."
>
> 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).
>
> 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:
>
> import pygame
> from pygame.locals import *
>
> def main():
> 	# Initialise screen
> 	pygame.init()
> 	screen = pygame.display.set_mode((640, 480))
> 	pygame.display.set_caption('Basic Pygame program')
>
> 	# Fill background
> 	background = pygame.Surface(screen.get_size())
> 	background = background.convert()
> 	background.fill((250, 250, 250))
>
> 	# 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
>             background.blit(text, textpos)
>
> 	
> 	# Blit everything to the screen
> 	screen.blit(background, (0, 0))
> 	pygame.display.flip()
>
> 	# Event loop
> 	while 1:
> 		for event in pygame.event.get():
> 			if event.type == QUIT:
> 				return
>
> 		screen.blit(background, (0, 0))
> 		pygame.display.flip()
> 		
>
> if __name__ == '__main__': main()
>
> Thanks, Max!!
>
> ~Denise
>
> On 4/26/05, Max Noel <maxnoel_fr at yahoo.fr> wrote:
>>
>> On Apr 26, 2005, at 23:57, D. Hartley wrote:
>>
>>> 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 :)
>>
>>        The idea, of course, is to convert the things you want to 
>> display to
>> strings. You may be interested in the following things:
>>
>> - the str() function -- converts anything it's passed into a string.
>> str(1) returns "1". str((2, 3, 4)) returns "(2, 3, 4)".
>> - Basic string concatenation -- use the + operator. Example: "a" + "b"
>> returns "ab".
>> - the string.join method -- joins a list or tuple of strings. Example:
>> 'xx'.join(["foo", "bar", "baz"]) returns "fooxxbarxxbaz".
>> - string formatting. You ever programmed in C? It's basically the same
>> thing as printf, only better. Example:
>> "Hi, my name is %s. I am %s years old. I come from %s." % ("Max", 21,
>> "France")
>> returns "Hi, my name is Max. I am 21 years old. I come from France."
>>
>>        By using some of these (string formatting and str() come to 
>> mind), you
>> should be able to do what you want.
>>
>> Hope that helps,
>> -- 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?"
>>
>>
>
>
-- 
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