使用精灵表的Pygame:alpha问题

2024-04-25 15:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个精灵表动画我的球员,动画工作得很好,但没有阿尔法当我blit在屏幕上的动画相应的图像。你知道吗

Class Animation:
    def __init__(self, path, img_size):
        self.images = pyagme.image.load(path).convert_alpha()
        self.cur_img = 0
        ....

    def get_image(self):
        img=pygame.Surface((self.img_width,self.img_height)).convert_alpha()
        rect = pygame.Rect((self.cur_img * self.img_width, 0),(self.img_width, self.img_height))
        img.blit(self.images, (0, 0), rect)
        return img

我正在使用get_image函数来绘制播放器: 每次更新时:self.image = self.cur_anim.get_image()self是Player类。你知道吗

在我的函数中drawself.screen.blit(self.player.image, self.player.rect)


Tags: pathrectimageselfalphaconvertimgget
1条回答
网友
1楼 · 发布于 2024-04-25 15:25:28

新的Surface从来都不是透明的,所以你必须用RGBA颜色填充它,这个颜色有A=0使它透明。你知道吗

img = pygame.Surface((self.img_width,self.img_height)).convert_alpha()

img.fill( (0,0,0,0) )

但是有pygame.Surface.subsurface可以创建子映像(并且不使用新内存)

def get_image(self):
    rect = pygame.Rect((self.cur_img * self.img_width, 0),(self.img_width, self.img_height))

    return self.images.subsurface(rect)

顺便说一句:您可以在__init__中创建所有子曲面,以后只能使用

def get_image(self):
    return self.all_subsurfaces[self.cur_img]

相关问题 更多 >