Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scale multiple images with a for loop?

I’m trying to use a for-loop to iterate through a list of self classes. I want to give each one the same scale.

def __init__(self):    
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("Migue/m_normal.png")

    self.quieto = pygame.image.load("Migue/m_normal.png")
    self.andando = pygame.image.load("Migue/m_andando_normal.png")
    self.image = pygame.transform.scale(self.image, sizenorm)
 
    states = [self.quieto, self.andando]

    for i in states:
        i = pygame.transform.scale(i, sizenorm)

This wont work, but I can achieve the result using this:

self.quieto = pygame.transform.scale(self.quieto, sizenorm) 
self.andando = pygame.transform.scale(self.andando, sizenorm)

The problem is that I have to make a lot of more states, and using that for loop would be shorter. However, it doesn’t work like the lower example does. I don’t know what’s wrong with the loop.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create a list of the scaled objects and assign the elements of the list to the original attributes:

states = [self.quieto, self.andando]
states = [pygame.transform.scale(i, sizenorm) for i in states]
(self.quieto, self.andando) = states

This can even be written in a single line

(self.quieto, self.andando) = [pygame.transform.scale(i, sizenorm) for i in [self.quieto, self.andando]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading