0

可能不是正确的标题,但我不知道如何制定它。
我正在尝试根据灵活的值计算统计数据,特别是 HP 和 MP 并返回这些值,以便它们可以打印在屏幕上。我尝试为统计数据创建一个单独的类并调用它,在统计数据以及调用它时将其更改为 self.HP 但似乎没有任何效果,我无法弄清楚如何修复它。
我知道这是很多代码,但有人可以看看吗?

E1:在此处复制正确的代码(我是个白痴-_-')并将HP 改回self.HP 后,该程序完全变成了香蕉……它确实会自行运行至死。

import formulae
from formulae import *

characters = {"1":battle_maiden("Hibikime")}
enemies = {"a":enemy("Enemy")}

while characters["1"].alive and enemies["a"].alive:
    print
    for charactersKey, in sorted(characters.keys()):
        print(characters[charactersKey])

formulae.py 中的代码:

class character(object):
    def __init__(self, name):
        self.name = name
        self.alive = True
        self.Hit = 10
        self.Dodge = 0
        self.Lv = 1

    def __str__(self):
        if self.alive:
            return "%s (%i HP, %i MP)" % (self.name, self.HP, self.MP)
        else:
            return "%s (DEAD)" % self.name

    def attack(self, enemy):
        print(self.name, "attacks", enemy.name)
        enemy.hit()

    def hit(self):
        self.HP -= self.Atk
        print(self.name, "is hit! (",self.Atk,")")
        if self.HP <= 0:
            self.die()

    def die(self):
        self.alive = False
        print(self.name, "died.")


class enemy(object):
    def __init__(self, name):
        self.name = name
        self.alive = True
        self.HP = 100
        self.MP = 20
        self.M_Atk = 10
        self.Atk = 30
        self.Def = 5
        self.M_Def = 5
        self.Hit = 10
        self.Dodge = 0

    def __str__(self):
        if self.alive:
            return "%s (%i HP, %i MP)" % (self.name, self.HP, self.MP)
        else:
            return "%s (DEAD)" % self.name

    def attack(self, enemy):
        print(self.name, "attacks", enemy.name)
        enemy.hit()

    def hit(self):
        self.HP -= self.Atk
        print(self.name, "is hit! (",self.Atk,")")
        if self.HP <= 0:
            self.die()

    def die(self):
        self.alive = False
        print(self.name, "died.")


##SUBCLASSES FOR CHARACTERS##

class battle_maiden(character):
    def __init__(self, name):
        super(battle_maiden, self).__init__(self)
        classHP = 80
        classMP = 5
        Vitality = 10
        Wisdom = 3
        Intelligence = 2
        Strength = 15
        Defense = 5
        Dexterity = 15
        Agility = 2

        self.HP = (classHP + (self.Lv*5) + (Vitality *(self.Lv *0.3)))
        self.MP = (classMP + (self.Lv*2) + (Wisdom *(self.Lv *0.2)))
        Atk = (Strength * (self.Lv*0.2))
        M_Atk = (Intelligence * (self.Lv*0.2))
        Def = (Defense + (self.Lv*1.5) + (Defense*0.1))
        M_Def = (Intelligence + (self.Lv*1.5) + (Intelligence*0.1))

我得到的错误:
return "%s (%i HP, %i MP)" % (self.name, self.HP, self.MP)
RuntimeError: maximum recursion depth exceeded

4

1 回答 1

1

首先是第一,双重导入很顽皮,所以不要这样做;)

解决这个问题很简单,你只需要更换替换

super(battle_maiden, self).__init__(self)

character.__init__(self, name)

至于为什么,试着自己找出来。如果你不能在 30 分钟内,请在评论中提问,我会破坏它;)

于 2013-05-02T19:39:59.677 回答