Advertisement
Guest User

Untitled

a guest
Oct 27th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. import time
  2.  
  3. class Spell:
  4.        
  5.     # spell data - filled in by specific spells
  6.     name = "default"
  7.     cooldown_seconds = 0
  8.     last_cast_time = 0
  9.    
  10.     def cast(self):
  11.         # only cast the spell if it has cooled down
  12.         if self.is_cooled_down():
  13.             # call the behaviour set by the specific spell
  14.             self.spell_specific_behaviour();
  15.             # set the last cast time to the current time
  16.             self.last_cast_time = time.time()
  17.         else:
  18.             print self.name + " has not cooled down yet!"
  19.    
  20.     def spell_specific_behaviour(self):
  21.         # implement in specific spell subclasses
  22.         return
  23.    
  24.     def is_cooled_down(self):
  25.         current_time_seconds = time.time()
  26.         cooldown_expire_time_seconds = self.last_cast_time + self.cooldown_seconds
  27.    
  28.         return current_time_seconds > cooldown_expire_time_seconds
  29.    
  30.  
  31. class FireballSpell(Spell):
  32.        
  33.     def __init__(self):
  34.         self.name = "fireball"
  35.         self.cooldown_seconds = 2
  36.    
  37.     def spell_specific_behaviour(self):
  38.         # do whatever you like with fireball
  39.         # this is only called if the spell has cooled down
  40.         print "casting fireball"
  41.            
  42.    
  43. class HealSpell(Spell):
  44.        
  45.     def __init__(self):
  46.         self.name = "heal"
  47.         self.cooldown_seconds = 5
  48.    
  49.     def spell_specific_behaviour(self):
  50.         # same applies here as from FireballSpell
  51.         print "casting heal"
  52.  
  53.  
  54. available_spells = [FireballSpell(), HealSpell()]
  55. equipped = {'Weapon': "Staff",
  56.            'Armor': "Robes",
  57.            'Spells': [FireballSpell(), HealSpell()]}
  58.  
  59. while True:
  60.  
  61.     print "Your available spell(s) is(are) '%s'. " % [spell.name for spell in equipped["Spells"]]
  62.     inp = raw_input("Type the name of a spell you want to use.: ").lower()
  63.     lst = [spell for spell in available_spells if spell.name.startswith(inp)]
  64.     if len(lst) == 0:
  65.         print "No such spell"
  66.         print ' '
  67.     elif len(lst) == 1:
  68.         spell = lst[0]
  69.         print "You picked", spell.name
  70.         spell.cast()
  71.     else:
  72.         print "Which spell of", [spell.name for spell in equipped["Spells"]], "do you mean?"
  73.  
  74.     print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement