I'm making a function that takes a string a breaks it up into lines and returns a surface with each line rendered below the previous one.
For example:
Line1\nLine 2
Renders into:
Line1
Line2
Anyway, my problem is that I cannot return a properly transparent surface to the calling function. I've tried using a color-key, but it does't work with anti-aliased text. Is there a way I can do this? Thanks for your answers.
My code: (Currently leaves an ugly purple shadow around text)
def render_message(messages):
surfaces = []
height = 0
max_width = 0
for message in messages.split('\n'):
surf = FONT.render(message, True, (0, 0, 0))
surfaces.append(surf)
height += surf.get_height() + 5
if surf.get_width() > max_width:
max_width = surf.get_width()
result = pygame.Surface((max_width, height))
result.fill((255, 0, 255))
result.set_colorkey((255, 0, 255))
top = 0
for surface in surfaces:
result.blit(surface, (max_width/2-surface.get_width()/2, top))
top += surface.get_height() + 5
return result
Got it working. Main not I found is: no AA = automatic transparent. With AA you need to set the color key too
Here's working example, and it toggles BG to ensure it is transparent.
edit: Improved code, uses alpha channel instead of set_colorkey.
Posted a second answer since this is more complex, people might want to see both.
1
,2
decrease/increase font sizespace
toggle BGI left text wrapping out. Edit
parse_text()
to create a list of text you want.