Pygame : image.convert_alpha() 不起作用

Pygame : image.convert_alpha() won't work

所以,我正在使用 Pygame、Pytmx 和 PyScroll 制作游戏原型(我在 youtube 上关注“Graven”Turorial)

这是我的问题:我有一个 spritesheet(96x38 像素),我只想得到它的一张图像..

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
    super(Player, self).__init__()

    self.spritesheet = pygame.image.load('assets/imgs/player0.png')

    self.image = pygame.Surface([12, 19]).convert_alpha()
    self.image.blit(self.spritesheet.convert_alpha(), (0, 0))

但是当我启动它时,它可以工作,但是有一个黑色背景,而不是透明背景。它有问题吗?我尝试在“self.spritesheet”和“self.image”上使用“set_colorkey(color)”,我尝试使用“convert_alpha” ()”,并尝试使 pygame.Surface 背景透明..

(代码): 玩家 Class:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Player, self).__init__()

        self.spritesheet = pygame.image.load('assets/imgs/player0.png')

        self.image = pygame.Surface([12, 19]).convert_alpha()
        self.image.blit(self.spritesheet.convert_alpha(), (0, 0))

        self.position = [x, y]

        self.rect = self.image.get_rect()
        self.speed = 3

    def update(self, *args, **kwargs) -> None:
        keys = pygame.key.get_pressed()

        if any((keys[pygame.K_UP], keys[pygame.K_w])):
            self.move_up()
        if any((keys[pygame.K_DOWN], keys[pygame.K_s])):
            self.move_down()
        if any((keys[pygame.K_LEFT], keys[pygame.K_a])):
            self.move_left()
        if any((keys[pygame.K_RIGHT], keys[pygame.K_d])):
            self.move_right()

        self.rect.x, self.rect.y = self.position[0], self.position[1]

    def move_right(self): self.position[0] += self.speed

    def move_left(self): self.position[0] -= self.speed

    def move_up(self): self.position[1] -= self.speed

    def move_down(self): self.position[1] += self.speed

游戏Class:

class Game:
def __init__(self, title, state):
    self.MainMenu = MenusClass.MainMenu(800, 600)
    self.Window = WindowClass.Window((800, 600), title, [self.MainMenu,])
    self.Window.menu2alpha()

    self.state = state
    self.colors = StaticsClass.Color

    self.player = PlayerClass.Player(0, 0)
    self.mapmanager = MapClass.MapManager(self.Window.scr, self.player)


# Game Events
def _events(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if self.state == "__mainmenu__":
            self.MainMenu.is_activated = True

def update(self):
    self.Window._fillscreen(self.colors.BLUE0)
    self._events()

    curr_group = self.mapmanager.get_group()
    curr_group.center(self.player.position)
    curr_group.update()
    curr_group.draw(self.Window.scr)

    self.Window._blitmenus()

我认为问题只出在播放器 class 的构造函数和游戏的更新函数 Class... 希望有人能帮助我!

说明

self.image = pygame.Surface([12, 19]).convert_alpha()

不生成透明图像。它生成一个 Surface 每像素 alpha 格式,完全不透明和黑色。

使用SRCALPHA标志生成完全透明的Surface:

self.image = pygame.Surface([12, 19], pygame.SRCALPHA)

另见 How to make a surface with a transparent background in pygame


Player:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Player, self).__init__()

        self.spritesheet = pygame.image.load('assets/imgs/player0.png')

        self.image = pygame.Surface([12, 19], pygame.SRCALPHA)
        self.image.blit(self.spritesheet.convert_alpha(), (0, 0))