views:

67

answers:

4

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.")
A: 

Probably you want to do something like this:

class Player:

   def __init__(self, ammo=5):
      self.ammo=ammo

   def blast(self, enemy):
      if self.ammo>0:
         self.ammo-=1
         enemy.die(5)
      else:
         print "U are out of ammunition!"

You need to use self.health for the alien as well.

zoli2k
+6  A: 

Think about it: the amount of ammunition available is part of a player's state. An object's state is best represented as instance variables of that object. So you should not have ammo as an argument to blast -- it should be self.ammo in that method, initialized to 5 or whatever in the __init__ you forgot to code;-).

It's not a matter of seeking for fancy workarounds to hide and stash that state somewhere else -- it's a matter of doing things in the simplest, most straightforward, most effective way. Why would you ever want anything but such a way?!

Alex Martelli
+2  A: 

You need to keep track of the alien's health. All you're doing now is decrementing the "health" local variable in the Alien.die function.

Here's a little snippet that should help get you going in the right direction:

class Alien(object):
    def __init__(self):
        self.health = 5
    def do_damage(self, amount):
        self.health -= amount

Similar tracking required for player's ammo.

dkamins
A: 

In class Player in the method named blast() your references to to ammo are incorrect. You want to refer to self.ammo in all cases within that function/method definition.

Furthermore you should probably (almost certainly) define an __init__ method to set the starting ammunition value to 5 (and perhaps later provide a reload() method to set self.ammo back up to 5 ... and so on.

Jim Dennis