首页 > 解决方案 > 如何在文本冒险中的对象之间指定?

问题描述

我制作了一个文字冒险游戏,玩家应该选择与哪个敌人战斗,我希望能够说“与食人魔战斗”并在蜘蛛出现时与食人魔战斗。我想通过对象的名称来调用对象以减少代码的总行数。

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

标签: classobjectcalladventure

解决方案


推荐阅读