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
358 views
in Technique[技术] by (71.8m points)

python - How do I scale a PyGame image (Surface) with respect to its center?

I am fairly new to pygame and am working on my first game. (So sorry if I'm asking a stupid question) I am trying to get the title of the game to slowly increase and decrease in size like a sort of breathing effect in order to make the home screen more visually appealing.

Here's what I've got to import the image:

name = self.dir_path + "pixeltitle.png"
self.pixeltitle = pg.image.load(name)
self.pixeltitlerect = self.pixeltitle.get_rect()
self.pixeltitlerect.center = (250,120)
self.screen.blit(self.pixeltitle,self.pixeltitlerect)

I've got a while loop in which I'm increasing the size of the rect, however it gets moved over to the right and downwards. Is there any way to increase the size and have the centre of the rect stay in the same place? Also is there a way of making the increase/decrease in size more smooth? Here's the rest of the code:

clicked = False
        grow = 0
        mode = 'grow'
        while not clicked:
            if grow>40:
                mode = 'shrink'
            if grow<1:
                mode = 'grow'
                self.pixeltitle = pg.transform.scale(self.pixeltitle,(400,400))


            if mode == 'grow':
                grow+=1
            else:
                grow-=1

            xsize=400+int(grow)
            ysize=400+int(grow)

            self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize))
            self.pixeltitlerect.center = (250,120)
            self.screen.blit(self.pixeltitle,self.pixeltitlerect)
            pg.display.flip()
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You missed to update the size of self.pixeltitlerect after the Surface has been scaled:

self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize))

# size of surface has been changed get the new rectangle
self.pixeltitlerect = self.pixeltitle.get_rect()

self.pixeltitlerect.center = (250,120)
self.screen.blit(self.pixeltitle,self.pixeltitlerect)

Or even shorter (see pygame.Surface.get_rect()):

self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize))
self.pixeltitlerect = self.pixeltitle.get_rect(center = (250,120))
self.screen.blit(self.pixeltitle,self.pixeltitlerect)

Do not scale the original Surface. If the original Surface is scaled it will get distorted. Keep the original image self.pixeltitleorig and scale the original image:

self.pixeltitle = pygame.transform.scale(self.pixeltitleorig,(xsize,ysize))

See also Transform scale and zoom surface

See the example:

import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

class ScalingSurface:
    def __init__(self):
        name = "pixeltitle.png"
        self.pixeltitleorig = pg.image.load(name)
        self.pixeltitle     = self.pixeltitleorig
        self.pixeltitlesize = self.pixeltitle.get_size()
        self.pixeltitlerect = self.pixeltitle.get_rect()
        self.pixeltitlerect.center = (250,120)
        self.mode = 'grow'
        self.grow = 0

    def update(self):
        if self.grow > 40:
            self.mode = 'shrink'
        if self.grow<1:
            self.mode = 'grow'
        self.grow += 1 if self.mode == 'grow' else -1

        xsize = self.pixeltitlesize[0] + round(self.grow)
        ysize = self.pixeltitlesize[1] + round(self.grow)
        self.pixeltitle = pygame.transform.scale(self.pixeltitleorig,(xsize,ysize))
        self.pixeltitlerect = self.pixeltitle.get_rect(center = (250,120))

    def draw(self, surf):
        surf.blit(self.pixeltitle,self.pixeltitlerect)

img = ScalingSurface()
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)
    img.update()
    img.draw(window)
    pygame.display.flip()

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

...