'Pygame cannot make image bigger over time
I am trying to make an image get bigger over time for a cool effect.
I've tried using this:
import pygame
from pygame.locals import *
pygame.init()
class Image(pygame.sprite.Sprite):
default_imgresizer = pygame.transform.scale
def __init__(self, win, x, y):
pygame.sprite.Sprite.__init__(self)
self.win = win
self.x = x
self.y = y
self.maxsize = [500, 500] # (not used yet)
self.size = [0, 0]
self.image = pygame.image.load("museum_pic.jpg")
self.image = self.default_imgresizer(self.image, self.size)
self.rect = self.image.get_rect(center=(self.x, self.y))
def increase_size(self):
self.size[0] += 5
self.size[1] += 5
self.image = self.default_imgresizer(self.image, self.size)
self.rect = self.image.get_rect(center=(self.x, self.y))
self.win.blit(self.image, self.size)
This is the event loop:
screen = pygame.display.set_mode((850, 600))
image = Image(screen, 100, 100)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
image.increase_size()
pygame.display.update()
But then, nothing showed up on the screen.
How do I make an image get bigger over time?
Solution 1:[1]
First the image is scaled down to a size of (0, 0). After that you are upscaling the reduced image. Hence the picture is all black. Even if you reduce the image to a size larger than 0 and then upscale the image, it will result in a large loss of quality.
Do not scale the scaled image, but scale the original image:
self.original_image = pygame.image.load("museum_pic.jpg")
self.image = self.default_imgresizer(self.original_image, self.size)
Keep the aspect ratio of the image when you calculate the scale:
self.size[1] += 5
self.size[0] = self.size[1] * self.original_image.get_width() // self.original_image.get_height()
Minimal example:
import pygame
from pygame.locals import *
pygame.init()
class Image(pygame.sprite.Sprite):
default_imgresizer = pygame.transform.scale
def __init__(self, win, x, y):
pygame.sprite.Sprite.__init__(self)
self.win = win
self.x = x
self.y = y
self.maxsize = [500, 500] # (not used yet)
self.size = [0, 0]
#self.original_image = pygame.image.load("museum_pic.jpg")
self.original_image = pygame.image.load(r"C:\source\PyGameExamplesAndAnswers\resource\icon\AirPlaneFront1-256.png")
self.image = self.default_imgresizer(self.original_image, self.size)
self.rect = self.image.get_rect(center=(self.x, self.y))
def increase_size(self):
self.size[1] += 5
self.size[0] = self.size[1] * self.original_image.get_width() // self.original_image.get_height()
self.image = self.default_imgresizer(self.original_image, self.size)
self.rect = self.image.get_rect(center=(self.x, self.y))
self.win.blit(self.image, self.rect)
screen = pygame.display.set_mode((500, 500))
image = Image(screen, *screen.get_rect().center)
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
image.size = [0, 0]
screen.fill(0)
image.increase_size()
pygame.display.update()
pygame.quit()
exit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |