【发布时间】:2020-06-22 00:16:38
【问题描述】:
我正在使用pygame 创建一个 GUI 库。我想支持透明表面以及透明rects。当我使用self.original_image.set_alpha(100) 时,透明矩形起作用。我正在转换 pygame 表面,以便它可以接受带有surface.convert() 的 alpha,但是当我这样做时,图像最终会出现黑色背景和一些失真。 surface.convert_alpha() 好像没有任何效果。
class UIComponent(pg.sprite.Sprite):
def __init__(self, width=10, height=10, visible=True):
pg.sprite.Sprite.__init__(self)
self.width_percent, self.height_percent = 0, 0
self.x_offset, self.y_offset = 0, 0
self.alignments = []
self.original_image = pg.Surface((width, height))
self.image = self.original_image
self.original_image.set_alpha(100)
self.rect = self.image.get_rect()
def set_image(self, surface):
self.original_image = surface.convert()
self.image = self.original_image
编辑:
请求与UIComponent 交互的代码以创建剑。
self.slot_icon = UILib.UIComponent()
self.slot_icon.set_image(win.icon)
self.slot_icon.add_alignment(self.slot_icon.relative_width)
self.slot_icon.add_alignment(self.slot_icon.height_ratio_constraint)
self.slot_icon.width_percent = 0.03
self.slot_icon.add_alignment(self.slot_icon.center_x)
self.slot_icon.add_alignment(self.slot_icon.center_y)
在win:
def __init__(self):
self.s_width = 0
self.s_height = 0
self.screen = pg.display.set_mode()
self.icon = pg.image.load("icon.png")
【问题讨论】:
-
能否请附上剑(?)位图与
UIComponent一起使用的代码。 -
所以当您调用
set_image()时,它只是将self.image替换为提供的位图。因此没有维护任何set_alpha()操作。我是否正确理解这一点?这是你想要的吗?你的意思是也调整参数的 alpha 吗?还是别的什么?