pygame 精灵中的透明度

Transparency in pygame sprites

我不明白为什么下面的代码渲染的 sprite 没有透明度(其余的 sprite 填充黑色)。我几乎是编程的初学者,所以它可能非常明显,但这里没有其他类似问题的答案对我有帮助。该文件具有透明度,我在加载文件时使用了convert_alpha()

import pygame


class Piece(pygame.sprite.Sprite):
    def __init__(self, kind, posx, posy, sheet, color):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image for the piece and fill it with the image
        # TO DO: cut the right type of piece from a sheet, preferably before constructing the object
        self.image = pygame.Surface([128, 128])
        self.image.blit(sheet, [0, 0])

        self.kind = kind  # piece type, designated with an uppercase letter, eg. Q for queen
        self.color = color  # W or B for white or black

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rect = self.image.get_rect()
        self.rect.x = posx
        self.rect.y = posy


class App:
    def __init__(self):
        self._running = True
        self._display_surf = None
        self.size = self.weight, self.height = 1024, 768
        self.sprites = pygame.sprite.Group()  # all sprites in the app to iterate on
        self.spritesheet = 0  # this will be loaded later
        self.bgcolor = [200, 200, 200]

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._running = True
        self.spritesheet = pygame.image.load("resources/w_queen_png_shadow_128px.png", "r").convert_alpha()
        self.sprites.add(Piece("Q", 64, 64, self.spritesheet, "W"))

    def on_event(self, event):
        if event.type == pygame.QUIT:
            self._running = False

    def on_loop(self):
        pass

    def on_render(self):
        self._display_surf.fill(self.bgcolor)
        self.sprites.draw(self._display_surf)
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        if self.on_init() is False:
            self._running = False

        while self._running:
            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()


if __name__ == "__main__":
    game = App()
    game.on_execute()

如果您要在另一个 pygame.Surface onto another surface, you need to ensure that the target Surface has a per pixel alpha format, too. If the target cannot save the alpha channel, the transparency will be lost. Set the SRCALPHA 标志上复制具有每像素 alpha 格式的图像,以创建具有包含每像素 alpha 的图像格式的表面。

self.image = pygame.Surface([128, 128])

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

class Piece:

class Piece(pygame.sprite.Sprite):
    def __init__(self, kind, posx, posy, sheet, color):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image for the piece and fill it with the image
        # TO DO: cut the right type of piece from a sheet, preferably before constructing the object
        self.image = pygame.Surface([128, 128], pygame.SRCALPHA)
        self.image.blit(sheet, [0, 0])

        self.kind = kind  # piece type, designated with an uppercase letter, eg. Q for queen
        self.color = color  # W or B for white or black

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rect = self.image.get_rect()
        self.rect.x = posx
        self.rect.y = posy