Python / Pygame - How to blit different transparencies onto an invisible surface

I was wondering if it is possible to blit two images with let's say 120 and 200 alpha on to a surface with 0 alpha.

For example:

Here are all my variables

game_display = pygame.display.set_mode((1280, 720))
transparent_display = pygame.Surface((1280, 720), pygame.SRCALPHA)
transparent_display.set_alpha(0)
object1 = pygame.Surface((100, 100))
object1.fill((255, 0, 0))
object2 = pygame.Surface((150, 50))
object2.fill((0, 0, 255))

Now I want to make a single surface (image) with all the components added into it (I want them all on one surface so I don't have to load each surface each game loop cycle)

transparent_display.blit(object1, (0, 0))
transparent_display.blit(object2, (50, 50))
game_display.blit(transparent_display, (0, 0)

I apologize as I do not have very much knowledge on the subject of alphas with surfaces in pygame. Also, I know to update the screen and everything, that is not the issue.

(comment below if something isn't clear enough and I will elaborate)

1 answer

  • answered 2017-11-15 01:46 skrx

    Just set the alpha values of the two objects and your code should work.

    object1.set_alpha(120)
    object2.set_alpha(200)
    # Then blit them onto `transparent_display`.
    

    Note that set_alpha doesn't work with per-pixel alpha surfaces (images loaded with .convert_alpha() or surfaces to which you passed pygame.SRCALPHA), but you can still blit transparent surfaces onto these per-pixel alpha surfaces.