[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Transparent surfaces



Does anyone know how to dynamically create a transparent surface? But before 
anyone mentions colorkeys, I'd like to sketch out what I'd like to do:

First some preamble:

font = pygame.font.Font(None, 36)
hello = font.render("Hello", 1, (255, 255, 255))
world = font.render("World", 1, (255, 255, 255))

Now, this is the way I'd normally render the text to the screen:

screen.blit(hello, (0, 0))
screen.blit(world, (0, world.get_height()))

But I'd like to do this:

width = max((hello.get_width(), world.get_width())
height = hello.get_height() + world.get_height()

text = Surface((width, height))
text.blit(hello, (0, 0))
text.blit(world, (0, world.get_height()))
screen.blit(text, (0, 0)

And ideally I'd like to get exactly the same result as I would if I rendered 
the text straight to the screen. The problem is that surfaces created with 
Surface have a solid-colour background. Colorkeys don't work either, because 
the text is anti-aliased.

So in short, is there an easy way of creating a transparent surface of 
arbitrary size in pygame? The only current solution I can think of is to use 
pygame.image.from_string() in some manner, or to load a transparent PNG and 
resize it.

Neither solution seems ideal, and surely there must be a simple way of 
creating a transparent surface in pygame.

Anyone have any idea?

-- 
James Reeves
http://www.monkeyengines.co.uk/