7
votes

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()
3

3 Answers

5
votes

Have you tried

pygame.transform.scale()

Official documentation notes that you should use this syntax:

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

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])
0
votes

If you want to scale an image in pygame you can just use pygame.transform.scale(image, (size, size)) for example:

image1_not_scaled = pygame.image.load('path_of_the_file')
image1 = pygame.transform.scale(image1_not_scaled, (64, 64))

[...]

screen.blit(image1, (0, 0))

For your example you can just write 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.image_scaled = pygame.image.load(self.image, (size, size))
        self.rect = self.image_scaled.get_rect()