Python/Pygame: subclass not finding variable

Probably not the right title but I've no idea how else to formulate it. I'm trying to calculate stats based on flexible values, specifically HP & MP and return the values so they can be printed on the screen. I've tried making a separate class for the stats and calling on that, changing it to self.HP in both the stats as well as when it's called upon but nothing seems to work and I can't figure out how to fix it. I know it's a lot of code but could someone have a look at it, please?

E1: After copying the right code here (I'm an idiot -_-') and changing HP back to self.HP the program goes completely bananas... It literally runs itself to death.

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])

Code that's in 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))

The error I'm getting: return "%s (%i HP, %i MP)" % (self.name, self.HP, self.MP) RuntimeError: maximum recursion depth exceeded

Answers


First is first, double importing is naughty so don't do it ;)

And fixing this is pretty simple, you just have to replace replace

super(battle_maiden, self).__init__(self)

with

character.__init__(self, name)

As of why, try to find out on your own. If you can't in 30 minutes, ask in comment and I will spoil it ;)


Need Your Help

Getting button and field on the same line in a fixed width div

html css forms position positioning

I have a form. The form contains a text field inside of a div. The button follows the div that contains the text field. By default, the button appears on the next line after the text field. I want ...

OpenGL 3: glBindVertexArray invalidates GL_ELEMENT_ARRAY_BUFFER

c++ opengl nvidia opengl-3

I was certain that if you bind a buffer via glBindBuffer(), you can safely assume that it stays bound, until the target is rebound through another call to glBindBuffer(). I was therefore quite surp...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.