学得小套路后,贵在用不同的程序去模仿和应用。
这是我模仿的丑小鸭版。
from sys import exit
from random import randint
from textwrap import dedent
# set class Scene
class Scene(object):
def enter(self):
print("There is nothing.")
exit(1)
# the transfer of scenes
class Engine(object):
def __init__(self,scene_map):
self.scene_map = scene_map
# set the current_scene and the next scene,
# when the current_scene isn't equal to the last scene, the current scene is equal to the next_scene.
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
class Haystack(Scene):
def enter(self):
print(dedent(""" You are the ugly ducking. Your mummy duck had 7 eggs at haystack. Do you want to break out? """))
action = input("out or no? > ")
if action =="no":
return 'death'
elif action == "out":
print("Congratulations! You come out, and you are big ugly ducklings.")
return 'farm'
else:
print("Please input your choice.")
return 'haystack'
class Farm(Scene):
def enter(self):
print(dedent(""" When they go to the farm, the animals mock at the ugly duckling and let you go away. What do you want? """))
action = input("Go away or beat back? > ")
if action == "beat back":
print("The animals are angry, the they hit you.")
return 'death'
elif action == "go away":
print("You are very sad and away from the farm.")
return 'hole'
else:
print("Please input your choice.")
return 'farm'
class Hole(Scene):
def enter(self):
print(dedent(""" You find a hole,but there is door. You should guess the right number. """))
code = f"{randint(1,3)}{randint(1,3)}{randint(1,3)}"
print(code)
action = input("The code is : > ")
guess = 1
while action != code and guess < 3:
print("Please try again!")
action = input("The code is : > ")
guess +=1
if action == code:
print("The door is open, you can survive winter until spring.")
return 'river'
elif action != code:
print("Sorry, you are frozen to death.")
return 'death'
else:
print("Please input your choice.")
return 'hole'
class River(Scene):
def enter(self):
print(dedent(""" In spring, the ugly duckling went to the river, and a swan told that it is swan. Do you believe it? """))
action = input("Believe or not? >")
if action == "believe":
print("It is truth. You are swan, you are beautiful, and you are so happy.")
return 'finished'
elif action == "not":
print("Idiot! You miss a good chance, and you are so sad to death.")
return 'death'
else:
print("Please input your choice.")
return 'river'
class Death(Scene):
def enter(self):
death_way = ['You are an idiot',
'I have to say you should try again',
'You almost win',
'Try again, you will win'
]
print(f"{death_way[randint(0,len(death_way)-1)]}")
exit(1)
class Finished(Scene):
def enter(self):
print("You are the winner.")
# 建立地图辞典,以及各个地图之间的关系。
class Map(object):
map_dict = {
"haystack": Haystack(),
"farm": Farm(),
"hole": Hole(),
"river": River(),
"death": Death(),
"finished": Finished()
}
def __init__(self, start_scene):
self.start_scene = start_scene
# get the value of dict, the value is the name of class.
# the scene_name is the key of map_dict.
def next_scene(self, scene_name):
val = Map.map_dict.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
opening_map = Map("haystack")
player = Engine(opening_map)
player.play()
# The story
# You are the ugly ducking. The mummy duck had 7 eggs at haystack.
# The gray egg cracked and the ugly duckling out. If you don't be out, you'll die in the egg.
# They went to the farm, and the animals mock at the ugly duckling go away. If you beat back, animals will hit you.
# The ugly duckling went to the hole, and survived winter. The hole had the code, and you must guess the right code,or you froze to death.
# In spring, the ugly duckling went to the river, and a swan told that it is swan.
# The ugly duckling looked into the water,and become happy. If you don't look into the water, you will sad to die.
# Word List
# Map
# Scene
# Engine
# Haystack
# farm
# forest
# hole
# river
# tree of concepts
# Scene
# Haystack
# Farm
# Forest
# Hole
# River
# Death
# Map
# Engine