mardi 18 avril 2017

AttributeError: type object ' ' has no attribute ' '

I already asked something similar on the site, but I can't get over this error. The code that I'm using is:

class Enemy:
# Base class to create all future enemies
def __init__(self, name, hp, dmg, desc): = name  # Enemy name
    self.hp = hp  # Enemy hp
    self.dmg = dmg  # Enemy damage
    self.desc = desc  # Enemy description (Will make sense later)

def is_alive(self):
    return self.hp > 0

The code above is used to define all future enemies, as I stated on with #. Then, I created three enemies for testing, as my game will generate random encounters from a list. However, I'm only going to list one because the error happens with any enemy:

class Goblin(Enemy):
def __init__(self):
    super().__init__(name = "Goblin",
                     hp = 40,
                     dmg = 33,
                     desc = "An ordinary goblin runs in your direction!")

Then, as the enemies do, the rooms of the dungeon also have a template class and a list for random purposes, but as I did before, I will only list one because it's the one that is causing errors to me:

class Room:
# Template to define all future rooms
    def __init__(self, intro):
        self.intro = intro

class VsRoom(Room):
# Room with an enemy
    duelist = None

    def __init__(self):
        self.duelist = duelist = random.choice(Enemy_List)
        super().__init__(intro = "There's an enemy here! \n%s" %(duelist.desc))

And then there's the "def" to run the game (currently just for testing):

def print_room():
#  Tells the player the type of room they are
    print("You enter the next room...")
    gen_room = random.choice(Rooms)()  # ("Rooms" is a list) 
    if gen_room == VsRoom:
        combat()  # No need to list the combat() definition, I think.

But when I run, I get this:

AttributeError: type object 'Goblin' has no attribute 'desc'

I know that AttributeError is quite easy to find here, but it seems that none of the existing questions suits me. If anybody knows why the code fails, please tell me.


0 commentaires:

Enregistrer un commentaire