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

时间:2017-11-14 23:51:24

标签: python image pygame transparency blit

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 个答案:

答案 0 :(得分:2)

只需设置两个对象的alpha值,您的代码就可以正常工作。

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

请注意,set_alpha不适用于每像素alpha表面(加载了.convert_alpha()的图像或您通过pygame.SRCALPHA的表面),但仍然可以使用blit透明表面到这些每像素的alpha表面上。

相关问题