2

我正在使用 pygame 中的自定义 gui 组件,并且在制作滑块组件时,我为滑块提供了一个Draw(parentSurface)函数,该函数将滑块绘制到其存储位置的父表面上。

现在这可行,但滑块存储两个图像:

  • 一个幻灯片图像,它是由瓦片动态制成的,以匹配滑块的给定长度,以给定长度的宽度和 3 像素的高度进行测量;和
  • “选择”图像,它只是一个小的 8x12 指示器,用于显示滑块的位置。

问题是当我Draw(parentSurface)这样做时:

def Draw(self, parsurf):
    '''draw the element on parsurf at the given location'''
    w1 = self.slider_img.get_width()
    h1 = self.slider_img.get_height()
    w2 = self.select_img.get_width()
    h2 = self.select_img.get_height()
    self.image = pygame.Surface((w1, h2 + h1))
    self.image = ColorMod.Transparency(self.image, 0)
    self.image.blit(self.slider_img, (0, self.image.get_height()-h1))
    x = self.value / self.conversionratio
    self.image.blit(self.select_img, (x- w2/2, 0))
    if self.debug_on:
        print "attempting to blit to the given surface"
    parsurf.blit(self.image, self.loc)

但是,它在self.image. 我希望它是半透明的,这样背景就可以通过self.image. 我应该放弃拥有并将and直接self.image绘制到父表面吗?self.slider_imgself.select_img

4

2 回答 2

0

If the Surface object that the self.slider_img name refers to is created dynamically, when you first create the Surface instance you should call Surface.convert_alpha to get a Surface with an appropriate pixel format for per-pixel alpha values. Then you should be able to set the alpha on a pixel-by-pixel basis as needed.

http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha

于 2014-02-11T04:32:46.673 回答
0

You have to make the image transparent by calling self.image.set_alpha((0,0,0)) Also in self.image = pyame.Surface you need to change it to self.image = pygame.Surface((width, height), pygame.SRCALPHA) I'd also recommend getting rid of self.image = ColorMod.Transparency...

于 2014-02-11T00:41:22.093 回答