While working to create a basic text-adventure in the form of "Colossal Cave Adventure", "Zork", etc., I've encountered an issue with my Zombie and Skeleton classes seemingly editing each other.
class Entity(object):
def __init__(self,name,hp,strength,defense,armor=False,weapon=Fist(),actions=["Attack","Block"]):
self.name = name
self.hp = self.maxhp = hp
self.strength = strength
self.default_defense = self.defense = defense
self.armor = armor
self.weapon = weapon
self.initiative = 0
self.actions = actions
def attack(self,target):
#An attack action
def block(self):
#A block action
def update(self):
#Updating the entity
class Zombie(Entity):
def __init__(self):
Entity.__init__(self,"Zombie",random.randint(13,20),4,5,Leather())
print self.actions #Printing the actions in order to try to fix this issue
self.actions.remove("Block")
print self.actions #Printing the actions in order to try to fix this issue
class Skeleton(Entity):
def __init__(self):
Entity.__init__(self,"Skeleton",random.randint(16,23),6,5,False,Bow(999))
print self.actions #Printing the actions in order to try to fix this issue
self.actions.remove("Block")
print self.actions #Printing the actions in order to try to fix this issue
monsters = [Zombie(),Skeleton()]
When the code is run, it returns
['Attack','Block']
['Attack']
['Attack']
#Error message
The error says that 'Block'
isn't in the Skeleton's self.actions
to remove, but as far as my understanding goes, 'Block'
should be in there when Entity.__init__
is called. If I switch Zombie()
and Skeleton()
in monsters
, the problem still happens, so the problem seems to be that the first subclass is removing the entry from both subclasses.
I'm new to subclasses, so it's very likely that the issue is in my limited understanding of how they work. Is this the intended behavior? If so, how would I get the behavior I'm looking for?