I get the description but not sure how (if at all) this can be done in Python. Below are some alternative ways to achieve what you're looking for.
if / elif / else
The simples solution would be to add a series of if/elif/else cases and call the functions according to how the conditions in the ifs evaluate.
Inheritance
A better solution would be to use inheritance again as we did before.
Create a function in the Locations (base) class, and override it in any derived classes (such as House) where you want to do something specific. An alternative solution is not to create any new function, just override the arrive() function in the derived class and when done implementing something specific, call the super().arrive() function in the end of the overwritten method.
import time
class States:
def __init__(self):
self.stepsTaken = 0
self.visitedHouse = False
class Action:
def __init__(self, description, newLocation):
self.description = description
self.newLocation = newLocation
class Location:
def __init__(self, name, description):
self.name = name
self.description = description
self.actions = []
#
# Generic logic for Location and derived classes
#
def addAction(self, action):
self.actions.append(action)
def arrive(self, states):
self.handleArriveSpecific(states)
states.stepsTaken += 1
print("----------------------------------------")
print('You arrived to: {}'.format(self.name))
print(self.description)
i = 0
print(" (0. Exit game)")
for action in self.actions:
i = i + 1
print(" {}. {}".format(i, action.description))
validChoice = False
while not validChoice:
try:
index = int(input("? : "))
if index == 0:
print("Abandoning your quest... :(")
return
if 0 < index and index <= len(self.actions):
validChoice = True
except ValueError:
pass
self.handleChoices(index - 1, states) # indexing is 0-based, but we get 1-based index from the user
self.actions[index - 1].newLocation.arrive(states)
#
# Virtual functions below. Override them in the derived class to do somethign specific
#
def handleChoices(self, choice, states):
pass # we don't want to do anything specific here, in the base class
def handleArriveSpecific(self, states):
pass # we don't want to do anything specific here, in the base class
class House(Location):
def __init__(self, name, description):
super().__init__(name, description)
def handleArriveSpecific(self, states):
states.visitedHouse = True
def handleChoices(self, choice, states):
#
# Add House specific state changes, if needed. When done, let the super class (base class) handle the rest, i.e. moving to the next location.
#
if choice == 0:
pass # You can add your specific changes for "go to your room" choice
elif choice == 1:
pass # You can add your specific changes for "go to the kitchen" choice
elif choice == 2:
pass # You can add your specific changes for "go to the main room" choice
#
# Declare your locations here
#
yourRoom = Location("your room", "Your room is pretty plain, but at least you have your own. A bed stands in the corner, and a small, crude dresser sits under the window.")
kitchen = Location("the kitchen", "Mom has set out her delicious rolls to cool and has left the room. There are cupboards, a large table, and an oven in the kitchen.")
mainRoom = Location("the main room", "Your father takes a break from his work in the chair, which is the only thing besides a low table that furnishes the room. Of course the fireplace is there too.")
house = House("house", "Inside the house there is a sweet aroma of rolls being baked.")
house.addAction(Action("go to your room", yourRoom))
house.addAction(Action("go to the kitchen", kitchen))
house.addAction(Action("go to the main room", mainRoom))
field = Location("field", "Grass swishes as you walk through it. a solitary horse is grazing in the field.")
stable = Location("stable", "You are greeted by the sound of various animal noises and the smell of various animal odors")
spawnPosition = Location("Spawn position", "You wake up in a sunny morning. After you get out of your bad, you decide to:")
spawnPosition.addAction(Action("go to the house", house))
spawnPosition.addAction(Action("walk the fields", field))
spawnPosition.addAction(Action("visit the stable", stable))
#
# The game starts here
#
print ("Cool Cousins Studios Presents...")
time.sleep(2) # Sleep for 2 seconds
print ("With use of Python Code...")
time.sleep(2) # Sleep for 2 seconds
print ("Adventure In Saldica")
time.sleep(2) # Sleep for 2 seconds
print ("(cue dramatic music)")
time.sleep(3) # Sleep for 3 seconds
states = States()
spawnPosition.arrive(states)
#
# Print some statistics
#
print("Moves in the game: {}".format(states.stepsTaken))
if states.visitedHouse:
print("House visited.")
else:
print("House not visited.")
print("Bye-bye")