T
Thomas Philips
I'm teaching myself programming using Python, and have a question
about subclasses. My game has two classes, Player and Alien, with
identical functions, and I want to make Player a base class and Alien
a derived class. The two classes are described below
class Player(object):
#Class attributes for class Player
threshold = 50
n=0 #n is the number of players
#Private methods for class Player
def __init__(self,name):
self.name = name
self.strength = 100
Player.n +=1
def __del__(self):
Player.n -=1
print "You got me, Alien"
#Public methods for class Player
def blast(self,enemy,energy):
enemy.hit(energy)
def hit(self,energy):
self.strength -= energy
if(self.strength <= Player.threshold):
self.__del__()
class Alien(Player):
#Class attributes for class Alien
threshold = 100
n=0 #n is the number of players
#Private methods for class Alien
def __init__(self,name):
self.name = name
self.strength = 100
Alien.n +=1
def __del__(self):
Alien.n -=1
print "You got me, earthling"
#Public methods for class Alien
def hit(self,energy):
self.strength -= energy
if(self.strength <= Alien.threshold):
self.__del__()
The two classes are almost identical, except that:
1. When a new player is instantiated or destroyed, Player.n is
incremented/decremented, while when a new alien is instantiated,
Alien.n is incremented/decremented.
2. When hit by an energy blast, the player and the alien have
different thresholds below which they die.
How can I base the Alien's __init__(), __del__() and hit() methods on
the Player's methods, while ensuring that the appropriate class
variables are incremented/decremented when a new object is
instantiated and that the appropriate threshold is used when the
player/alien is hit by an energy bolt?
Thomas Philips
about subclasses. My game has two classes, Player and Alien, with
identical functions, and I want to make Player a base class and Alien
a derived class. The two classes are described below
class Player(object):
#Class attributes for class Player
threshold = 50
n=0 #n is the number of players
#Private methods for class Player
def __init__(self,name):
self.name = name
self.strength = 100
Player.n +=1
def __del__(self):
Player.n -=1
print "You got me, Alien"
#Public methods for class Player
def blast(self,enemy,energy):
enemy.hit(energy)
def hit(self,energy):
self.strength -= energy
if(self.strength <= Player.threshold):
self.__del__()
class Alien(Player):
#Class attributes for class Alien
threshold = 100
n=0 #n is the number of players
#Private methods for class Alien
def __init__(self,name):
self.name = name
self.strength = 100
Alien.n +=1
def __del__(self):
Alien.n -=1
print "You got me, earthling"
#Public methods for class Alien
def hit(self,energy):
self.strength -= energy
if(self.strength <= Alien.threshold):
self.__del__()
The two classes are almost identical, except that:
1. When a new player is instantiated or destroyed, Player.n is
incremented/decremented, while when a new alien is instantiated,
Alien.n is incremented/decremented.
2. When hit by an energy blast, the player and the alien have
different thresholds below which they die.
How can I base the Alien's __init__(), __del__() and hit() methods on
the Player's methods, while ensuring that the appropriate class
variables are incremented/decremented when a new object is
instantiated and that the appropriate threshold is used when the
player/alien is hit by an energy bolt?
Thomas Philips