proffreda

A Zombie Class

Mar 17th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. class Zombie:
  2.     def __init__(self,h,s,p):
  3.         self.health = h
  4.         self.size = s
  5.         self.position = p
  6.  
  7.     def __str__(self):
  8.         return("Zombie has health: " + str(self.health) +
  9.                         ", size: " + str(self.size)  +
  10.                         ", position: " + str(self.position))
  11.    
  12.     def __lshift__(self, other):
  13.         '''returns an offspring that has health and size that averages the parents '''
  14.         h = (self.health + other.health) / 2
  15.         s = (self.size + other.size) /2
  16.         p = self.position
  17.         return Zombie(int(h),int(s),p)
  18.  
  19.     def __xor__(self, other):
  20.         ''' returns T/F whether 2 zombies share same position '''
  21.         return( self.position == other.position)
  22.        
  23. z1=Zombie(100,250,0)
  24. z2=Zombie(10,200,0)
  25. if z1^z2 :
  26.     offspring = (z1 << z2)
  27.     print("New Zombie: " + str(offspring))
Advertisement
Add Comment
Please, Sign In to add comment