By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

how to create a multicolor "font-string" in pygame??

P: n/a
Hi! I need to manipulate multicolor strings, i.e. strings with a color
associated with each letter.
Any suggestions?

Nov 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ia**********@gmail.com writes:
Hi! I need to manipulate multicolor strings, i.e. strings with a color
associated with each letter.
Any suggestions?
If you're on Unix / Linux the curses module might help.

--
Jorge Godoy <jg****@gmail.com>
Nov 9 '06 #2

P: n/a
Ia**********@gmail.com schrieb:
Hi! I need to manipulate multicolor strings, i.e. strings with a color
associated with each letter.
Any suggestions?
There is no support for multi-color strings as such in pygame. If you
use a fixed-width font, things are easy. Just create the strings one
after another using spaces as prefix. E.g.

back = (0,0,0)
font = pygame.font.Font(font_fn, size)
images = []
for i, c in enumerate("colored string"):
s = " " * i + c
color = color_for_index(i)
s_image = font.render(s, True, color, back)
images.append(s_image)
Then blit the images onto each other.

If you don't use fixed widht fonts, things get more complicated. Then
you should calculate the size of the prefix to a certain characer, and
store that together with the image:

text = "colored string"
back = (0,0,0)
font = pygame.font.Font(font_fn, size)
images = []
for i, c in enumerate(text):
s = " " * i + c
color = color_for_index(i)
prefix_offest = font.size(text[:i])
s_image = font.render(s, True, color, back)
images.append((prefix_offset, s_image))

Then you have to combine the images according to the prefix offset.

Diez
Nov 9 '06 #3

P: n/a
Diez B. Roggisch schrieb:
Ia**********@gmail.com schrieb:
>Hi! I need to manipulate multicolor strings, i.e. strings with a color
associated with each letter.
Any suggestions?

There is no support for multi-color strings as such in pygame. If you
use a fixed-width font, things are easy. Just create the strings one
after another using spaces as prefix. E.g.

back = (0,0,0)
font = pygame.font.Font(font_fn, size)
images = []
for i, c in enumerate("colored string"):
s = " " * i + c
color = color_for_index(i)
s_image = font.render(s, True, color, back)
images.append(s_image)
Don't drink and code... *sigh*

Of course you can just create an equally dimensioned image for each
character and each color (cached or not, however you like it), and just
blit these one after another, offsetting them with the character width.

Diez
Nov 9 '06 #4

P: n/a
Of course you can just create an equally dimensioned image for each
character and each color (cached or not, however you like it), and just
blit these one after another, offsetting them with the character width.

Diez
thanks.

Nov 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.