class - 如何在文本冒险中的对象之间指定?
问题描述
我制作了一个文字冒险游戏,玩家应该选择与哪个敌人战斗,我希望能够说“与食人魔战斗”并在蜘蛛出现时与食人魔战斗。我想通过对象的名称来调用对象以减少代码的总行数。
class Enemy:
def __init__(self, name, hp, df, pw, st, ab):
self.name = name
self.hp = hp
self.df = df
self.pw = pw
self.st = st
self.ab = ab
Ogre = Enemy("Ogre", 300, 1.5, 75, "Live", "None")
Spider = Enemy("Spider", 110, 1.2, 12, "Live", "Venom")
response = input('What will you do? ')
# I want to say "fight ogre" in the terminal and see the game recognize that I'm fighting the ogre, without making a statement for each possibility by including both "if response == 'fight spider'" and "if response =='fight ogre'. I just want one line to apply to all, if that's possible. I know this next part is wrong, but it's my best.
if response.lower() == 'fight' + Enemy.name:
Opponent = Enemy
# I know simply stating the opponent's class won't work, but I hope you get the point.
Opponent.hp = - 18
解决方案
推荐阅读
- javascript - HTML中可调整大小的弹性表
- sql - 在 SQL Server T-SQL 中创建“特定”形式的 XML,而不使用字符串连接
- android - 如何使用线性布局动态制作这种类型的布局?
- c# - GC 用于 F# 和 C# 之间的互操作
- c# - 需要不同的模型类型,ASP.NET MVC
- java - 无空指针异常从 Map 的 Map 中获取值
- android - 命令以错误代码 0 完成:adb -s,emulator-5554,shell,ps
- c++ - c ++中的平衡括号
- python - 正则表达式匹配数据框python中字符串对应值的一部分
- r - 在绘图 R 中添加文本