簡體   English   中英

pygame 精靈的透明度

[英]Transparency in pygame sprites

我不明白為什么下面的代碼會渲染沒有透明度的精靈(精靈的 rest 用黑色填充)。 我幾乎是編程的初學者,所以這可能很明顯,但這里沒有其他類似問題的答案對我有幫助。 該文件具有透明度,我在加載文件時使用了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上的每像素 alpha 格式的圖像復制到另一個表面上,則需要確保目標Surface也具有每像素 alpha 格式。 如果目標無法保存 Alpha 通道,則透明度將丟失。 設置SRCALPHA標志以創建具有包含每像素 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM