I am creating my own GUI library with pygame
. I have a UIComponent
class that creates the UI elements. The UI element itself is created through self.image
with width
and height
. The problem with this is that changing the width and height doesn't seem to work. I'm using self.rect.x
to modify its coordinates but when I tried using self.rect.width
it didn't have an effect.
ui_elements = pg.sprite.Group()
class UIComponent(pg.sprite.Sprite):
def __init__(self, width, height):
pg.sprite.Sprite.__init__(self)
self.width_percent, height_percent = 0, 0
self.alignments = []
self.image = pg.Surface((width, height))
self.rect = self.image.get_rect()
ui_elements.add(self)
def set_colour(self, colour_value):
self.image.fill(colour_value)
def update(self, win):
for i in self.alignments:
i(win)
def center_x(self, win):
self.rect.x = (win.s_width/2)-self.image.get_width()/2
def center_y(self, win):
self.rect.y = (win.s_height/2)-self.image.get_height()/2
def relative_width(self, win):
self.rect.width = 25
def relative_height(self, win):
self.rect.height = 25
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…