Moving Text across a background
Author |
Message |
Notthebbq
![](http://compsci.ca/v3/uploads/user_avatars/11181326514ee8f2aca9c82.jpg)
|
Posted: 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 (^_^) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
apython1992
![](http://compsci.ca/v3/uploads/user_avatars/17001640684d87a16bb8fbb.jpg)
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Notthebbq
![](http://compsci.ca/v3/uploads/user_avatars/11181326514ee8f2aca9c82.jpg)
|
Posted: Wed Apr 13, 2011 11:00 am Post subject: Re: Moving Text across a background |
|
|
could you give example code? |
|
|
|
|
![](images/spacer.gif) |
apython1992
![](http://compsci.ca/v3/uploads/user_avatars/17001640684d87a16bb8fbb.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Notthebbq
![](http://compsci.ca/v3/uploads/user_avatars/11181326514ee8f2aca9c82.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
apython1992
![](http://compsci.ca/v3/uploads/user_avatars/17001640684d87a16bb8fbb.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|
|