pygame 中的属性错误

AttributeError in pygame

这是我的错误跳到屏幕上的地方。有了这个消息: self.rect = self.image.get_rect() AttributeError: 'list' 对象没有属性 'get_rect' 因为我正在尝试制作精灵

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)

rigto 是图像列表(pygame.Surface 个对象)。您必须选择一张图片:

例如:

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

或者,您可以管理 class 中的图像(例如动画):

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)