(Pygame) Is it possible to change the scale on all images in a list?

Viewed 71

I am new to python and pygame and I am trying to make animations for my character, but all my sprites for the animations are to small for my display and background size.

Here is my code for my list and i figured out how to change the scale one image which was not in a list.

walkRight = [pygame.image.load("./gubbe/Run/Warrior_Run_1.png"), pygame.image.load("./gubbe/Run/Warrior_Run_2.png"), pygame.image.load("./gubbe/Run/Warrior_Run_3.png"), pygame.image.load("./gubbe/Run/Warrior_Run_4.png"),
             pygame.image.load("./gubbe/Run/Warrior_Run_5.png"), pygame.image.load("./gubbe/Run/Warrior_Run_6.png"), pygame.image.load("./gubbe/Run/Warrior_Run_7.png"), pygame.image.load("./gubbe/Run/Warrior_Run_8.png")]
walkLeft = [pygame.image.load("./gubbe/Run/Warrior_Run_1.png"), pygame.image.load("./gubbe/Run/Warrior_Run_2.png"), pygame.image.load("./gubbe/Run/Warrior_Run_3.png"), pygame.image.load("./gubbe/Run/Warrior_Run_4.png"),
            pygame.image.load("./gubbe/Run/Warrior_Run_5.png"), pygame.image.load("./gubbe/Run/Warrior_Run_6.png"), pygame.image.load("./gubbe/Run/Warrior_Run_7.png"), pygame.image.load("./gubbe/Run/Warrior_Run_8.png")]
bg = pygame.image.load("./gubbe/bg1.png")
char = pygame.image.load("./gubbe/idle/Warrior_Idle_1.png")

char = pygame.transform.rotozoom(char, 0, 2)
2 Answers

Generate a new list from the walkRight and walkLeft lists with List comprehensions::

walkRight = [pygame.transform.rotozoom(img, 0, 2) for img in walkRight]
walkLeft = [pygame.transform.rotozoom(img, 0, 2) for img in walkLeft]

You can simplify the code by using pygame.transform.scale2x() to double the size of an image:

walkRight = [pygame.transform.scale2x(img) for img in walkRight]
walkLeft = [pygame.transform.scale2x(img) for img in walkLeft]

If you do

transformed_walkRight = []
for im in walkRight:
   transformed_walkRight.append(pygame.transform.rotozoom(im, 0, 2))

you will apply pygame.transform.rotozoom() to each image in walkRight, and store the results in transformed_walkRight.

Related