Charles Noon
Charles Noon

Reputation: 601

Pygame scaling a sprite

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

Upvotes: 11

Views: 37395

Answers (4)

zucc
zucc

Reputation: 11

Just created an account to share my solution here, since I was having this problem myself. So for a bit of context I'm running a Player() class for the sprites, movement, and collisions of the player. In the init method, I'm using

self.image = self.sprites_idle[self.current_sprite] # to set image in variable self.rect = self.image.get_rect(topleft = pos) # to set rect of the image

current_sprite is a variable that cycles the sprite index in to create the motion, the rest is pretty straight-forward. At first I tried adding

self.rect = self.image.get_rect()

Every time I crouch or un-crouch to update the rectangle, after updating the image of course. with

self.image = self.sprites_idle[self.current_sprite]

However I found that updating the rect() doesn't work because it keeps resetting the x,y as well as the w,h. (therefore teleporting the player to the starting position every iteration of the loop, which is 60 times per second, meaning we can't move out of the starting position. If this is your problem, then do:

The solution:

self.image = self.sprites_idle[self.current_sprite] self.rect.height = self.image.get_height()

By only updating the height it now runs perfectly as intended.

Upvotes: 1

Histreike
Histreike

Reputation: 31

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

Upvotes: 3

Aidan
Aidan

Reputation: 767

Have you tried

pygame.transform.scale()

Official documentation notes that you should use this syntax:

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

Upvotes: 10

macfij
macfij

Reputation: 3209

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

Upvotes: 5

Related Questions