Reputation: 386
I was setting up this list where it had images (images are sprites) loaded in. I was trying to create this loop where it would rescale all the images inside this list.
walkLeft = [pygame.image.load('Moving1.png'), pygame.image.load('Moving2.png'), pygame.image.load('Moving3.png'), pygame.image.load('Moving4.png'), pygame.image.load('Moving5.png'), pygame.image.load('Moving6.png'), pygame.image.load('Moving7.png'), pygame.image.load('Moving8.png'), pygame.image.load('Moving9.png')]
walkRight = []
for i in walkLeft:
walkRight.append(pygame.transform.flip(i, True, False))
for x in walkLeft:
pygame.transform.smoothscale(x,(372, 493))
I was expecting the image size to be resized, but instead, it just stayed the same and I was not sure with what to do.
Upvotes: 0
Views: 419
Reputation: 210968
pygame.transform.smoothscale()
can't process lists.
The method scales an image which is passed by parameter and returns a new, scaled image by the return value. The return value hast be reassigned to the list.
You have to do it in a loop:
Either:
for i in range(len(walkLeft)):
walkLeft[i] = pygame.transform.smoothscale(walkLeft[i], (372, 493))
or in one line:
walkLeft = [pygame.transform.smoothscale(x, (372, 493)) for x in walkLeft]
Upvotes: 1
Reputation: 31
pygame.transform.smoothscale returns a new object, so you need to return it to a value:
for x in range(len(walkLeft)):
walkLeft[x] = pygame.transform.smoothscale(x,(372, 493))
Upvotes: 0
Reputation: 24691
I'm not sure about pygame itself, as I've never used it, but I think the issue is that pygame.transform.smoothscale()
isn't transformative - it doesn't change the x
you're feeding it, but rather outputs a changed version without modifying the original. You can see similar behavior in pygame.transform.flip(i, True, False))
- every iteration, it produces a modified copy of i
, which you append to walkRight
, but it isn't changing the i
you started with.
The way you'd probably want to go about this is by iterating by index instead of by element, and actually replacing elements as you go:
for i in range(len(walkLeft)):
walkLeft[i] = pygame.transform.smoothscale(x, (372, 493))
You could alternatively skip a step and simply recreate the entire list using list comprehensions, which would make your script more idiomatic overall:
walkLeft = [pygame.image.load('Moving1.png'), ...]
walkRight = [pygame.transform.flip(x, True, False) for x in walkLeft]
walkLeft = [pygame.transform.smoothscale(x, (372, 493)) for x in walkLeft]
Upvotes: 2