Simple way to decrease values without making a new attribute?

Posted by Jam on Stack Overflow See other posts from Stack Overflow or by Jam
Published on 2010-04-06T05:37:29Z Indexed on 2010/04/06 5:43 UTC
Read the original article Hit count: 502

I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this, or do I just have to make a new attribute for ammo and health? Here's what I have:

class Player(object):
""" A player in a shooter game. """
def blast(self, enemy, ammo=5):
    if ammo>=1:
        ammo-=1
        print "You have blasted the alien."
        print "You have", ammo, "ammunition left."
        enemy.die(5)
    else:
        print "You are out of ammunition!"


class Alien(object):
    """ An alien in a shooter game. """
    def die(self, health=5):
        if health>=1:
            health-=1
            print "The alien is wounded. He now has", health, "health left."
        elif health==0:
            health-=1
            print "The alien gasps and says, 'Oh, this is it.  This is the big one. \n" \
                  "Yes, it's getting dark now.  Tell my 1.6 million larvae that I loved them... \n" \
                  "Good-bye, cruel universe.'"
        else:
            print "The alien's corpse sits up momentarily and says, 'No need to blast me, I'm dead already!"

# main
print "\t\tDeath of an Alien\n"

hero = Player()
invader = Alien()
hero.blast(invader)
hero.blast(invader)
hero.blast(invader)
hero.blast(invader)
hero.blast(invader)
hero.blast(invader)

raw_input("\n\nPress the enter key to exit.")

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner