Commit e6b8147f authored by Adrian Buchholz's avatar Adrian Buchholz

added game and properties file for OOP sololearn

parent 6f7265a2
#Set Attribute Error | Commented out to allow code to run
'''
class Airplane:
def __init__(self, color):
self.color = color
@property
def white(self):
return False
airplane = Airplane(["black", "white"])
print(airplane.white)
airplane.white = True
'''
#Messing Arround with setters
class Airplane:
def __init__(self, color):
self.color = color
self._red_allowed = False
@property
def red_allowed(self):
return self._red_allowed
@red_allowed.setter
def red_allowed(self, value):
if value:
password = input("Enter the password: ")
if password == "1234":
self._red_allowed = value
else:
raise ValueError("Alert! Intruder!")
airplane = Airplane(["white", "blue"])
print(airplane.red_allowed)
airplane.red_allowed = True
print(airplane.red_allowed)
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 Goblin(GameObject):
def __init__(self, name):
self.class_name = "goblin"
self.health = 3
self._desc = "A foul creature"
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 = "It is dead."
return self._desc + "\n" + health_line
@desc.setter
def desc(self, value):
self._desc = value
goblin = Goblin("Gobbly")
#HIT COMMAND
def hit(noun):
if noun in GameObject.objects:
thing = GameObject.objects[noun]
if type(thing) == Goblin:
thing.health -= 1
if thing.health <= 0:
msg = "You killed the goblin!"
else:
msg = "You hit the {}".format(thing.class_name)
else:
msg = "There is no {} here.".format(noun)
return msg
#EXAMINE COMMAND
def examine(noun):
if noun in GameObject.objects:
return GameObject.objects[noun].get_desc()
else:
return "There is no {} here.".format(noun)
#SAY COMMAND
def say(noun):
return 'You said "{}"'.format(noun)
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))
else:
print(verb("nothing"))
verb_dict = {
"say": say,
"examine": examine,
"hit": hit
}
while True:
get_input()
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment