pygame return 转换表面 set_alpha 不工作

pygame return converted surface with set_alpha not working

我有这个功能:

def imgload(dir, file, convert=2, colorkey=None):
    img = pygame.image.load(os.path.join(dir, file))
    if convert == 1:
        img.convert()
    elif convert == 2:
        img.convert_alpha()
    
    if colorkey is not None:
        img.set_colorkey(colorkey)
    return img

那我有

logo = imgload("Big_Images", "logo.png", 1, (0, 0, 0))

和一个class(别看缩进,我没写对,在实际代码中是正确的)

class Intro(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = logo
    self.rect = self.image.get_rect()
    self.rect.centerx = round(WINDOW_WIDTH / 2)
    self.rect.centery = round(WINDOW_HEIGHT / 2)
    self.transparency = 255

def update(self):
    global game_state
    if self.transparency > 0:
        self.transparency -= 1
        self.image.set_alpha(self.transparency)
    else:
        game_state = "home"

游戏循环中的绘制函数(while): if game_state == "intro": WIN.fill(GRAY) all_intro_sprites.draw(WIN) all_intro_sprites.update()

奇怪的是当我这样做时代码工作正常 logo= pygame.image.load(os.path.join("Big_Images", "logo.png").convert()

convert()convert_alpha() 不会就地转换图像。这些函数创建并 return 具有所需像素格式的表面的新副本。您必须将 return 值分配给 img。例如:

img = img.convert_alpha() 

修正函数imgload:

def imgload(dir, file, convert=2, colorkey=None):
    img = pygame.image.load(os.path.join(dir, file))
    
    if convert == 1:
        img = img.convert()
    elif convert == 2:
        img = img.convert_alpha()
    
    if colorkey is not None:
        img.set_colorkey(colorkey)
    return img