0

I have a problem with mi code, in the class Player, when im trying to image_list change the image to make the movement effect give me the error NameError: name "image_list" is not defined. And @Rabbid76 help me, but i dont know how to continue.

class Player:
    def __init__(self, x, y, image_list):
        self.image_list = image_list
        self.count = 0

        self.image = self.image_list[self.count]
        self.rect = self.image.get_rect(topleft = (x, y))

    def update(self): # update se utiliza para la posicion del jugador y su movilidad
        dx = 0
        dy = 0
        self.count += 1
        frame = self.count // 10
        if frame >= len(image_list):
            self.count = 0
            frame = 0
        self.count = self.image_list[self.count]
        key = pygame.key.get_pressed()
        if key[pygame.K_a]:
            dx -= 5
            self.direction = -1
        if key[pygame.K_d]:
            dx += 5
            self.direction = 1
        if key[pygame.K_w]:
            dy -= 5
        if key[pygame.K_s]:
            dy += 5
        if key[pygame.K_z]:
            pygame.mixer.music.play(-1, 0.0, 5000)
            draw_text("Tienes " + str(money) + " monedas", font_score, (0, 0, 0), 800, 10)
        # update player coordinates
        self.rect.x += dx
        self.rect.y += dy

    def draw(self):
        pantalla.blit(self.image, self.rect)
martineau
  • 112,593
  • 23
  • 157
  • 280
  • `image_list` is a class attribute. So it has to be `if frame >= len(self.image_list):` instead of `if frame >= len(image_list):` – Rabbid76 Dec 31 '21 at 16:48

0 Answers0