【Python基礎】lpthw - Exercise 43 基本的面向對象分析和設計

1. A gameapp

  1 from sys import exit
  2 from random import randint
  3 from textwrap import dedent     # 使能夠使用三引號型的字符串
  4 
  5 # 具體實現了場景間的切換
  6 class Engine(object):
  7 
  8     def __init__(self, scene_map):
  9         self.scene_map = scene_map
 10 
 11     def play(self):
 12         current_scene = self.scene_map.opening_scene()  # 初始場景的類
 13         last_scene = self.scene_map.next_scene('finished')
 14 
 15         while current_scene != last_scene:
 16             next_scene_name = current_scene.enter()     # 返回一個場景的名字
 17             current_scene = self.scene_map.next_scene(next_scene_name)
 18 
 19         # 確保print出最後一個場景
 20         current_scene.enter()
 21 
 22 # 各場景共有的父類
 23 class Scene(object):
 24 
 25     def enter(self):
 26         pass
 27 
 28 # 具體的每一個場景,其中定義了一些說明和交互
 29 class Death(Scene):
 30 
 31     quips = ["You died.", "Please try again.", "That's not very good."] # 類變量,能夠經過類名調用
 32 
 33     def enter(self):
 34         print(Death.quips[randint(0,2)])
 35         exit(1)
 36 
 37 class CentralCorridor(Scene):
 38 
 39     def enter(self):
 40         print("""
 41         You are met with some strange things.
 42         This is a locked room.
 43         It will be a good idea to leave right away.
 44         You see a Gothon blocking the door.
 45         Try to do something.
 46         """)
 47         action = input('>')
 48         if action == 'shoot!':
 49             return 'death'
 50         elif action == 'tell a joke!':
 51             return 'laser_weapon_armory'
 52         else:
 53             print("Nothing happened.")
 54             return 'central_corridor'
 55 
 56 
 57 class LaserWeaponArmory(Scene):
 58 
 59     def enter(self):
 60         print("""
 61         This is the laser weapon armory.
 62         Destroy their weapon!
 63         Please enter the code.
 64         """)
 65 
 66         code = "123"
 67         guess = input('>')
 68         guesses = 0
 69 
 70         while guess != code and guesses < 5:
 71             print("That's not right!")
 72             guesses += 1
 73             guess = input('Try again >')
 74 
 75         if guess == code:
 76             print("Bingo! The weapon is destroyed!")
 77             return 'the_bridge'
 78         else:
 79             print("A bell rings and you are locked here to die!")
 80             return 'death'
 81 
 82 class TheBridge(Scene):
 83 
 84     def enter(self):
 85         print("A bridge is in front of you and you see a huge bomb. ")
 86         action = input('>')
 87 
 88         if action == "throw it away":
 89             print("It exploded as you aprroached it. You died.")
 90             return 'death'
 91         elif action == "leave it behind":
 92             print("It seems nothing happened. You can safely pass it.")
 93             return 'escape_pod'
 94         else:
 95             return 'death'
 96 
 97 class EscapeRod(Scene):
 98 
 99     def enter(self):
100         print("""
101         You finally make it to the escape rod.
102         There are five ships in front of you.
103         Which one do you want to take?
104         """)
105         num = input('>')
106         correct_ship = '3'
107         if num == correct_ship:
108             print("You got on the ship. Everything seems getting on well. You won!")
109             return 'finished'
110         else:
111             print("The ship is not working well. It exploded as take off!. You died!")
112             return 'death'
113 
114 class Finished(Scene):
115 
116     def enter(self):
117         print("Congratulations! You successfully escaped from the ship!")
118         return 'finished'
119 
120 # 定義了各場景的名字,定義了進出場景的方法
121 class Map(object):
122 
123     scenes = {'central_corridor': CentralCorridor(),
124               'laser_weapon_armory': LaserWeaponArmory(),
125               'the_bridge': TheBridge(),
126               'escape_pod': EscapeRod(),
127               'death': Death(),
128               'finished': Finished()
129               }
130 
131     def __init__(self, start_scene):
132         self.start_scene = start_scene
133 
134     def next_scene(self, scene_name):
135         nextScene = Map.scenes.get(scene_name)
136         return nextScene                            # 根據傳入名稱返回一個場景的類
137 
138     def opening_scene(self):
139         return self.next_scene(self.start_scene)    # 根據出發點名稱返回出發點場景的類
140 
141 a_map = Map("central_corridor")
142 a_game = Engine(a_map)
143 a_game.play()
相關文章
相關標籤/搜索