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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…