user2423223
user2423223

Reputation: 115

ideas resetting stats in class

I made a simple program but when my monster dies the stats don't reset (hp mainly) I am lost In how to make it reset every time the monsters hp reaches 0 and the xp is awarded. I know that I can wright the code over again and again but I would like to be able to make it continue is as little amount of code as possible. I'm still learning python so I don't really know as much as everyone else here. Ive gotten to classes but not so much in depth here is the code:

import random

def title():
    print"hello Hero, welcome to staghold"
    print"you have traveld along way, and you find yourself"
    print"surrounded by and army of monsters as far as the eye can see"
    print"begin to draw your sword.....you run full speed towards the army"
    print"how many monsters can you kill before the inevatable comes?"
    raw_input("(press enter to continue)")

def stats():
    print"you have 200 health"
    print"your level is 1"
    print"you have 0 exp"
    raw_input("(press enter to continue)")
class monster:
    hp=50
    monsterattack=random.randint
    xp=random.randint(20,50)

health=200
level=1
exp=0
wave=1

title()
stats()
print"you run into a wave"
while level==1:
    if monster.hp<=0:
        print"you have defeated this wave of monsters"
        wave+=1
        exp+=monster.xp
        print" you get, "+str(monster.xp)+" exp from the monster"
        print"you now have, "+str(exp)+" exp"
        if exp>=300:
            level+=1
            if level==2:
                print"CONGRADULATIONS YOU HAVE REACHED LEVEL 2"
    elif monster.hp>=0:
        choice=raw_input("Will you 'fight' or 'run' from this horde?")
        if choice=='fight':
            print"you swing your sword at the monster"
            att=random.randint(2, 13)
            health-=monster.monsterattack(2,15)
            monster.hp-=att
            hp=200-health
            print"you do, "+str(att)+" damage to the monster"
            print"the monster does, "+str(hp)+"  damage to you"
            print"you have, "+str(health)+" health left"
            print"the monster has, "+str(monster.hp)+" health left"
        elif choice=="run":
            print"you got away from this wave safely"
        else:
            print"NOT A VALID CHOICE"

Upvotes: 1

Views: 151

Answers (1)

Paul Becotte
Paul Becotte

Reputation: 9977

I can see you're pretty early in your programming journey-

in your example, monster is a class. This means it is a definition of how an object will behave. That is good- but you never define an example of a monster. That would be something like

lion = monster()

which would create a new monster named lion. You need to set up a constructor in the monster class that tells the program how to build a new monster, for example

class Monster:
    def __init__(self):
        self.hp = 50
        self.xp = random.randint(20,50)

    def monsterattack(self):
        return random.randint()

This would allow you to create a monster, and allow you to have that monster attack with

damage = lion.monsterattack

You could then create a new lion, of class monster, during each loop - and have your hero fight it. The lion would only exist within the current run through of the loop, so each time you create a lion, it would be a completely new monster.

I like your dedication- keep at it, read some basic tutorials!

Upvotes: 1

Related Questions