Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Moving Text across a background
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Notthebbq




PostPosted: Tue Apr 12, 2011 11:59 am   Post subject: Moving Text across a background

Hello there, our team has another question. We have managed to import text onto a background using a snippet code from a site, but how do we move the text around? This is a copy pasta of what we have found. Thank you. With this code, we are only able to put the text in the center, and to be honest we haven't modified the code snippet much, due to the risks involved.

Here is the code snippet:

if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pummel the Chimp, and win $$$", 1, (10,10,10))
textpos = text.get_rect(centerx=background.get_width()/2) #This is the variable that we want to change.
background.blit(text, textpos)

Also, if you guys could, it would really, really helpful if you could explain the code snippet with the code to us. Eg: what each syntax does. You don't have to, it's optional.

Thanks again (^_^)
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Tue Apr 12, 2011 12:04 pm   Post subject: RE:Moving Text across a background

If you're afraid of messing up something you already have, just save what you know works as a backup file. Alternatively you could go with a source control system. In Pygame, it's very easy to move rectangle objects. If you can represent a block of text as a rectangle, do you think you can move the rectangle containing the text?
Notthebbq




PostPosted: Wed Apr 13, 2011 11:00 am   Post subject: Re: Moving Text across a background

could you give example code?
apython1992




PostPosted: Wed Apr 13, 2011 11:08 am   Post subject: RE:Moving Text across a background

If you're using the font module, you can render it into a surface object, which is an image that can be moved. I won't give you sample code, but this will be helpful: http://pygame.org/docs/ref/font.html
Notthebbq




PostPosted: Tue Apr 19, 2011 12:34 pm   Post subject: Re: Moving Text across a background

my group member does not understand your previous statement: represent teh text block as a rectangle and would appreciate it if you could provide actual code please and thanks
apython1992




PostPosted: Tue Apr 19, 2011 1:07 pm   Post subject: RE:Moving Text across a background

Use my last statement, about rendering it onto a surface object (in Pygame, a surface is an image-like object that can be moved) and then getting its rectangle.

Quote:
Font.render
draw text on a new Surface
Font.render(text, antialias, color, background=None): return Surface


Quote:
Surface.get_rect
get the rectangular area of the Surface
Surface.get_rect(**kwargs): return Rect
Returns a new rectangle covering the entire surface. This rectangle will always start at 0, 0 with a width. and height the same size as the image.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: