So basically im creating this python adventure text game. And so far its good. But i have this problem with class instances(Is that the right term?)

Say for example i have this class,

    class monster(object):
        def __init__(self,health,attack):
            self.damage = damage
            self.health = health

So then I create a class object(again, is that the right term?)

    goblin = monster(5,30)
    skeleton = monster(10,20)

so now, i have this piece of code for the fighting mechanic between the player and the monster. (I just copy past this so the indentation might be messed up)

    if char.location.war == True:
    while death == False:       
        #check where located for enemy localization
        if char.location == GoblinCmp:
            enemy = goblin
            boss = goblinChief
        elif char.location == Forest:
            enemy = skeleton
            boss = SkeletalKing

        #Randomly Chooses wave
        wavenum = random.randrange(1,4)
        currentwave = 0
        #Actual Fighting stage

        if currentwave > wavenum:
            enemy = boss

        if enemy.boss == False:
            print "Wave %s!\n" % currentwave
            print "A %s attacks you!" % enemy.name
        if enemy.boss == True:
            print "The miniboss %s appears! Get ready!" % enemy.name

        if currentwave <= wavenum:               
            while death == False:
                print fightstats % {'1':enemy.name,'2':enemy.health,'3':enemy.att,'b':char.health,'c':char.att}


                #User choices when fighting
                if fightuser == "attack":
                    outcome = random.choice(attackstat)
                    if outcome == 'hit':
                        enemy.health = enemy.health - char.att
                        print ('You hit him! You dealt %(1)s damage to the %(a)s!\n') % {'1':char.att,'a':enemy.name}
                    elif outcome == 'miss':
                        print ('You attacked, but missed!\n')
                    elif outcome == 'graze':
                        currentatt = char.att / 2
                        enemy.health = enemy.health - currentatt
                        print ('You grazed it! You dealth %(1)s damage to the %(2)s!\n') % {'1':currentatt,'2':enemy.name}

                if enemy.health <= 0:
                    print 'You defeated the %(a)s! You earned %(b)s gold! \n' % {'a':enemy.name, 'b':enemy.drops}
                    print 'Press enter to continue!'
                    char.gold = char.gold + enemy.drops
                    currentwave = currentwave + 1

                eneoutcome = random.choice(attackstat)
                #Enemy Hits
                if eneoutcome == 'hit':
                    char.health = char.health - enemy.att
                    print ('The %(a)s hit you! It dealth %(b)s damage to you!\n') % {'a':enemy.name,'b':enemy.att} 
                #Enemy Miss
                elif eneoutcome == 'miss':
                    print ('The %s attacked, but missed!\n') % enemy.name
                #Enemy Graze
                elif eneoutcome == 'graze':
                    grazed_attack = enemy.att / 2
                    char.health = char.health - grazed_attack
                    print ('The %(a)s grazed you! It dealth %(b)s damage to you!\n') % {'a':enemy.name,'b':grazed_attack}

                if char.health <= 0:
                    print 'You Died! Press enter to End game!'
                    death = True

As you can see, i have a wave system in this. everytime a player enters a location that has a "war" property, they have to fight a few waves of monsters. The problem here is that i thought if i did

    enemy = goblin #Or something like this.

It would create a whole new class object(Im sure that isn't the proper term.) apprantly it doesn't. Instead everytime i do something like enemy.health = 0 it changes goblin.health as well.I dont want that to happen. I only need enemy.health to change. Is there anyway something like that might be possible? Also optional, is there anyway to clean up this code a little?

5 Years
Discussion Span
Last Post by AppleR

Well, when you set enemy equal to goblin, that's really just an association, not a new object. You can see an explanation of the issue and the use of "copy" to deal with it here. Perhaps it is simpler just to instantiate a new obect.


Well, for this game, the player should be able to fight monsters as much as they want. So i don't think creating an object for EVERY monster they fight will work.

Also, i checked out that link you gave me. I think its what need. Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.