Advertisement
homer512

health

Jun 2nd, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3.  
  4. import datetime
  5. import time
  6.  
  7.  
  8. HEAL_SECONDS = 2
  9.  
  10.  
  11. class Health(object):
  12.     def __init__(self, initial=10):
  13.         self.hearts = initial
  14.         if initial < 10:
  15.             self.lasthealing = datetime.datetime.utcnow()
  16.         else:
  17.             self.lasthealing = None
  18.  
  19.     def monitor(self):
  20.         if self.hearts < 10:
  21.             if self.lasthealing:
  22.                 # calculate how much time has passed, how much there is to heal
  23.                 # then update the time info
  24.                 now = datetime.datetime.utcnow()
  25.                 spendtime = now - self.lasthealing
  26.                 healing = spendtime.seconds / HEAL_SECONDS
  27.                 maxhealing = 10 - self.hearts
  28.                 realhealing = min(healing, maxhealing)
  29.                 for _ in xrange(realhealing):
  30.                     print "+1 heart :D"
  31.                 self.hearts += realhealing
  32.                 if self.hearts < 10:
  33.                     healupdate = healing * HEAL_SECONDS
  34.                     self.lasthealing += datetime.timedelta(seconds=healupdate)
  35.                 else:
  36.                     self.lasthealing = None
  37.             else:
  38.                 # apparently we just lost some hearts
  39.                 self.lasthealing = datetime.datetime.utcnow()
  40.             if self.hearts < 1:
  41.                 # we check this in the end so that healings that occured
  42.                 # previously are properly accounted for
  43.                 print "You are dead"
  44.         else:
  45.             self.lasthealing = None
  46.  
  47.     def __iadd__(self, amount):
  48.         self.monitor()
  49.         self.hearts += amount
  50.         print "+{} hearts! :D".format(amount)
  51.         self.monitor()
  52.         return self
  53.  
  54.     def __isub__(self, amount):
  55.         self.monitor()
  56.         self.hearts -= amount
  57.         print "-{} hearts! :'(".format(amount)
  58.         self.monitor()
  59.         return self
  60.  
  61.     @property
  62.     def dead(self):
  63.         return self.hearts < 1
  64.  
  65.  
  66. def main():
  67.     me = Health()
  68.     me.monitor()
  69.     me -= 2
  70.     assert(me.hearts == 8)
  71.     time.sleep(2)
  72.     me.monitor()
  73.     assert(me.hearts == 9)
  74.     time.sleep(4)
  75.     me.monitor()
  76.     assert(me.hearts == 10)
  77.     me -= 6
  78.     time.sleep(4)
  79.     me -= 5
  80.     assert(me.hearts == 1)
  81.     me -= 1
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement