-4

作为同一个 RPG 的一个单独部分,我在攻击敌人时遇到了问题。函数 do_damage 从enemy_dict 列表中获取名称/值(健康)并计算造成的伤害。

def do_damage(self, enemy):
    damage = min(
        max(randint(0, self.health) - randint(0, self.enemy[1]), 0), self.enemy[1])
    self.enemy[1] = self.enemy[1] - damage
    if damage == 0:
        print "The %s evades %s's attack." % (self.enemy[0], self.name)
    else:
        print "%s hurts the %s! -%dhp" % (self.name, self.enemy[0], damage)
    return enemy[1] <= 0

self.enemy 是这样分配的:

self.enemy = random.choice(enemy_dict)

作为 explore 函数的一部分,当被调用时,它提供了一个遇到敌人的机会。

当玩家调用攻击函数时,会调用 do_damage 函数。

def attack(self):
    if self.state != 'fight':
        print"%s swings wildly at nothing." % self.name
    else:
        if self.do_damage(self.enemy[1]):
            print "%s slaughters %s!" % (self.name, self.enemy[0])
            self.enemy = None
            self.state = 'normal'
            eloot = randint(0,25)
            self.loot = self.loot + eloot
            qxp = randint(0,30)
            self.xp = qxp + self.xp
            print "You find %d gold on the corpse." %(eloot)
            stimp = randint(0,4)
            if stimp == 0:
                self.stim = self.stim + 1
                print "You find a potion!"
            else:
            self.enemy_attacks()
def enemy_attacks(self):
    if self.enemy[0].do_damage(self): print "%s was disemboweled by %s!" % (self.name, self.enemy[0])

攻击函数是 Player 类的一部分,而 do_damage 是 Character 类的一部分,在 Player 类中调用。

作为参考,这里是enemy_dict 列表:

enemy_dict = [
    ("sprite", 5),
    ("thief", 10)
]

我如何让玩家用我所拥有的攻击敌人?

4

2 回答 2

2

我将假设您从enemy_dict列表中获取其中一件事,并将其作为参数传递给您的玩家角色__init__并将其分配给self.enemy. 问题是您正在尝试更改该元组中的第二个值(我假设是敌人的 HP)。您不能为元组中的值分配任何内容 - 元组是不可变的。

>>> t = (1,2,3)
>>> t[1] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

所以,你需要使用 alist而不是 a tuple, a la enemy_dict = [["sprite", 5], ...]

不过,您在帖子中使用了其他一些令人担忧的语言。

Enemy 是一个类,其中 self.enemy 被分配(我假设)

“我假设”是什么意思?你没有写这门课吗?此外,您没有enemy在构造函数中为此类的属性分配任何内容,因此您显然没有self.enemyEnemy类中进行分配。

此外,您正在尝试调用.keys(),并且.values()enemy_dict其上是一个列表。它没有那些方法。如果它是 a dict,那么无论如何您都会尝试将类似的东西分配给['sprite', 'thief']敌人的名字,同样也[5, 10]分配给健康。

您也没有enemy_dict作为参数传递给Enemy's __init__。如果您自己编写了此类,则应该将其更改为:

def __init__(self, name, health):
    self.name = name
    self.health = values

并这样称呼它:an_enemy = Enemy(enemy_dict[0][0], enemy_dict[0][1]). 这是假设你不会改变enemy_dict,你真的应该这样做。我可以建议:

enemy_dict = {
    "sprite": 5,
    "thief": 10
}

然后你可以通过说获得健康enemy_dict[name]。但是,您必须选择一个名称 - 您可以通过说 来确保您选择了一个有效的名称if name in enemy_dict.keys()。你现在可以说an_enemy = Enemy(name, enemy_dict[name])

如果你想实现“随机遭遇”,就像使用random模块一样简单。您已经使用了random.choice- 所以这应该不会太难。enemy_dict.keys()如果您使用字典,请使用。

self.enemy = random.choice(enemy_dict.keys())

上面的代码使enemy属性成为名称,这是该字典的键。为了获得健康,当然是enemy_dict[self.enemy]

您应该注意,如果您修改 中的生命enemy_dict值,这将影响每个对象对该敌人生命值的感知。如果你enemy_dict代表独特的实体,那很好 - 但如果你想拥有多个"sprite",你可以使用enemy_dict作为一种查找表,从中获取随机类型的敌人进行战斗,你最好使用这样的东西:

self.enemy_name = random.choice(enemy_dict.keys())
self.enemy_health = enemy_dict[self.enemy_name]

然后,enemy_health从您的玩家角色的职业中进行修改。

于 2013-07-19T23:54:51.740 回答
0

The declaration of the Enemy class does not make sense to me, because it means that one instance of Enemy has all the enemy names and health defined in it. I would have written this way:

class Enemy(Character):
    def __init__(self, name, health):
        self.name = name
        self.health = health

But even now, the code you wrote does not fit with what I understand: you call self.do_damage() with the parameter enemy=self.enemy[1]. According to the do_damage method, this object is a list where enemy[0] is the name of the enemy, and enemy[1] is his health. I would have written respectively enemy.name and enemy.health:

def do_damage(self, enemy):
    damage = min(max(randint(0, self.health) - randint(0, self.enemy.health), 0), self.enemy.health)
    self.enemy.health = self.enemy.health - damage
    if damage == 0:
        print "The %s evades %s's attack." % (self.enemy.name, self.name)
    else:
        print "%s hurts the %s! -%dhp" % (self.name, self.enemy.name, damage)
    return enemy.health <= 0

and I would have call do_damage differently:

def attack(self):
    if self.state != 'fight':
        print"%s swings wildly at nothing." % self.name
    else:
        # we suppose that self.enemy is an instance of the Enemy class
        # initialized somewhere else
        if self.do_damage(self.enemy):
            print "%s slaughters %s!" % (self.name, self.enemy.name)
            self.enemy = None
            self.state = 'normal'
            eloot = randint(0,25)
            self.loot = self.loot + eloot
            qxp = randint(0,30)
            self.xp = qxp + self.xp
            print "You find %d gold on the corpse." %(eloot)
            stimp = randint(0,4)
            if stimp == 0:
                self.stim = self.stim + 1
                print "You find a potion!"
        else:
              self.enemy_attacks()

def enemy_attacks(self):
    if self.enemy.do_damage(self):
        print "%s was disemboweled by %s!" % (self.name, self.enemy.name)
于 2013-07-19T23:56:11.390 回答