pygame 精灵的透明度

时间:2021-04-11 17:00:48

标签: python pygame pygame-surface

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

1 个答案:

答案 0 :(得分:1)

如果您要将另一个 pygame.Surface 上的每像素 alpha 格式的图像复制到另一个表面上,则需要确保目标 Surface 也具有每像素 alpha 格式。如果目标无法保存 Alpha 通道,则透明度将丢失。设置 SRCALPHA 标志以使用包含每像素 alpha 的图像格式创建表面。

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

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

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