Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

python - 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
question from:https://stackoverflow.com/questions/65899193/pygame-rotozoom-rescaling-the-image-out-of-bounds

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

    # [...]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...