[英]How do I make my sprite image background appear transparent?
I am trying to make sprites appear as images in my pygame code.我试图让精灵在我的 pygame 代码中显示为图像。 Here is one of the sprites:这是其中一个精灵:
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.Surface((20, 10))
self.surf = pygame.image.load("Rocket 1.png").convert()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(5, 10)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 1
self.kill()
It works, but the image displays with a black background.它有效,但图像显示为黑色背景。 Here is the image:这是图像: As you can see, the image has no background, but when I run my code, it still displays it as having a black background.如您所见,图像没有背景,但是当我运行我的代码时,它仍然显示为黑色背景。
I used the 1st link given by @mkrieger1 and found out that .convert_alpha
works.我使用了@mkrieger1 提供的第一个链接,发现.convert_alpha
有效。 Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.