0

I'm trying to make my code cycle through images every time I hit an arrow key to move it to make it appear like animated walking. However, the sprite is keeping the initial image as it moves around. Here I've included an example of the up movement.

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.image.load("down1.png").convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()
        
    def update(self, pressed_keys):
        up_counter = 0
        down_counter = 0
        right_counter = 0
        left_counter = 0
        if pressed_keys[K_UP]:
            player = pygame.image.load(up_images[up_counter]).convert()
            self.surf.set_colorkey((255, 255, 255), RLEACCEL)
            up_counter = (up_counter + 1) % len(up)
            self.rect.move_ip(0, -5)
            down_counter = 0
            left_counter = 0
            right_counter = 0

Does anyone know how I could fix this? Please let me know if I'm missing any info that could be helpful in solving. Thanks!

FilchsRat
  • 3
  • 1
  • maybe first use `print` to see which part of code is executed and what you have in variables. It is called `"print debuging"` and it helps to see what code is doing. Especially check variables in `update` – furas May 20 '22 at 00:17
  • in `update` you always set `up_counter = 0`, etc. and this resets all your calculations and image never change. You should set it only once - in `__init__` as `self.up_counter` - and later use this `self.up_counter` to keep this value to next execution. – furas May 20 '22 at 00:18

1 Answers1

0

I see two mistakes in update (and they are connected)

First:

In update you always set up_counter = 0 at start and this resets all your calculations and image always use up_images[0]. But removing it can't help. And there is another mistake (with self.).

Second:

You should set up_counter = 0 only once but in __init__ as self.up_counter - and later use this self.up_counter. And this self. will also keep value to next execution.


It should be something like this:

BTW: it has to be self.surf instead of player

class Player(pygame.sprite.Sprite):
    
    def __init__(self):
        super().__init__()
        
        self.surf = pygame.image.load("down1.png").convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()
        
        self.up_counter = 0
        self.down_counter = 0
        self.right_counter = 0
        self.left_counter = 0
        
    def update(self, pressed_keys):
        if pressed_keys[K_UP]:
            self.surf = pygame.image.load(up_images[up_counter]).convert()
            self.surf.set_colorkey((255, 255, 255), RLEACCEL)
            up_counter = (up_counter + 1) % len(up)
            self.rect.move_ip(0, -5)
            self.down_counter = 0
            self.left_counter = 0
            self.right_counter = 0

Code may work better if you load all images at start (and convert, and set key) and keep on list or dictionary to get it directly from memory.

furas
  • 119,752
  • 10
  • 94
  • 135