您如何解决:“ AttributeError:'list'对象没有属性'player'”?

时间:2019-08-08 23:53:09

标签: pygame python-3.7.4

我正在制作一款平台不断变化的平台游戏。我有一个类,其中有用于创建移动平台的参数。然后,我将所有内容放入数组中,最后将它们绘制在for循环中。但是,我不断收到此错误:“ AttributeError:'list'对象没有属性'player'”。

class MovingPlatform(Platform):

    def __init__(self, sprite_sheet_data, change_x, change_y, x, y, 
                 boundary_top, boundary_bottom, boundary_left, 
                 boundary_right):

        super().__init__(sprite_sheet_data)

        self.change_x = change_x
        self.change_y = change_y

        self.rect.x = x
        self.rect.y = y

        self.boundary_top = boundary_top
        self.boundary_bottom = boundary_bottom
        self.boundary_left = boundary_left
        self.boundary_right = boundary_right

        self.player = None

        self.level = None

class Level(object):

     def __init__(self, player):
        self.platform_list = None
        self.platform_list = pygame.sprite.Group()
        self.player = player

    def update(self):
        self.platform_list.update()

    def draw(self, screen):
        self.platform_list.draw(screen)


class Level01(Level):

    def __init__(self, player):

        Level.__init__(self, player)

        level_moving_blocks = 
        [[MovingPlatform(STONE_PLATFORM_MIDDLE, 1, 0, 1350, 280, 0, 
          0, 1350, 1600)], 
         [MovingPlatform(STONE_PLATFORM_MIDDLE, 0, 1, 2000, 400, 
          300, 500, 0, 0)]
        ]

        for block in level_moving_blocks:
            block.player = self.player
            block.level = self
            self.platform_list.add(block)



Traceback (most recent call last):
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 672, in <module>
    main()
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 591, in main
    level_list = [Level01(player)]
  File "/Users/qingduliu/PycharmProjects/Platformer/Video Game.py", 
line 374, in __init__
    block.player = self.player
AttributeError: 'list' object has no attribute 'player'

1 个答案:

答案 0 :(得分:1)

您的列表中有列表。换句话说,列表嵌套是个问题。改为:

    level_moving_blocks = [
        MovingPlatform(STONE_PLATFORM_MIDDLE, 1, 0, 1350, 280, 0, 
                       0, 1350, 1600), 
        MovingPlatform(STONE_PLATFORM_MIDDLE, 0, 1, 2000, 400, 
                       300, 500, 0, 0)
      ]