我要显示带有阿尔法的图像具有指定的透明度,但不知道如何做到这一点。
为了详细说明我是如何解决这个问题的,下面的简介是稍微修改过的代码块这个答案,但是如果你运行它,你会看到“image”失去了它的原生 alpha,而“image2”的 alpha 永远不会改变!恶心。
#!/usr/bin/env python
import pygame, sys
pygame.init()
window = pygame.display.set_mode((200, 200))
background = pygame.Surface((window.get_size()))
background.fill((255, 255, 255))
image = image2 = pygame.image.load('alpha.png')
image = image.convert()
rect = image.get_rect()
image2 = image2.convert_alpha()
rect2 = image2.get_rect()
rect2.left = rect.width + 1
i = 0
while True:
for event in pygame.event.get():
if event.type == 12:
pygame.quit()
sys.exit()
image.set_alpha(i)
image2.set_alpha(i)
window.fill((255, 255, 255))
window.blit(background, background.get_rect())
window.blit(image, rect)
window.blit(image2, rect2)
if i == 255:
i = 0
else:
i += 1
pygame.display.update()
pygame.time.Clock().tick(60)
那么...是怎么做到的呢?
复制您想要显示的图像(不更改原始图像)并使用以下命令:
self.image = self.original_image.copy()
# this works on images with per pixel alpha too
alpha = 128
self.image.fill((255, 255, 255, alpha), None, pygame.BLEND_RGBA_MULT)
抱歉,没有提供完整的示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)