2

Here is where my error jumps onto the screen. With this message: self.rect = self.image.get_rect() AttributeError: 'list' object has no attribute 'get_rect' Because im trying to make a sprite

rigto = [pygame.image.load("img/Iconos/guy1.png"), pygame.image.load("img/Iconos/guy2.png"), pygame.image.load("img/Iconos/guy3.png"), pygame.image.load("img/Iconos/guy4.png")]

class Player:
    def __init__(self, x, y, image):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

jugador1 = Player(500, 500, rigto)

1 Answers1

0

rigto is a list of images (pygame.Surface objects). You have to choose one image:

e.g.:

jugador1 = Player(500, 500, rigto[1])

Alternatively, you can manage the images in the class (e.g. for animations):

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)
        self.count += 1
        frame = self.count // 10
        if frame >= len(self.image_list):
            self.count = 0
            frame = 0
        self.image = self.image_list[frame]
jugador1 = Player(500, 500, rigto)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174