Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

python - Set the width and height of a pygame surface

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Changing the width and height of the self.rect attribute does effect the size of the pygame.Surface. Even if a rectangle is passed to the dest argument of pygame.Surface.blit, only the position of the rectangle is considered when the surface is _blit__ onto the target surface.
You have to scale the original image to the desired size by either pygame.transform.scale or pygame.transform.smoothscale.
Keep the original image (self.original_image) and create a scaled image when the size the width or height is changed:

self.image = pygame.transform.smoothscale(self.original_image, self.rect.size)

Class UIComponent:

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.original_image = pg.Surface((width, height))
        self.image = self.original_image
        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
        self.image = pygame.transform.smoothscale(self.original_image, self.rect.size)

    def relative_height(self, win):
        self.rect.height = 25
        self.image = pygame.transform.smoothscale(self.original_image, self.rect.size)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...