Created
April 15, 2020 09:23
-
-
Save johnifegwu/e26723b3fa61c248bcdd245cd047a173 to your computer and use it in GitHub Desktop.
Simple Game (Quench COVID19)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GameObject: | |
class_name = "" | |
desc = "" | |
objects = {} | |
def __init__(self, name): | |
self.name = name | |
GameObject.objects[self.class_name] = self | |
def get_desc(self): | |
return self.class_name + "\n" + self.desc | |
class Coronavirus(GameObject): | |
def __init__(self, name): | |
self.class_name = "covid19" | |
self.health = 3 | |
self._desc = " A deadly virus that must be quenched!" | |
super().__init__(name) | |
@property | |
def desc(self): | |
if self.health >=3: | |
return self._desc | |
elif self.health == 2: | |
health_line = "It has a wound on its knee." | |
elif self.health == 1: | |
health_line = "Its left arm has been cut off!" | |
elif self.health <= 0: | |
health_line = "COVID-19 is dead." | |
str = "\n" + health_line | |
if self.health > 0: | |
str += " \n \n Type \"hit Covid19\" to hit this menace! \n" | |
return str | |
@desc.setter | |
def desc(self, value): | |
self._desc = value | |
covid19 = Coronavirus("covid19") | |
def examine(noun): | |
if noun in GameObject.objects: | |
return GameObject.objects[noun].get_desc() | |
else: | |
return "There is no {} here.".format(noun) | |
def hit(noun): | |
if noun in GameObject.objects: | |
thing = GameObject.objects[noun] | |
if type(thing) == Coronavirus: | |
thing.health = thing.health - 1 | |
if thing.health <= 0: | |
msg = "\n You killed Coronavirus!" | |
else: | |
msg = "\n You hit {}".format(thing.class_name) | |
else: | |
msg ="There is no {} here.".format(noun) | |
return msg | |
def get_input(): | |
command = input(": ").split() | |
verb_word = command[0] | |
if verb_word in verb_dict: | |
verb = verb_dict[verb_word] | |
else: | |
print("Unknown verb {}". format(verb_word)) | |
return | |
if len(command) >= 2: | |
noun_word = command[1] | |
print (verb(noun_word)) | |
print("") | |
print(examine(noun_word)) | |
else: | |
print(verb("nothing")) | |
def say(noun): | |
return 'You said "{}"'.format(noun) | |
verb_dict = { | |
"say": say, | |
"examine": examine, | |
"hit": hit, | |
} | |
print("Enter \"hit Covid19\" to help me Quench this Devil! \n") | |
while True: | |
get_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment