Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- class Spell:
- # spell data - filled in by specific spells
- name = "default"
- cooldown_seconds = 0
- last_cast_time = 0
- def cast(self):
- # only cast the spell if it has cooled down
- if self.is_cooled_down():
- # call the behaviour set by the specific spell
- self.spell_specific_behaviour();
- # set the last cast time to the current time
- self.last_cast_time = time.time()
- else:
- print self.name + " has not cooled down yet!"
- def spell_specific_behaviour(self):
- # implement in specific spell subclasses
- return
- def is_cooled_down(self):
- current_time_seconds = time.time()
- cooldown_expire_time_seconds = self.last_cast_time + self.cooldown_seconds
- return current_time_seconds > cooldown_expire_time_seconds
- class FireballSpell(Spell):
- def __init__(self):
- self.name = "fireball"
- self.cooldown_seconds = 2
- def spell_specific_behaviour(self):
- # do whatever you like with fireball
- # this is only called if the spell has cooled down
- print "casting fireball"
- class HealSpell(Spell):
- def __init__(self):
- self.name = "heal"
- self.cooldown_seconds = 5
- def spell_specific_behaviour(self):
- # same applies here as from FireballSpell
- print "casting heal"
- available_spells = [FireballSpell(), HealSpell()]
- equipped = {'Weapon': "Staff",
- 'Armor': "Robes",
- 'Spells': [FireballSpell(), HealSpell()]}
- while True:
- print "Your available spell(s) is(are) '%s'. " % [spell.name for spell in equipped["Spells"]]
- inp = raw_input("Type the name of a spell you want to use.: ").lower()
- lst = [spell for spell in available_spells if spell.name.startswith(inp)]
- if len(lst) == 0:
- print "No such spell"
- print ' '
- elif len(lst) == 1:
- spell = lst[0]
- print "You picked", spell.name
- spell.cast()
- else:
- print "Which spell of", [spell.name for spell in equipped["Spells"]], "do you mean?"
- print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement