Advertisement
Guest User

stock price object.py

a guest
Feb 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. class GameObject:
  2.     class_name = ""
  3.     desc = ""
  4.     objects = {}
  5.  
  6.     def __init__(self, name):
  7.         self.name = name
  8.         GameObject.objects[self.class_name] = self
  9.  
  10.     def get_desc(self):
  11.         return self.class_name + "\n" + self.desc
  12.  
  13.  
  14. class Goblin(GameObject):
  15.     def __init__(self, name):
  16.         self.class_name = "goblin"
  17.         self.health = 3
  18.         self._desc = "A foul creature"
  19.         super().__init__(name)
  20.  
  21.     @property
  22.     def desc(self):
  23.         if self.health >= 3:
  24.             return self._desc
  25.         elif self.health == 2:
  26.             health_line = "It has a wound on its knee."
  27.         elif self.health == 1:
  28.             health_line = "Its left arm has been cut off!"
  29.         elif self.health <= 0:
  30.             health_line = "It is dead."
  31.         return self._desc + "\n" + health_line
  32.  
  33.     @desc.setter
  34.     def desc(self, value):
  35.         self._desc = value
  36.  
  37.  
  38. goblin = Goblin("Gobbly")
  39.  
  40.  
  41. def hit(noun):
  42.     if noun in GameObject.objects:
  43.         thing = GameObject.objects[noun]
  44.         if type(thing) == Goblin:
  45.             thing.health -= 1
  46.             if thing.health <= 0:
  47.                 msg = "You killed the goblin!"
  48.             else:
  49.                 msg = "You hit the {}".format(thing.class_name)
  50.     else:
  51.         msg = "There is no {} here.".format(noun)
  52.     return msg
  53.  
  54. class Elf(GameObject):
  55.     def __init__(self, name):
  56.         self.class_name = "elf"
  57.         self.health = 3
  58.         self._desc = "A noble creature"
  59.         super().__init__(name)
  60.  
  61.     @property
  62.     def desc(self):
  63.         if self.health >= 3:
  64.             return self._desc
  65.         elif self.health == 2:
  66.             health_line = "It cut off your knee."
  67.         elif self.health == 1:
  68.             health_line = "It cut off your arms!"
  69.         elif self.health <= 0:
  70.             health_line = "It killed your mum."
  71.         return self._desc + "\n" + health_line
  72.  
  73.     @desc.setter
  74.     def desc(self, value):
  75.         self._desc = value
  76.  
  77.  
  78. elf = Elf("Elfy")
  79.  
  80.  
  81. def hit(noun):
  82.     if noun in GameObject.objects:
  83.         thing = GameObject.objects[noun]
  84.         if type(thing) == Elf:
  85.             thing.health -= 1
  86.             if thing.health <= 0:
  87.                 msg = "The elf killed your mum!"
  88.             else:
  89.                 msg = "The {} hit you back".format(thing.class_name)
  90.     else:
  91.         msg = "There is no {} here.".format(noun)
  92.     return msg
  93.  
  94.  
  95. def examine(noun):
  96.     if noun in GameObject.objects:
  97.         return GameObject.objects[noun].get_desc()
  98.     else:
  99.         return "There is no {} here.".format(noun)
  100.  
  101.  
  102. def get_input():
  103.     command = input(": ").split()
  104.     verb_word = command[0]
  105.     if verb_word in verb_dict:
  106.         verb = verb_dict[verb_word]
  107.     else:
  108.         print("Unknown verb {}".format(verb_word))
  109.         return
  110.  
  111.     if len(command) >= 2:
  112.         noun_word = command[1]
  113.         print(verb(noun_word))
  114.     else:
  115.         print(verb("nothing"))
  116.  
  117.  
  118. def say(noun):
  119.     return 'You said "{}"'.format(noun)
  120.  
  121.  
  122. verb_dict = {
  123.     "say": say,
  124.     "examine": examine,
  125.     "hit": hit
  126. }
  127.  
  128. while True:
  129.     get_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement