简体   繁体   English

如何让我的精灵图像背景看起来透明?

[英]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.

相关问题 如何在没有PIL的情况下使图像的背景显得透明? - How can I make the background of an image appear transparent without PIL? 如何使Sprite出现在窗口中? - How do I make my Sprite appear in my window? 如何使pygame精灵的背景透明 - How to make the background of a pygame sprite transparent 当我的 Sprite 左右移动时,如何让我的背景滚动? Pygame - How do I make my background scroll as my Sprite moves left and right? Pygame 如何使粘贴在另一个图像顶部的图像背景在枕头中透明? - How do I make the background of an image, that is pasted on top of another image, transparent in pillow? 如何使按钮显示在背景后面,或使按钮背景透明? - How to make buttons appear behind a background, or make the buttons background transparent? 如何让精灵一一出现 - How to I make sprite appear one by one 将具有透明背景的图像与普通图像(非透明背景)混合后如何调整 alpha 值? - How do I adjust the alpha value after blending an image with a transparent background to a normal image (NOT transparent background)? 如何使我的精灵正确动画并让我的精灵向左移动? - How do i make my sprite animate properly and make my sprite move left? 如何使 tkinter 中的画布背景透明? - How do I make it so the background of a canvas in tkinter is transparent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM