python - 无法将方法添加到 Python 中的封闭类中存在的列表中
问题描述
这是我的挑战:
我正在开发一款游戏,并且我试图将所有玩家/敌人的能力都内置到方法中,以便更加“pythonic”(如果我误用了这个表达式,请道歉)。
我成功地从父类(即“Actor”,这里)收集方法列表作为 self.abilities,但我不能在继承“Actor”的类中添加到同一个列表中。
我已经进行了一些研究,并且我知道继承是棘手的,这种方式,并且还有很多其他方法可以将方法附加到每个类,但它们大多看起来很麻烦并且完全违背了拥有这些类的目的。
这是我想做的快照/示例:
livingThing --> 演员(可以攻击/治疗)--> 怪物(可以攻击/治疗/施法)--> 龙(可以攻击/治疗/施法/飞行)--> 等等
我试图避免在类的每次新迭代中复制相同的方法列表,并且我还试图避免使该游戏中的每个对象都成为“Actor”。
但是,因为我在init下有能力列表,但我似乎无法在方法之外访问它。我目前将 Monster 和 Ally 设置为简单地“通过”只是为了向自己证明他们确实继承了 Actor 的特征,但我无法从任何地方修改该列表,只能在 Actor 内部进行。
任何人都可以帮忙吗?谢谢!
# Anything that can interact directly (take action)
class Player(livingThing):
def __init__(self,name="The Stranger", HP=10, MP=5, strength=1, intellect=1, spirit=1, luck=5, gil=6):
self.name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.luck = luck
def act(player, enemy):
actions = {
"attack" : player.attack,
"heal" : player.heal,
"flee" : player.flee
}
#Takes input from the player
player.safe = False
while ((player.HP > 0) and (enemy.HP > 0)) and (player.safe != True):
decision = input("What would you like to do? ")
#Sets the user's input as lower-case and checks for it within the dictionary
if decision.lower() in actions:
actions[decision.lower()](enemy)
if player.safe != True:
enemy.agreact(player)
player.printHP(enemy)
else:
print("That didn't workkkkkk! Try again.")
# Prints both player and enemy HP
def printHP(player, enemy):
print("{0}'s' HP: {1} \n{2}'s HP: {3}".format(player.name, player.HP, enemy.name, enemy.HP))
# Allows the player to attack an enemy (currently functional)
def attack(player, enemy):
enemy.HP -= player.strength
print("You strike {0} for {1} damage!".format(enemy.name, player.strength))
#player.printHP(enemy)
# Allows the player to heal a certain amount of health based on its "spirit" stat (currently functional)
def heal(player, enemy):
healed = randint(0, player.spirit)
player.HP += healed
print("You've healed for {0}!".format(healed))
#player.printHP(enemy)
#Allows the player to attempt to run away
def flee(player, enemy):
randluck = randint(0, player.luck)
if randluck > 3:
print("You successfully escaped!")
player.safe = True
else:
print("You weren't able to escape!")
# Anything that can act with/against the player
class Actor(livingThing):
def __init__(self, name="Unknown Entity", HP=10, MP=2, strength=1, intellect=1, spirit=3, gil=3):
self. name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.abilities = [self.strike, self.heal]
def printabilities():
print(self.abilities)
# Chooses how your opponent will respond to your attack
def agreact(self, player):
choice = randint(0, len(self.abilities)-1)
self.abilities[choice](player)
# A basic "hit back" reaction from your opponent--everyone can do this
def strike(self, player):
player.HP -= self.strength
print("{0} hit {1} for {2}!".format(self.name, player.name, self.strength))
def heal(self, enemy):
healed = randint(0, self.spirit)
self.HP += healed
print("{0} healed for {1}!".format(self.name, healed))
class Monster(Actor):
pass
class Ally(Actor):
pass
解决方案
推荐阅读
- javascript - Angular 6 modal,作为单独组件使用时增加了modal的宽度
- getorgchart - GetOrgChart 中的错误文本位置
- javascript - 在 Ionic 物理设备中获取用户的当前位置(iOS 版本)
- php - Route::delete 在 Laravel 中没有响应
- angular - 如何在ionic3中打印页面中的对象数据
- keras - 神经网络:为什么我不能更深入?
- ios - 核心数据迁移是否必要?
- python - 如何在 selenium python 中获取 Web 元素的正确位置
- mysql - 如何获得每辆经过停靠站的公共汽车的下 2 个停靠站时间(GTFS 数据)?
- javascript - 获取文件的路径会降低我的 API 性能