pythonpygamepygame-surfaceblit

Pygame rotozoom rescaling the image out of bounds


I want to display an image of a star that grows bigger by 1% every second. I use the pygame.transform.rotozoom function to do so. When using rotozoom, the star indeed grows bigger, but its drawing rectangle doesn't grow enough, making the star blitting out of its rectangle bounds. The star then appears cut.

The original star.png

The result with the program

I tried searching for a solution to expand the rectangle but I did not succeed. I also tried pygame.transform.scale but it didn't work at all, the star didn't grow or was distorded depending on how much its dimensions were incremented.

Does anyone has any solution for the drawing rectangle or an alternative ?

Here's my program :

import pygame
import time
pygame.init()

star = pygame.image.load('assets/star.png')
bg = pygame.image.load('assets/background.jpg')

_w = pygame.display.set_mode((480,480))
cpt = 0
Done = False

while not Done :
    pygame.time.delay(1000)
    pygame.display.flip()
    _w.blit(bg, (0,0))
    _w.blit(star, (0,0))
    star = pygame.transform.rotozoom(star, 0, 1.01) 
    cpt += 1
    if cpt == 20 :
        Done = True

Solution

  • You need to scale the original image instead of gradually scaling the scaled image:

    scale = 1
    while not Done :
        # [...]
        
        scaled_star = pygame.transform.rotozoom(star, 0, scale)
        scale += 0.01 
        _w.blit(scaled_star, (0, 0))
    
        # [...]