Hello All,
I have been trying to run the ex43 code but have been running into obstacles. Can someone review the code and lend me a helping hand. I am new to Python (infact new to programming) and started learning python for a month now, but still struggling with errors.
Here is the code:
from sys import exit
from random import randint
from textwrap import dedent
class Scene(object):
def enter(self):
print("This is scene is not yet configured")
print("Subclass it and implement enter().")
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
print(">>>scene_map in class engine", scene_map)
def play(self):
current_scene = self.scene_map.opening_scene()
print("in play function:", current_scene)
last_scene = self.scene_map.next_scene('finished')
print(">>before entering while: current_scene=", current_scene,"last_scene=", last_scene)
while current_scene != last_scene:
print("current scene:", current_scene)
next_scene_name = current_scene.enter()
print(">>", next_scene_name)
current_scene = self.scene_map.next_scene(next_scene_name)
print("<<play ends here>>")
current_scene.enter()
print("<<>>CS", current_scene)
class Death(Scene):
quips = ["You died. You kind of suck at this.",
"Your mom would be proud>>>>>>>>if she were smarter.",
"Such a loooooooooooser!",
"I have a small puppy that's better at this.",
"You are worse than your Dad's jokes..."]
def enter(self):
print(Death.quips[randint(0,len(self.quips)-1)])
exit(1)
class centralcorridor(Scene):
def enter(self):
print(dedent("""
The Gothons of planet Percal #25 have invaded Your
ship and destroyed your entire crew. You are the
last surviving member and your last mission is to get
the neutron destruct bomb from the weapons armoury, put
it in the bridge, and blow the ship up after getting into
an escape pod.
You are running down the central corridor to the weapons
armoury, when a Gothon jumps out, red, scaly skin,
dark grimy teeth, and evil clown costume flowing around
his hate filled body.
He's blocking the door to the armoury and about to pull
a weapon to blast you."""))
action = input(">>")
if action == "shoot!":
print(dedent("""
Quick on the draw you yank out your blaster and fire it
at the Gothon.
His clown costume is flowing and moving around
his body, which throws off your aim.
Your laser hits his costume but misses him entirely.
This completely ruins his brand new costume his mother
bought him, which makes him fly into an insane rage
and balst you repeatedly in the face until you are
dead. Then he eats you.
"""))
return 'Death'
elif action == "dodge!" :
print(dedent("""
Like a world class boxer you dodge, weave, slip and clide
right through as the Gothon's blaster cranks a laser past
your head. In the middle of your artful dodge your foot slipd
and you bang your head on the metal wall and pass out.
You wake up shortly after only to
die as Gothon stomps on you"""))
return 'Death'
elif action == "tell a joke":
print(dedent("""
Lucky for you they made you learn Gothon insults in the academy
You tell the one Gothon joke you know:
ldhe zugrekvsd v d vbisdf
jump through the weapon armoury door"""))
return 'Weapon_Armoury'
else:
print("DOES NOT COMPUTE!")
return 'centralcorridor'
class Weapon_Armoury(Scene):
def enter(self):
print(dedent("""
Do a dive roll into the weapon armoury....
......crouch and scan the room."""))
code = f"{randint(1,9)} {randint(1,9)} {randint(1,9)}"
guess = input("[keypad]>")
guesses = 0
while guess != code and guesses < 10:
print("BZZZZZED!")
guesses +=1
guess = input("[keypad]>")
if guess == code:
print(dedent("""Container clicks open....run to the bridge."""))
return 'Bridge'
else:
print(dedent("""
Gotohons blow up the ship from their ship
....................BBYEEEE!"""))
return 'Death'
class Bridge(Scene):
def enter(self):
print(dedent("""
burst onto bridgen and set the bomb off"""))
action = input(">")
if action == "throw the bomb":
print(dedent("""PANIC and Die"""))
return 'Death'
elif action == "Slowly place the bomb":
print(dedent(""" Move slowly, activate bomb and jump"""))
return 'pod'
class pod(Scene):
def enter(self):
print(dedent("""
there are five pods
which one do you choose?"""))
good_pod = randint(1,5)
guess = input("[pod]>")
if int(guess) != good_pod:
print(dedent("""
crush your body into a jelly"""))
return 'Death'
else:
print(dedent("""
You WON!!!"""))
return 'finished'
class finished(Scene):
def enter(self):
print("you Won! Good Job")
return 'finished'
class Map(object):
scenes = {
'central_corridor':centralcorridor(),
'laser_weapon_armoury':Weapon_Armoury(),
'the_bridge':Bridge(),
'escape_pod':pod(),
'death':Death(),
'finish':finished(),
}
print("*****central_corridor is:**********",scenes['central_corridor'])
def __init__(self, start_scene):
self.start_scene = start_scene
print("Map & start scene:", start_scene)
def next_scene(self, scene_name):
print(">>before val//////:", scene_name)
val = scene_name
print("val in Map:", val)
return val
def opening_scene(self):
x = self.next_scene(self.start_scene)
print(">>>x", x)
return x
a_map = Map(scenes['central_corridor'])
print("map", a_map.scenes)
a_game = Engine(a_map)
print("a_game", a_game)
a_game.play()
print("\n************\n", a_game.play())
and here’s the error which i get----
PS C:\Users\Karthik\zpyexercises> python zx43.py *****central_corridor is:********** <__main__.centralcorridor object at 0x03337340>
Map & start scene: central_corridor
map {'central_corridor': <__main__.centralcorridor object at 0x03337340>, 'laser_weapon_armoury': <__main__.Weapon_Armoury object at 0x03337358>, 'the_bridge': <__main__.Bridge object at 0x03337370>, 'escape_pod': <__main__.pod object at 0x03337388>, 'death': <__main__.Death object at 0x0348EF10>, 'finish': <__main__.finished object at 0x0345B628>}
>>>scene_map in class engine <__main__.Map object at 0x0345B610>
a_game <__main__.Engine object at 0x0345B160>
>>before val//////: central_corridor
val in Map: central_corridor
>>>x central_corridor
in play function: central_corridor
>>before val//////: finished
val in Map: finished
>>before entering while: current_scene= central_corridor last_scene= finished
current scene: central_corridor
Traceback (most recent call last):
File "zx43.py", line 162, in <module>
a_game.play()
File "zx43.py", line 21, in play
next_scene_name = current_scene.enter()
AttributeError: 'str' object has no attribute 'enter'
Cheers,
Orear