Mads Lildholdt Hansen
Mads Lildholdt Hansen

Reputation: 49

Early stages of text-based rpg - classes and methods

I am in the very early stages of making my own text-based Rpg with python 3. I decided to create classes (still not finished with that) to maintain the different characters. I just ran into a problem regarding the attacking part of the game.

I am trying to make a method for my class, that attacks something. My problem is then to have it attack an enemy. I tried combining it with the class for the enemy, so the character attacks the enemy's hp. This, however, creates an error, saying I did not define the hp for the enemy.

This is my classes:

class Mage():
    def __init__(self,name):
        self.name = name
        self.hp = 100
        self.mana = 200
        self.spellpower = 1
    def fireball(self, enemy):
        dmg = 10 * self.spellpower
        self.mana -= 20
        enemy.hp -= dmg
class Orc():
    def __init__(self):
        self.name = "Orc"
        self.hp = 20
        self.dmg = 5
    def hit(self):
        dmg = self.dmg

and this is the rest of the code (where I use the fireball method in the end):

print ("Welcome to the game")
name = input("What is your name? ")

class1 = input("Choose your class: (Fighter, Mage) ")
if class1 == "Fighter" or class1 == "fighter":
    print (f"welcome Fighter,{name}. Grab your sword and come with me")
    class1 = Fighter(name)
if class1 == "Mage" or class1 == "mage":
    print (f"Come with us Mage {name}, we need your help")
    class1 = Mage(name)
class1.fireball(Orc)

Upvotes: 1

Views: 603

Answers (1)

Yacine Mahdid
Yacine Mahdid

Reputation: 731

You are calling fireball on Orc which is the class, but not the instance. Instantiate first an orc and then call fireball on it. This is equivalent to trying to call fireball on the entire Orc race instead of calling fireball on a particular orc.

Upvotes: 1

Related Questions