[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Odd problem with lists
Howdy all, new guy here.......been lurking for a few days, have been
playing with Pygame for a little over a month now. I originally ran
across Pygame while looking for a joystick API for Python (for a
completely unrelated project), but now I'm having serious fun making a
2D side scroller. :)
I just ran into a strange problem with lists.........it sorta looks like
a Python bug, I sure can't see what else could be going on. Here's the
deal: I have an "images" class that loads images for my player image. To
handle animations in a convenient and flexible fashion, I'm making a
list in images (as an attribute, "images.player_ladder_imgs") and
parsing through this in my player class. For right now, for testing, I
have a list as an attribute of the player object ("player.ladder_imgs"),
which I create from the "images.player_ladder_imgs" list when the player
is initialized. This works fine. To handle the animating, I set the
player's image every frame to be the first element of my animation
images sequence, player.ladder_imgs[0]. On every iteration,
player.ladder_imgs[0] is removed. When image.ladder_imgs reaches 1, I
re-copy the list from the images class to player.ladder_imgs, using the
*exact* code I used when initializing the player object........but it
doesn't seem to do anything. The list stays at its current state (so it
ends up being empty, and therefore "self.image = self.ladder_imgs[0]"
throws an error). Here's some example code:
(from my player class):
self.ladder_imgs.remove(self.ladder_imgs[0])
if len(self.ladder_imgs) < 1:
print "caught ladder list at 0!"
self.ladder_imgs = images.player_ladder_imgs
# ^ list of player images for
animation.......never changes
midtop = self.rect.midtop
for thing in self.ladder_imgs:
print thing
print ""
print ""
self.image = self.ladder_imgs[0]
self.rect = self.image.get_rect()
self.rect.midtop = midtop
With the printing stuff, I see that self.ladder_imgs never gets changed
back to images.player_ladder_imgs, yet I see the print message so I know
that statement is being executed. So I tried something
else........instead of using the predfined list in my images class, I
build the list again:
self.ladder_imgs.remove(self.ladder_imgs[0])
if len(self.ladder_imgs) < 1:
print "caught ladder list at 0!"
player_ladder1 = pygame.image.load(os.path.join(images.dir,
'player_ladder1.png')).convert()
player_ladder2 = pygame.image.load(os.path.join(images.dir,
'player_ladder2.png')).convert()
player_ladder3 = pygame.image.load(os.path.join(images.dir,
'player_ladder3.png')).convert()
player_ladder4 = pygame.image.load(os.path.join(images.dir,
'player_ladder2.png')).convert()
player_ladder5 = pygame.image.load(os.path.join(images.dir,
'player_ladder1.png')).convert()
self.ladder_imgs = [player_ladder1, player_ladder2, player_ladder3,
player_ladder4, player_ladder5]
midtop = self.rect.midtop
for thing in self.ladder_imgs:
print thing
print ""
print ""
self.image = self.ladder_imgs[0]
self.rect = self.image.get_rect()
self.rect.midtop = midtop
This works! But the other way should work too! Especially since
"self.ladder_imgs = images.player_ladder_imgs" is the exact line I use
in the __init__ function of my player object to define the list in the
first place!
What could be wrong?
-Matt Bailey
PS: I realize the order of events in my code keeps the first image from
the animation sequence from ever being used, I'll move it later. :)