473,659 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 9 '06 #1
4 5592
Ia**********@gm ail.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.c om>
Nov 9 '06 #2
Ia**********@gm ail.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.Fon t(font_fn, size)
images = []
for i, c in enumerate("colo red 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.Fon t(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
Diez B. Roggisch schrieb:
Ia**********@gm ail.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.Fon t(font_fn, size)
images = []
for i, c in enumerate("colo red 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3084
by: Andreas Prilop | last post by:
Internet Explorer displays <tt>, <code> etc. in a smaller font size: http://www.unics.uni-hannover.de/nhtcapri/temp/monospace.html I don't like the idea that monospaced text is displayed in a different size. Should we state "font-size: 100%" with monospaced text? -- Meanwhile at the Google Ranch ... "I can't read this bloody site; it's all Falsh and JavaScrap." "Forget it and move on! Still 2 718 281 828 pages to crawl."
6
13090
by: Matt Adams | last post by:
I want define for a couple of words inside a longer text with different font specifications. I declared it like: ....text before.... <FONT class=aaa> these text should get other attributes </FONT> text after However when I let this html source validate the FONT tag is marked as not compatible with XHTML. So how do I specify otherwise new font attributes to only a few words?
3
1346
by: David Phillip Oster | last post by:
In Javascript, how do I detect that the user has used the browser's menu command to make the font smaller? I just need some kind of event or callback, so I can adapt to the new environment. -- David Phillip Oster
2
1779
by: David Trimboli | last post by:
If you look at http://www.trimboli.name/klingon/, you'll see that the headers on the page are displayed in small-caps. Usually, this is desired. However, the header "may'vaD ghuHmoH QoQ"* is surrounded by <span lang="tlh"> ("tlh" for "tlhIngan Hol"), and I have put a declaration of "font-variant: normal" in the main stylesheet. I can't figure out why the Klingon title is still in small-caps. I want all Klingon phrases to appear in normal...
0
1204
by: Chad Sanford | last post by:
I have some server side code that instantiates a font object that is then used for generating a chart. It has worked great on many machines, but I have run into one where it is throwing a System.ArgumentException. From the documentation, that would seem to indicate the font is not present, but since I am using generic sans serif I have my doubts about that. The offending line of code is:
1
1881
by: Achim Domma (SyynX Solutions GmbH) | last post by:
Hi, is there a possibility to set the font size to a percent value of the "top level" font size? For example: The font size of the body might be 100% and I have a div which should have a font size of 80% of this value, independent of other tags around this div:
0
1267
by: Altemir | last post by:
I have a datagrid in an .aspx page that contains some columns using the "Font=Bold" inline style. The problem is that some users' browsers aren't displaying the bold font while others are. We all seem to be using Internet Explorer 6.0. Is the "Font-Bold" style dependent on some sort of browser setting? If so, where do I find this setting?
9
4519
by: MadingS | last post by:
Is there an HTML (or CSS) way to say "use the user's INPUT font, whatever that might be."? Most browsers have the ability for the user to pick his or her own preference for what font to use inside of <INPUTtags. My question is, is there a way in HTML or StyleSheet language to say something like this: <h4>Here are some values - some are hardcoded, and others are input feilds:</h4> <FONT face="same as user's input font preference"...
4
10818
by: david.karr | last post by:
I'm a CSS newbie, but I was browsing through the css files in the YUI library, and I noticed the following line: body {font:13px/1.231 arial,helvetica,clean,sans-serif;*font- size:small;*font:x-small;} I understand everything but the "/1.231". Is that something like a multiplier on the EM size? I can't find an example of that in the CSS spec.
3
4419
moishy
by: moishy | last post by:
I have over 3000 fonts that I've backed up. (I had to reformat my drive.) Now I try to copy/paste the fonts into the fonts directory. For most of them I get the message: The <font name> font is already installed. To install a new version, first remove the old version. It would be silly for me to sit there for a year and a quarter clicking "OK" "OK" "OK" "OK" ...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.