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

Re: [pygame] Transparent surfaces



James Reeves wrote:

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?


Hi,

i ran into the same problem just a few days ago. I got it to work:
like

image = pygame.Surface([640,480], SRCALPHA, 32)
#image.fill([0,0,0,0])
#image.convert_alpha()

i don't know if either fill or convert are necessary, though, you'll have to try.

I think it's necessary to give the '32', eg. the color depth, or it gets it's color depth information from the screen surface, which, per definition, isn't transparent, because there can't be something behind it.

Or so i guess.

Have fun, i hope it works

   Daniel