0
\$\begingroup\$

I'm using pygame and trying to create an animation by going through a list of variables:

self.image = self.walk_right[self.index]

I have loaded the images into variables and now I want to add them to my list. I assume there should be a way to use a for loop to do this.

I tried using a formatted string to append them to the list, however then they become strings and I get the error:

TypeError: Source objects must be a surface

Is there a way to convert these strings back to being variables? Is there a better way to do this? Or do I need to type out the name of each frame once i've imported it from the sprite sheet?

        walk_right_1 = sprite_sheet.get_image(1,11,sprite_width,sprite_height,1.25,0)
        walk_right_2 = sprite_sheet.get_image(2,11,sprite_width,sprite_height,1.25,0)
        walk_right_3 = sprite_sheet.get_image(3,11,sprite_width,sprite_height,1.25,0)
        walk_right_4 = sprite_sheet.get_image(4,11,sprite_width,sprite_height,1.25,0)
        walk_right_5 = sprite_sheet.get_image(5,11,sprite_width,sprite_height,1.25,0)
        walk_right_6 = sprite_sheet.get_image(6,11,sprite_width,sprite_height,1.25,0)
        walk_right_7 = sprite_sheet.get_image(7,11,sprite_width,sprite_height,1.25,0)
        walk_right_8 = sprite_sheet.get_image(8,11,sprite_width,sprite_height,1.25,0)
        walk_right_9 = sprite_sheet.get_image(9,11,sprite_width,sprite_height,1.25,0)
        self.walk_right = []

        for num in range (1,10):
            self.walk_right.append(f'walk_right_{num}')
\$\endgroup\$
1
  • 3
    \$\begingroup\$ You've phrased this like a general programming question, "how do I write syntactically correct code that puts variables in a list", which belongs on StackOverflow, not here. When asking here, focus on the game development problem to solve (create list of images for animation), not the programming syntax you're struggling with. This will also help you avoid the X/Y problem. \$\endgroup\$
    – DMGregory
    Commented Jul 27, 2022 at 10:46

1 Answer 1

1
\$\begingroup\$

You can simply put sprite_sheet.get_image into the loop:

self.walk_right = []

for num in range (1,10):
    walk_right_temp = sprite_sheet.get_image(num,11,sprite_width,sprite_height,1.25,0)
    self.walk_right.append(walk_right_temp)

Or use python's list comprehension:

self.walk_right = [sprite_sheet.get_image(num,11,sprite_width,sprite_height,1.25,0) for num in range (1,10)]

Or, It is indeed possible to find local/global/member variables by string if really necessary(Most of the time you don't need to do this):

self.__dict__["walk_right"]= []
for num in range (1,10):
    self.__dict__["walk_right"].append(locals()[f'walk_right_{num}'])
\$\endgroup\$
3
  • \$\begingroup\$ Hey. Thank you, putting the get_image into the loop as per your first suggestion worked perfectly. I'm confused though as it appears you are setting the variable to "walk_right_temp" and then appending it to the list, then assiging a different surface to the same variable. I thought this would over write or cause an issue with that variable but it works perfectly and when I print the list it displays all nine surfaces. Thanks again! \$\endgroup\$
    – jacko_123
    Commented Aug 3, 2022 at 1:55
  • \$\begingroup\$ In fact, Saying it's "the same variable" is inaccurate. It is a value type variable that holds the actual image heap memory address. When it is assigned another value, the previously used value does not change at the same time, because it was copied when it was passed to the function. \$\endgroup\$
    – Mangata
    Commented Aug 3, 2022 at 9:39
  • \$\begingroup\$ BTW,In the equivalent syntax of some languages, the local variables in different loop rounds are not actually the same address. When entering the next round of loops, the variables are cleared because they leave the scope. In python, the interpreter will scan the entire local variables of the function body, and treat them as local variables whose scope is the function body, which is an optimization term. so in python they are indeed "the same variable". But it doesn't matter, the result is the same. \$\endgroup\$
    – Mangata
    Commented Aug 3, 2022 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .