Python實戰(二十)
阿新 • • 發佈:2018-11-25
一 新增環境變數
二 實戰——開始web遊戲
1 app.py
#coding:utf-8 import web from gothonweb import map urls = ( '/game', 'GameEngine', '/', 'Index' ) app = web.application(urls, globals()) if web.config.get('_session') is None: store = web.session.DiskStore('sessions') session = web.session.Session(app, store, initializer={'room': None}) web.config._session = session else: session = web.config._session render = web.template.render('templates/', base="layout") # 渲染templates/資料夾下的index class Index(object): def GET(self): session.room = map.START web.seeother("/game") class GameEngine(object): def GET(self): if session.room: return render.show_room(room=session.room) else: return render.you_died() def POST(self): form = web.input(action=None) if session.room and form.action: session.room = session.room.go(form.action) web.seeother("/game") if __name__ == "__main__": app.run()
2 map.py
#coding:utf-8 class Room(object): def __init__(self, name, description): self.name = name self.description = description self.paths = {} # 返回依據direction在paths中的到的值 def go(self, direction): return self.paths.get(direction, None) # 給paths賦值 def add_paths(self, paths): self.paths.update(paths) central_corridor = Room("Central Corridor", """ 行星Percal#25 Gothons已經侵入你的飛船,銷燬 了你的所有船員。你是最後生存者,你的最後 任務是從武器軍械庫獲得中子毀滅炸彈, 把它放在橋樑,引爆它併成功進入 逃生艙逃生。 當你通過中央走廊向武器倉庫跑去的時候, 一個Gothon跳出,鱗狀面板紅,黑骯髒的牙齒,和邪惡的小丑服裝 流動圍繞著他充滿仇恨的身體。他堵大門 (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 Armory, put it in the bridge,and blow the ship up after getting into an escape pod. You're running down the central corridor to the Weapons Armory 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) """) laser_weapon_armory = Room("Laser Weapon Armory", """ 你是幸運的,他們讓你學會Gothon侮辱的學院。 你講一個你知道的笑話給Gothon: Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhag gur ubhfr。 Gothon停止,儘量不笑,然後爆發出大笑,無法動彈。 在他的笑的時候你跑起來拍他的頭部 把他打倒,然後跳進武器軍械庫的門。 你你翻身滾進武器兵工廠,蹲下以及掃描這個房間 尋找可能隱藏著的Gothons。這裡死一般的安靜,太安靜了。 你站起來,跑到房間的另一邊,並找到 獲得炸彈需要的程式碼。如果你得到的程式碼 錯10次那麼鎖將永遠關閉,你就不能 拿到炸彈。程式碼是3位數的。 (Lucky for you they made you learn Gothon insults in the academy. You tell the one Gothon joke you know: Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhag gur ubhfr. The Gothon stops, tries not to laugh, then busts out laughing and can't move. While he's laughing you run up and shoot him square in the head putting him down, then jump through the Weapon Armory door. You do a dive roll into the Weapon Armory, crouch and scan the room for more Gothons that might be hiding. It's dead quiet, too quiet. You stand up and run to the far side of the room and find the and you need the code to get the bomb out. If you get the code wrong 10 times then the lock closes forever and you can't get the bomb. The code is 3 digits.) """) the_bridge = Room("The Bridge", """ 該容器點選開啟,密封斷開,讓氣體出來。 你拿起中子彈和拼錦全力的跑,你可以到 橋,你必須將其放置在正確的位置。 你用胳膊夾著炸彈,5個試圖佔領 飛船的控制權的Gothons。他們每個人都有一個更醜陋 小丑服裝對比之前。他們沒有拿出自己 武器出來呢,因為他們看到在活動的炸彈 手臂,不希望被炸死。 (The container clicks open and the seal breaks, letting gas out. You grab the neutron bomb and run as fast as you can to the bridge where you must place it in the right spot. You burst onto the Bridge with the netron destruct bomb under your arm and surprise 5 Gothons who are trying to take control of the ship. Each of them has an even uglier clown costume than the last. They haven't pulled their weapons out yet,as they see the active bomb under your arm and don't want to set it off.) """) escape_pod = Room("Escape Pod", """ 你全部寄託在你的胳膊夾著的原子彈 Gothons舉起他們的手,開始流口水。 你快速到達門口,開啟它,然後仔細地 把炸彈放置在地板上,就指著你的炸彈了。 之後你跳回到門外,按下了關閉按鈕 和摧毀了鎖使Gothons無法通過。 現在,炸彈被放置好了,你逃到了逃生艙,以 逃離,這個鐵罐(飛船)。 你急於飛船,拼命取保飛船爆炸前 到達逃生艙。好像 幾乎沒有任何Gothons在船上,所以你逃跑時沒有任何 干擾。你到達了逃生艙的房間,現在 需要選擇一個上去。他們中的一些可能是損壞的 但你沒有時間去仔細看。有艘逃生艇,你打算上 那個? (You point your blaster at the bomb under your arm and the Gothons put their hands up and start to sweat. You inch backward to the door, open it, and then carefully place the bomb on the floor, pointing your blaster at it. You then jump back through the door, punch the close button and blast the lock so the Gothons can't get out. Now that the bomb is placed you run to the escape pod to get off this tin can. You rush through the ship desperately trying to make it to the escape pod before the whole ship explodes. It seems like hardly any Gothons are on the ship, so your run is clear of interference. You get to the chamber with the escape pods, and now need to pick one to take. Some of them could be damaged but you don't have time to look. There's 5 pods, which on do you take?) """) the_end_winner = Room("The End", """ 你跳進2號逃生艙,按下彈出按鈕。 該逃生艙很容易滑出到太空 前往下面的星球。當它飛向地球時,你回頭 看,會發現你的船爆炸時像 一顆明亮的星星,於此同時逃出了Gothon的飛船。你贏了! (You jump into pod 2 and hit the ejext button. The pod easily slides out into space heading to the planet below. As it flies to the planet, you look back and see your ship implode then explode like a bright star, taking out the Gothon ship at the same time. You won!) """) the_end_loser = Room("The End", """ 你隨機跳進一個逃生艙按下彈出按鈕。 該逃生艙掉進了空間空隙,然後 船體破裂爆炸了,你的身體粉碎成了 果醬果凍。 (You jump into a random pod and hit the eject button. The pod escapes out into the void of space, then impolodes as the hull ruptures, crushing your body into jam jelly.) """) escape_pod.add_paths({ '2': the_end_winner, '*': the_end_loser }) generic_death = Room("death", "You died.") the_bridge.add_paths({ 'throw the bomb': generic_death, 'slowly place the bomb': escape_pod }) laser_weapon_armory.add_paths({ '0132': the_bridge, '*': generic_death }) central_corridor.add_paths({ 'shoot!': generic_death, 'dodge!': generic_death, 'tell a joke': laser_weapon_armory }) START = central_corridor
3 layout.html
$def with (content) <html> <head> <title>Gothons From Planet Percal #25</title> </head> <body> <p>遊戲攻略: <ul> <li>escape_pod( '2': the_end_winner, '*': the_end_loser) </li> <li>the_bridge( 'throw the bomb': generic_death, 'slowly place the bomb': escape_pod) </li> <li>laser_weapon_armory( '0132': the_bridge, '*': generic_death) </li> <li>central_corridor( 'shoot!': generic_death, 'dodge!': generic_death, 'tell a joke': laser_weapon_armory) </li> <p> $:content </body> </html>
4 show_room.html
$def with (room)
<h1> $room.name </h1>
<pre>
$room.description
</pre>
$if room.name == "death":
<p><a href="/">Play Again?</a></p>
$else:
<p>
<form action="/game" method="POST">
-<input type="text" name="action"> <input type="SUBMIT">
</form>
</p>
5 you_died.html
<h1>You Died!</h1>
<p>Looks like you bit the dust.</p>
<p><a href="/">Play Again</a></p>
三 執行結果
E:\Python\exercise\ex52\gothonweb>python bin/app.py
http://0.0.0.0:8080/