AtrributeError:类型对象'Player'没有属性'image_dict'

时间:2014-03-22 14:15:25

标签: python pygame

试图让一些字母为一些精灵工作。

class Player(Digimon):
def __init__(self):
    super(Player, self).__init__(0,2,0,0,0,0) # age, weight, str, def, speed, intelligence
    self.image_dict = {(0,2,0,0,0,0): itertools.cycle([pygame.image.load("images/egg_sprite_1.png"),
                                                   pygame.image.load("images/egg_sprite_2.png"), 
                                                   pygame.image.load("images/egg_sprite_3.png")]), 
                   (2,4,0,0,2,5): itertools.cycle([pygame.image.load("images/leaf_1.png"), 
                                                    pygame.image.load("images/leaf_2.png"), 
                                                    pygame.image.load("images/leaf_3.png"),
                                                    pygame.image.load("images/leaf_4.png")])}

然后在主循环中

Player.images = Player.image_dict[(Player.age, Player.weight, Player.strength, Player.defence, Player.speed, Player.intelligence)] 

Player.image = next(Player.images)

我收到标题中所述的错误。我看过类似的问题,但他们似乎没有帮助。据我所知,它应该工作。 Player.image_dict是在Player(digimon)子类中创建的,所以我不知道没有属性。

2 个答案:

答案 0 :(得分:2)

嗯,实际上你需要创建一个Player类的实例,如下所示:

player = Player()

然后你可以打电话:

player.images = player.image_dict[(player.age, player.weight, player.strength, player.defence, player.speed, player.intelligence)] 

player.image = next(player.images)

请注意,这些是player对象,而不是Player

答案 1 :(得分:1)

Player.image_dict 这样您就只能访问类Player的静态成员。如果你不想这样做,你需要创建一个类播放器的对象。像这样

Player player = Player(<arguments>)
player.image_dict(...)