Pygame scaling a sprite

2019-05-11 02:40发布

问题:

How would one scale a sprite's image to be bigger or smaller? I can change the rect and all, but not the image.

Code:(though i'm not sure why you would need it for this)

class test(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load("testImage.png").convert()
        self.image.set_colorkey((255,255,255))
        self.rect = self.image.get_rect()

回答1:

Have you tried

pygame.transform.scale()

Official documentation notes that you should use this syntax:

scale(Surface, (width, height), DestSurface = None) -> Surface


回答2:

Read about pygame's transform module: http://www.pygame.org/docs/ref/transform.html

class test(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("testImage.png").convert()
        self.image.set_colorkey((255,255,255))
        # return a width and height of an image
        self.size = self.image.get_size()
        # create a 2x bigger image than self.image
        self.bigger_img = pygame.transform.scale(self.image, (int(self.size[0]*2), int(self.size[1]*2)))
        # draw bigger image to screen at x=100 y=100 position
        self.screen.blit(self.bigger_img, [100,100])


标签: python pygame