Hello,
ok, here is the updated version. I have simplified it greatly as I have removed the Map class
since it was merged with the Engine class
. By combining these two classes and making use of the global variable Location.next_location
, there is no need to make function calls via instance variables and nested function calls via inheritance. The Location.next_locati on
serves as the key
for the scenes
dictionary. Thus, the Location.next_location
determines the current method being executed.
I have also updated other areas of the script whereby I removed unnecessary self
object instance variables in the cases of static methods
. Please reference the material on the matter so that you understand the theory behind it.
from sys import exit
from random import randint
from textwrap import dedent
class Scene:
@staticmethod
def enter():
print("This scene is not yet configured.")
print("Subclass it and implement enter().")
exit(1)
class Riddle:
riddle = ' '
class Riddles(Riddle):
@staticmethod
def first_riddle():
Riddle.riddle = "What is always on its way, but never arrives?"
@staticmethod
def second_riddle():
Riddle.riddle = "What thrives when you feed it, but dies when you water it?"
@staticmethod
def third_riddle():
Riddle.riddle = "What do you hold but never keep? If you take your last, make it deep."
@staticmethod
def fourth_riddle():
Riddle.riddle = "I don't breathe flames, but I light the night, guiding sailors with my steady sight. I stand tall on cliffs, where dragons might dwell, but I save lives with every bell. What am I?"
@staticmethod
def fifth_riddle():
Riddle.riddle = "They come out at night without being asked yet lost in the day without being stolen. What are they?"
class Location:
death_location = ' '
next_location = ' '
class Death(Location):
@staticmethod
def enter():
if Location.death_location in ('start', 'dark_woods', 'outside_house_without_bone', 'outside_house_with_bone'):
print("Day becomes night and you're devoured by wolves.")
elif Location.death_location in ('foyer', 'living_room'):
print("The dog chews your leg off and you bleed to death.")
elif Location.death_location == 'cellar':
print("The dragon burns you to a crisp.")
else:
print("You are turned into a rodent and eaten by an owl.")
print("You're dead! Better luck next time!")
exit(0)
class Start(Location):
@staticmethod
def enter():
Location.death_location = 'start'
print(dedent("""
Welcome to Arcamadius, where magic and creatures exist.
You come to a house in the dark woods.
There is a window on the east wall and a door on the
South wall. The house is surrounded by woods. What
would you like to do?
"""))
action = input("> (open window, open door) ")
if action == 'open window':
print(dedent("""
You open the window. It makes a squeaking noise.
What do you want to do now?
"""))
action = input("> (climb into window) ")
if action == 'climb into window':
print("You enter the window into the kitchen.")
Location.next_location = 'kitchen'
else:
print("You just stand there!")
Location.next_location = 'death'
elif action == 'open door':
print("You enter the door into the Foyer")
Location.next_location = 'foyer'
else:
Location.next_location = 'death'
class OutsideHouseWithoutBone(Location):
@staticmethod
def enter():
Location.death_location = 'outside_house_without_bone'
have_bone = False
print(dedent("""
You are at a house in the dark woods.
There is a window on the east wall and a door on the
South wall. The house is surrounded by woods. What
would you like to do?
"""))
action = input("> (open window, open door) ")
if action == 'open window':
print(dedent("""
You open the window. It makes a squeaking noise.
What do you wnat to do now?
"""))
action = input("> (climb into window) ")
if action == 'climb into window':
print("You enter the window into the kitchen.")
Location.next_location = 'kitchen'
else:
print("You just stand there!")
Location.next_location = 'death'
elif action == 'open door':
print("You enter the door into the Foyer")
Location.next_location = 'foyer'
else:
Location.next_location = 'death'
class OutsideHouseWithBone(Location):
@staticmethod
def enter():
Location.death_location = 'outside_house_with_bone'
have_bone = True
print(dedent("""
You are at a house in the dark woods.
There is an open window on the east wall and a door on the
South wall. The house is surrounded by woods. You are
carrying a bone. What would you like to do?
"""))
action = input("> (climb into window, open door) ")
if action == 'climb into window':
print("You enter the window into the kitchen.")
Location.next_location = 'kitchen_with_bone'
elif action == 'open door':
print("You enter the door into the Foyer")
Location.next_location = 'foyer_with_bone'
else:
Location.next_location = 'death'
class Kitchen(Location):
@staticmethod
def enter():
have_bone = False
print(dedent("""
There is a doorway ahead. There is also a table
with a bone on it. What do you wnat to do?
"""))
action = input("> (go through doorway, grab bone, climb out window) ")
if action == "go through doorway" and have_bone == True:
Location.next_location = 'living_room'
elif action == "grab bone" and have_bone == False:
print("You grab the bone")
Location.next_location = 'kitchen_with_bone'
elif action == "climb out window":
Location.next_location = 'outside_house_without_bone'
else:
print("I'm not sure what you are saying.")
# self.enter(self)
class KitchenWithBone(Location):
@staticmethod
def enter():
Location.location = 'kitchen_with_bone'
have_bone = True
print(dedent("""
There is a doorway ahead. There is also an empty
table. What do you wnat to do?
"""))
action = input("> (go through doorway, climb out window)")
if action == "go through doorway" and have_bone == True:
Location.next_location = 'living_room'
elif action == "climb out window":
Location.next_location = 'outside_house_with_bone'
else:
print("I'm not sure what you are saying.")
# self.enter(self)
class Foyer(Location):
@staticmethod
def enter():
Location.death_location = 'foyer'
have_bone = False
print(dedent("""
A huge dog is staring and growling at you
What do you do?
"""))
action = input("> (flee, give dog the bone) ")
if action == "flee":
print(dedent("""
You managed to get away from the dog chasing you and the
dog goes back into the house.
"""))
Location.next_location = 'outside_house_without_bone'
elif action == "give dog the bone" and have_bone == False:
print("you have no bone.")
Location.next_location = 'death'
else:
print("I have no idea what that means.")
Location.next_location = 'foyer'
class FoyerWithBone(Location):
@staticmethod
def enter():
have_bone = True
print(dedent("""
A huge dog is staring and growling at you and you are
carrying a bone. What do you do?
"""))
action = input("> (flee, give dog the bone) ")
if action == "flee":
print(dedent("""
You managed to get away from the dog chasing you and the
dog goes back into the house.
"""))
Location.next_location = 'outside_house_with_bone'
elif action == "give dog the bone" and have_bone == True:
print(dedent("""
You give the dog the bone.
The dog wants to play. You play with the dog for awhile.
Now back to business. The entrance to the living room
is ahead.
"""))
Location.next_location = 'living_room'
else:
print("I have no idea what that means.")
Location.next_location = 'foyer_with_bone'
class LivingRoom(Location):
@staticmethod
def enter():
Location.death_location = 'living_room'
have_bone = True
print(dedent("""
You are in the living room. There is a trap door in the floor.
There is a mantle with an electric lantern on it. A huge dog
is staring and growling at you. You are carrying a bone. What
would you like to do?
"""))
action = input("> (give dog the bone) ")
if action == "give dog the bone" and have_bone == True:
print(dedent("""
You give the dog the bone.
The dog wants to play. You play with the dog for awhile.
The dog goes asleep. You are in the living room. There is
a trap door in the floor. There is a mantle with an electric
lantern on it. What would you like to do?
"""))
action = input("> ( grab lantern)")
if action == "grab lantern":
print(dedent("""
You grab the lantern and turn it on. You are in the
living room. There is a trap door in the floor.
There is an empty mantle. You open the trap door.
"""))
Location.next_location = 'cellar'
else:
print(dedent("""
The dog wakes up from its slumber and is hungry. It
growls and snarls at you.
"""))
Location.next_location = 'death'
else:
Location.next_location = 'death'
class DarkWoods(Location):
@staticmethod
def enter():
pass
class Cellar(Location):
@staticmethod
def enter():
Location.death_location = 'cellar'
print(dedent("""
You open the open the door to the cellar and a foul
stench is coming from the darkness. You head into
the cellar. In the corner, you see a dragon laying
on a bed of gold. On top of the gold is a nest and
in the nest is five dragon eggs What would you like
to do?
"""))
action = input("> (grab some gold, grab an egg, approach dragon) ")
if action == "grab some gold":
print(dedent("""
The dragon awakens in a fury and says, 'How dare
thee take my gold! For that you will perish!'
"""))
Location.next_location = 'death'
elif action == "grab an egg":
print(dedent("""
The dragon awakens in a fury and says, 'How dare
thee take one of my eggs! For that you will perish!'
"""))
Location.next_location = 'death'
elif action == "approach dragon":
print(dedent("""
You approach the dragon and ask the dragon, 'How may I
obtain an egg from you?' The dragon says, '
You must first answer a riddle. Then you will get an egg'
"""))
Location.next_location = 'cellar'
else:
print("I'm not sure what you mean.")
Location.next_location = 'cellar'
riddle_number = f"{randint(1, 5)}"
guesses = 1
if riddle_number == 1:
print(Riddles.first_riddle())
guess = input("[your answer]> ")
answer = "Tomorrow"
elif riddle_number == 2:
print(Riddles.second_riddle())
guess = input("[your answer]> ")
answer = "Fire"
elif riddle_number == 3:
print(Riddles.third_riddle())
guess = input("[your answer]> ")
answer = "A breath of air"
elif riddle_number == 4:
print(Riddles.fourth_riddle())
guess = input("[your answer]> ")
answer = "A lighthouse"
else:
print(Riddles.fifth_riddle())
guess = input("[your answer]> ")
answer = "Stars"
while guess != answer and guesses < 3:
print("Try again!")
guesses += 1
guess = input("[your answer]> ")
if guess == answer:
print(dedent("""
You have answered correctly. You carefully chooose
an egg. The dragon speaks some magic words and you
are teleported to the Dark Woods.
"""))
Location.next_location = 'dark_woods'
class WitchHouse(Location):
@staticmethod
def enter():
pass
class Finished(Location):
@staticmethod
def enter():
pass
class Engine(Location):
# Function dictionary
scenes = {
'start': Start(),
'outside_house_without_bone': OutsideHouseWithoutBone(),
'outside_house_with_bone': OutsideHouseWithBone(),
'kitchen': Kitchen(),
'kitchen_with_bone': KitchenWithBone(),
'foyer': Foyer(),
"foyer_with_bone": FoyerWithBone(),
'living_room': LivingRoom(),
'dark_woods': DarkWoods(),
'cellar': Cellar(),
'witch_house': WitchHouse(),
'death': Death(),
'finished': Finished(),
}
@staticmethod
def play(start):
Location.next_location = start
while True: # Start game
Engine.scenes[Location.next_location].enter()
try:
Engine.play('start')
except KeyboardInterrupt:
pass
For the print menus, consider the option of giving the user the option of just requiring them to enter a number
or a letter
as it will greatly simplify the user experience. There is prone to be typos. If that is the case, there is potential for unknown unintended consequences and will require you to add additional layers of code to catch typos.
This is an example of the current version:
action = input("> (open window, open door) ")
Here is an alternative. Notice how it is much easier to deal with from a user point of view. No typing required and less potential for typos.
action = input('\nPlayer options:'
'\n--------------'
'\n1. Open window? Enter "1"'
'\n2. Open door? Enter "2"')
Good luck!