Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I started programming a month ago. I took up python as my learning language and thought of making a rpg game(text-based) from internet sources. Here is the code-

from random import randint
Python
class Dice:
    def die(num):
        die=randint(1,num)
        return die

class Character:
    def _init_(self,name,hp,damage):
        self.name=name
        self.hp=hp
        self.damage=damage

class Fighter(Character):
    def _init_(self):
        super()._init_(name=input("what is your character's name?"),
                       hp=20,
                       damage=12)
        prof="fighter"

class Assasin(Character):
    def _init_(self):
        super()._init_(name=input("what is your character's name?"),
                       hp=15,
                       damage=14)
        name=input("what is your character's name?")
        prof="assasin"

##NOW FOR ENEMIES

class Goblin(Character):
    def _init_(self):
        super._init_(name="Goblin",
                     hp=15,
                     damage=3)
        hp=15
        name="Goblin"
        damage=3

class Ogre(Character):
    def _init_(self):
        super._init_(name="Ogre",
                     hp=25,
                     damage=6)
        name="Ogre"
        hp=25
        damage=6




def profession():
    print("What is your class?",'\n' "Press f for fighter" , '\n' "Press a for assasin")
    pclass=input("Enter your choice>>>")
    if(pclass=="f"):
        Prof=Fighter()
    elif(pclass=="a"):
        Prof=Assasin()
    else:
        Prof=Fighter()
    return Prof

def ranmob():
    if Dice.die(2)<2:
        mob=Ogre()
    else:
        mob=Goblin()
    return mob

def playerAttack():
    print("You hit")
    if (hero.prof=="fighter"):
        if(Dice.die(8)<8):
            print("them for 12 damage")
            mob.hp-=12
            print("The",mob.name,"has",mob.hp,"hp left")
        else:
            print("You missed your attack")
    elif(hero.prof=="assasin"):
        if(Dice.die(8)<8):
            print("them for 14 damage")
            mob.hp-=14
            print("The",mob.name,"has",mob.hp,"hp left")
        else:
            print("You missed your attack")

def monsterAttack():
    print("The monster attacks")
    if(mob.name=="Ogre"):
        if(Dice.die(8)<8):
            print("6 damage taken")
            hero.hp-=6
        else:
            print("The attack misses")

    elif(mob.name=="Goblin"):
        if(Dice.die(8)<8):
            print("3 damage taken")
            hero.hp-=3
        else:
            print("The attack misses")

def commands():
    if hero.prof=="fighter":
        print("press f to fight\n","press e to pass")
        command=input(">>>>>")
        if(command=="f"):
            playerAttack()
        elif command=="e":
            pass

mob=ranmob()
hero=profession()

#print("name hp",'\n',hero.name,hero.hp)

while True:
    if mob.hp<=0:
        print('The',mob.name,'is dead')
        mob=ranmob()
    if hero.hp<=0:
        print(hero.name,'died!')
        hero=profession()
        print("name hp",'\n',hero.name,hero.hp)

    print("You see",mob.name,",",mob.name,"has",mob.hp,"hp")
    if hero.hp>0:
        commands()
    if mob.hp>0:
        monsterAttack()

But when I try to run it,it shows the initial selection of characters after which it throws an error-
Traceback (most recent call last):
  File "C:/Users/Hi/Desktop/python programs/nvm/MYFIRSTGAME.py", line 117, in <module>
    if mob.hp<=0:
AttributeError: 'Goblin' object has no attribute 'hp'

Can anyone help me out here? Thanks in advance :-)

What I have tried:

Not sure what is causing the problem
Posted
Updated 1-Feb-18 2:53am
v2

Constructor is
__init__
(note double underscores), not
_init_
 
Share this answer
 
v3
Comments
Sushmit Chakraborty 1-Feb-18 9:01am    
Thankyou :-)
CPallini 1-Feb-18 9:13am    
You are welcome.
Richard MacCutchan 1-Feb-18 9:02am    
+5, eagle eyes.
CPallini 1-Feb-18 9:14am    
Nope. Experimental evidence. :-)
BTW Thank you, Richard.
In addition to Carlo's well spotted mistake:
- You do not need to declare name, hp and damage in your subclasses, as the are already defined in Character.
- The Fighter and Assassin classes need the self keyword before the prof attribute.
 
Share this answer
 
Comments
Sushmit Chakraborty 1-Feb-18 9:27am    
Thankyou very much,you solved 2 of my problems :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900