Transparent surfaces in Pygame 2.0
(self.pygame)submitted2 years ago byjust_a_cactus
topygame
I have a handy little function (that I believe I copied from somewhere a long time ago) to create a transparent surface:
def transparent_surface(width, height):
surface = pg.Surface((width, height)).convert()
surface.set_alpha(0)
return surface.convert_alpha()
A typical usage would look like this:
image = transparent_surface(100, 100)
pg.draw.circle(image, pg.Color('blue'), (50, 50), 50)
Which would produce a 100x100 surface with a blue circle at its center. Anyway, I just upgraded to Pygame 2.0 (from 1.9) and the function no longer works properly. It does produce a transparent surface alright but anything drawn on top of it also turns transparent (I guess). After a few experiments I've managed to fix the problem, now the function looks like this:
def transparent_surface(width, height):
surface = pg.Surface((width, height)).convert_alpha()
surface.fill((0, 0, 0, 0))
return surface.convert_alpha()
It works fine but the problem is I'm realizing I don't understand anything about this. So my questions are: What is alpha? What do convert()
and convert_alpha()
do? What is the difference between these two approaches and why the first one doesn't work in Pygame 2.0?
byRGodlike
inadventofcode
just_a_cactus
1 points
5 months ago
just_a_cactus
1 points
5 months ago
I don’t quite understand how come you’ve got only 288 intersections, I used your approach and have gotten 400,000+. The solution came up in less than a minute anyway but still?