【发布时间】:2016-01-30 23:16:14
【问题描述】:
在以“巨大洞穴冒险”、“Zork”等形式创建基本文本冒险游戏时,我遇到了一个问题,我的 Zombie 和 Skeleton 类似乎相互编辑。
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()]
代码运行时返回
['Attack','Block']
['Attack']
['Attack']
#Error message
错误说'Block' 不在骨架的self.actions 中删除,但据我了解,'Block' 应该在调用Entity.__init__ 时在那里。如果我在monsters 中切换Zombie() 和Skeleton(),问题仍然存在,所以问题似乎是第一个子类正在从两个子类中删除条目。
我是子类的新手,所以问题很可能在于我对它们的工作原理的有限理解。这是预期的行为吗?如果是这样,我将如何获得我正在寻找的行为?
【问题讨论】:
-
我没有选票,但请参阅stackoverflow.com/q/1132941/3001761
-
@jonrsharpe 非常有帮助。我很高兴能够理解问题,而不仅仅是简单地解决问题。