简体   繁体   English

Python / Pygame-如何将不同的透明胶片放到不可见的表面上

[英]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. 我想知道是否有可能将两个图像用120和200 alpha映射到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. 我很抱歉,因为我对pygame中带有表面的alpha知识了解不多。 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) (下面的注释,如果有些不清楚的地方,我会详细说明)

Just set the alpha values of the two objects and your code should work. 只需设置两个对象的alpha值,您的代码就可以工作。

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. 请注意, set_alpha不适用于按像素的Alpha曲面(加载有.convert_alpha()或传递pygame.SRCALPHA曲面的pygame.SRCALPHA ),但是您仍可以将透明曲面pygame.SRCALPHA到这些按像素的alpha曲面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM