《魔塔》是一種策略類的固定數值RPG遊戲。遊戲需要動很多腦筋,任何一個輕率的選擇都可能導致遊戲的失敗。魔塔遊戲雖不大,但是製作精美,道具很多,而且難度不低,對智商是一次艱巨的考驗。
雖然魔塔的介面很像是一般的地牢遊戲,貌似隨便的打打殺殺就可以過關,但事實上玩這個遊戲需要動很多腦筋,任何一個輕率的選擇都可能導致遊戲的失敗,該遊戲有屬性攻擊、防禦、生命、金幣、經驗。對怪物的傷害次數計算公式,是敵人的生命/(自己的攻擊-敵人的防禦);而傷害的數目計算是怪物傷害次數(敵人的攻擊-自己的防禦)。
這個是Python2.7版本的
這個遊戲內含的元素
故事背景
怪物(名稱、生命、攻擊、防禦、擊殺獲取經驗值)
房間
物品名稱
人物屬性:經驗值、生命值、攻擊、防禦
main.py
# -*- coding:utf-8 -*-
import display,fight
import re
u"""
魔王抓走了公主,玩家要進入魔塔打敗魔王,救出公主。
魔塔由很多房間組成,房間裡有物品、怪物,還要公主和魔王。
玩家要打敗怪物升級,搜尋房間獲得物品,讓自己屬性變得更強,才能打敗魔王。
要用一個引擎讓這個充滿房間的魔塔執行。
"""
#遊戲引擎類
class Engine(object):
def __init__(self,tower):
self.tower = tower
def play(self):
#遊戲介紹
print u"""
歐洲中世紀時期,一位叫瑪麗的公主被惡魔擄走了。
國王悲痛欲絕,許下諾言說誰要是能救回公主,就把國家的國土分他一半。
可是並沒有人敢答應,因為大家都知道惡魔十分恐怖,它所住的地方--魔塔,
更是一個陰森恐怖,極度危險的地方,到過那的人沒有一個能活著回來。
這個時候,一個叫卡爾的年輕人主動上前,表示願意試一試。
國王說,年輕人那就拜託你了,魔塔離這裡路途遙遠,不過我可以用時光之杖直接送你過去。
只見一道白光,你被傳送到了魔塔。
"""
raw_input('>')
print u"""
你來到了魔塔,這裡陰森恐怖,有很多怪物在這出沒,也有一些地方藏著很厲害的武器,
你要試著去找到那些武器讓自己變得更加強大,挑戰不同的怪物讓自己升級,才能讓自己
有足夠能力打敗魔王!
在遊戲中,Q鍵可以檢視你和怪物的屬性,戰鬥預測可以告訴你打敗怪物會消耗的血量。
E鍵可以檢視小地圖,瞭解你在地圖中的位置。
遊戲開始了,加油!
"""
raw_input('>')
now_room = None
next_room = self.tower.first_room
while True:
room = self.tower.enter_room(next_room,now_room)
now_room = next_room
next_room = room
#魔塔類
class Tower(object):
def __init__(self,first_room):
self.room = {"room1":Room1(),"room2":Room2(),"room3":Room3(),
"room4":Room4(),"room5":Room5(),"room6":Room6(),
"room7":Room7(),"room8":Room8(),"room9":Room9(),
"basement":basement(),"palace":palace()}
self.first_room = first_room
def enter_room(self,next_room,now_room):
next_room = self.room.get(next_room)
return next_room.enter(now_room)
#房間類
class Room(object):
room_dict = {'room1':1,'room2':2,'room3':3,'room4':4,
'room5':5,'room6':6,'room7':7,'room8':8,
'room9':9,'palace':-1,'basement':11}
toward_dict = {"w":"up","s":"down",
"a":"left","d":"right","f":"middle"}
open_flag = 0
def enter(self,last_room):
#顯示房間劇情
print "---------------------------------------------\n"
num = re.findall(r"\d",self.room_name)
if num == []:
if self.room_name == "palace":
print u"宮殿"
else:
print u"地下室"
else:
print u"進入房間%s" % num[0]
print self.words
#獲取玩家在房間的位置
self.player_pos = self.get_playerpos(last_room)
#顯示房間樣子
display.display_room(self.player_pos,self.monster,self.monster_pos,
self.door_pos,self.goods)
#如果去過地下室,則可以進入宮殿
if self.room_name == "basement":
Room.open_flag = 1
#進入玩家選擇迴圈
while True:
print u"請選擇:1、搜尋物品 2、消滅怪物 3、離開房間"
print u"Q鍵檢視玩家屬性 E鍵顯示小地圖"
choose0 = raw_input('>')
if choose0 == "q":
display.display_property(self.monster)
elif choose0 == "e":
display.display_pos(self.room_name)
elif choose0 == "1":
fight.find_goods(self.goods)
self.goods = []
elif choose0 == "2":
print u"請選擇你要挑戰的怪物:W、上 S、下 A、左 D、右 F、中 R、傳回"
choose1 = raw_input('>')
if choose1 == "r":
continue
monster_toward = Room.toward_dict.get(choose1)
if monster_toward not in self.monster_pos:
print u"那裡沒有怪物。"
continue
fight.fight(self.monster)
self.monster_pos.remove(monster_toward)
elif choose0 == "3":
print u"請選擇你要進入的房間:W、上 S、下 A、左 D、右 R、傳回"
choose2 = raw_input('>')
if choose2 == "r":
continue
door_toward = Room.toward_dict.get(choose2)
if door_toward not in self.door_pos:
print u"那是一堵牆。"
continue
if door_toward in self.monster_pos:
print u"你必須打敗門前的怪物,才能進去。"
continue
#根據方向得到下個房間的名字
num_dict = {"up":-3,"down":3,
"left":-1,"right":1}
next_roomnum = Room.room_dict.get(self.room_name)
next_roomnum += num_dict.get(door_toward)
for key,value in Room.room_dict.items():
if value == next_roomnum:
next_room = key
break
if next_room == "palace" and Room.open_flag == 0:
print u"這個地方感覺很危險,我還是等會再進去吧~"
continue
return next_room
else:
print u"指令錯誤。"
def get_playerpos(self,last_room):
if last_room == None:
return "middle"
last_roomnum = Room.room_dict.get(last_room)
now_roomnum = Room.room_dict.get(self.room_name)
num = now_roomnum - last_roomnum
if num == 3:
return "up"
elif num == 1:
return "left"
elif num == -1:
return "right"
elif num == -3:
return "down"
else:
print u"無法確定玩家位置!"
#房間1
class Room1(Room):
def __init__(self):
self.room_name = "room1"
self.goods = [u"聖劍",u"中血瓶"]
self.monster = ""
self.monster_pos = []
self.door_pos = ["right"]
self.words = u"這是個存放物品的房間,說不定藏著什麼好東西!"
#房間2
class Room2(Room):
def __init__(self):
self.room_name = "room2"
self.goods = []
self.monster = u"巫師"
self.monster_pos = ["left","middle","up","right"]
self.door_pos = ["left","up","down"]
self.words = u"房間裡似乎有一些可怕的生物,你要打敗門口的怪物,才能開啟相應的門。"
#房間3
class Room3(Room):
def __init__(self):
self.room_name = "room3"
self.goods = [u"聖盾",u"超大血瓶"]
self.monster = ""
self.monster_pos = []
self.door_pos = ["down"]
self.words = u"這是個存放物品的房間,說不定藏著什麼好東西!"
#房間4
class Room4(Room):
def __init__(self):
self.room_name = "room4"
self.goods = []
self.monster = u"史萊姆"
self.monster_pos = ["left","middle","up","down"]
self.door_pos = ["right","down"]
self.words = u"房間裡似乎有一些可怕的生物,你要打敗門口的怪物,才能開啟相應的門。"
#房間5
class Room5(Room):
def __init__(self):
self.room_name = "room5"
self.goods = []
self.monster = ""
self.monster_pos = []
self.door_pos = ["left","right","up","down"]
self.words = u"這是一個空蕩盪的房間,可以通向四面八方。"
#房間6
class Room6(Room):
def __init__(self):
self.room_name = "room6"
self.goods = []
self.monster = u"小魔王"
self.monster_pos = ["up"]
self.door_pos = ["left","up"]
self.words = u"你走進了小惡魔的房間,這是魔塔裡十分強大的怪物,最好不要惹他。"
#房間7
class Room7(Room):
def __init__(self):
self.room_name = "room7"
self.goods = [u"小血瓶"]
self.monster = ""
self.monster_pos = []
self.door_pos = ["up"]
self.words = u"這是個存放物品的房間,說不定藏著什麼好東西!"
#房間8
class Room8(Room):
def __init__(self):
self.room_name = "room8"
self.goods = []
self.monster = u"騎士"
self.monster_pos = ["left","down","right","middle"]
self.door_pos = ["down","up","right"]
self.words = u"房間裡似乎有一些可怕的生物,你要打敗門口的怪物,才能開啟相應的門。"
#房間9
class Room9(Room):
def __init__(self):
self.room_name = "room9"
self.goods = [u"大血瓶",u"聖水"]
self.monster = ""
self.monster_pos = []
self.door_pos = ["left"]
self.words = u"這是個存放物品的房間,說不定藏著什麼好東西!"
#地下室
class basement(Room):
def __init__(self):
self.room_name = "basement"
self.goods = []
self.monster = u"公主"
self.monster_pos = ["middle"]
self.door_pos = ["up"]
self.words = u"""
這是一個的地下室,有個人被囚禁在其中。原來是公主!
你叫醒了公主,說到"公主,我是你父王派來救你的,快跟我走吧!"
公主揉了揉眼睛,說:"非常感謝你英雄,可是我被魔王用法術禁錮在這裡了,
只有打敗大魔王,才能離開。大魔王的位置在地圖最上邊的神秘房間裡,
那個地方十分危險,在進去之前一定要做好充分的準備!"
"恩,等我的好訊息吧!",你說到。
"""
""
#魔王宮殿
class palace(Room):
def __init__(self):
self.room_name = "palace"
self.goods = []
self.monster = u"大魔王"
self.monster_pos = ["middle"]
self.door_pos = []
self.words = u"""你走進了大魔王的宮殿。
"年輕人,你終於來了,我等你很久了。我要告訴你一個不幸的訊息,這個房子不打敗我
是無法離開的。哈哈哈,乖乖受死吧!"
"""
#角色類
class Role(object):
pass
tower = Tower("room5")
game = Engine(tower)
game.play()
figth.py
# -*- coding:utf-8 -*-
import copy
u"""
本模組用於配置玩家和怪物的屬性,提供戰鬥函式。另外提供一個物品使用函式。
"""
#配置玩家屬性
player_property = {u"名字":u"卡爾",u"生命值":100,u"攻擊力":10,u"護甲":10,u"經驗":0,u"符號":"(^_^)"}
##配置怪物屬性
Slime_property = {u"名字":u"史萊姆",u"生命值":25,u"攻擊力":15,u"護甲":15,u"符號":"~0Y0~"}
wizard_property = {u"名字":u"巫師",u"生命值":50,u"攻擊力":40,u"護甲":10,u"符號":">@Y@}
knight_property = {u"名字":u"騎士",u"生命值":350,u"攻擊力":30,u"護甲":50,u"符號":"\OYO/"}
s_devil_property = {u"名字":u"小魔王",u"生命值":400,u"攻擊力":100,u"護甲":100,u"符號":"~/-Y-\~"}
g_devil_property = {u"名字":u"大魔王",u"生命值":3000,u"攻擊力":300,u"護甲":300,u"符號":"~~//-Y-\\\~~"}
princess_property = {u"名字":u"公主",u"符號":"^o^"}
def fight(monster):
u"""
函式功能:讓玩家和怪物進行戰鬥,若玩家勝利,則設定玩家剩餘生命值,若失敗,則退出遊戲。
輸入引數:monster--怪物,有效的值:u"史萊姆",u"巫師",u"騎士",u"小魔王",u"大魔王"
"""
global player_property,Slime_property,wizard_property
global knight_property,s_devil_property,g_devil_property
#判斷是什麼怪物
if monster == Slime_property[u"名字"]:
monster_property = copy.deepcopy(Slime_property)
elif monster == wizard_property[u"名字"]:
monster_property = copy.deepcopy(wizard_property)
elif monster == knight_property[u"名字"]:
monster_property = copy.deepcopy(knight_property)
elif monster == s_devil_property[u"名字"]:
monster_property = copy.deepcopy(s_devil_property)
elif monster == g_devil_property[u"名字"]:
monster_property = copy.deepcopy(g_devil_property)
elif monster == princess_property[u"名字"]:
print u"是不是傻!公主你也殺。"
exit()
else:
print u"怪物名字輸入錯誤!"
exit()
#開始戰鬥
who_round = "player"
while player_property[u"生命值"] > 0 and monster_property[u"生命值"] > 0:
if who_round == "player":
monster_property[u"生命值"] -= (player_property[u"護甲"] + player_property[u"攻擊力"])
who_round = "monster"
else:
player_property[u"生命值"] -= (monster_property[u"護甲"] + monster_property[u"攻擊力"])
who_round = "player"
#戰鬥結果處理
if player_property[u"生命值"] <= 0:
print u'"不自量力的傢伙,哼!" 怪物將你大卸八塊,吃掉了!'
print u"你被打敗了,遊戲結束!"
exit()
else:
print u"你打敗了怪物,贏得了勝利,獲得25點經驗!"
player_property[u"經驗"] += 25
#打敗小魔王升3級
if monster == s_devil_property[u"名字"]:
print u"打敗小魔王,連升3級!生命值+300,攻擊力加30,護甲加30!"
player_property[u"生命值"] += 300
player_property[u"攻擊力"] += 30
player_property[u"護甲"] += 30
#打敗大魔王遊戲結束
if monster == g_devil_property[u"名字"]:
print u"""
"啊~不,不可能!我竟然被打敗了!",大魔王怒吼道,
"既然你打敗了我,那麼我就要你陪葬,你們誰都跑不了,哈哈哈哈哈~"
這時地動山搖,牆壁開裂,屋頂的石頭不斷落下,魔塔開始坍塌。
你趁機從牆壁的裂縫裡溜了出去,救出了公主。
從此,英雄卡爾的名字成了一個傳說~
恭喜你,通關!
"""
exit()
#經驗達到100升級屬性
if player_property[u"經驗"] == 100:
print u"恭喜你,升了1級,生命值+100,攻擊力加10,護甲加10!"
player_property[u"生命值"] += 100
player_property[u"攻擊力"] += 10
player_property[u"護甲"] += 10
player_property[u"經驗"] = 0
def forecast_fight(monster):
u"""
函式功能:預測玩家和怪物戰鬥需要的生命值
輸入引數:monster--怪物名字,有效的值:u"史萊姆",u"巫師",u"騎士",u"小魔王",u"大魔王"
傳回引數:health--消耗玩家的生命值
"""
global player_property,Slime_property,wizard_property
global knight_property,s_devil_property,g_devil_property
#判斷是什麼怪物
if monster == Slime_property[u"名字"]:
monster_property = copy.deepcopy(Slime_property)
elif monster == wizard_property[u"名字"]:
monster_property = copy.deepcopy(wizard_property)
elif monster == knight_property[u"名字"]:
monster_property = copy.deepcopy(knight_property)
elif monster == s_devil_property[u"名字"]:
monster_property = copy.deepcopy(s_devil_property)
elif monster == g_devil_property[u"名字"]:
monster_property = copy.deepcopy(g_devil_property)
else:
print u"怪物名字輸入錯誤!"
exit()
player_health = player_property[u"生命值"]
#模擬戰鬥
who_round = "player"
while monster_property[u"生命值"] > 0:
if who_round == "player":
monster_property[u"生命值"] -= (player_property[u"護甲"] + player_property[u"攻擊力"])
who_round = "monster"
else:
player_health -= (monster_property[u"護甲"] + monster_property[u"攻擊力"])
who_round = "player"
#計算消耗的血量
health = player_property[u"生命值"] - player_health
return health
def find_goods(goods):
u"""
函式功能:搜尋物品
輸入引數:goods--物品,型別--串列
"""
if goods == []:
print u"什麼都沒找到!"
else:
for good in goods:
if good == u"小血瓶":
print u"你獲得一個小血瓶,生命值增加100點!"
player_property[u"生命值"] += 100
elif good == u"中血瓶":
print u"你獲得一個中血瓶,生命值增加200點!"
player_property[u"生命值"] += 200
elif good == u"大血瓶":
print u"你獲得一個大血瓶,生命值增加400點!"
player_property[u"生命值"] += 400
elif good == u"超大血瓶":
print u"你獲得一個超大血瓶,生命值增加600點!"
player_property[u"生命值"] += 600
elif good == u"聖水":
print u"你獲得一瓶聖水,生命值增加一倍!攻擊力增加一倍!護甲增加一倍!"
player_property[u"生命值"] *= 2
player_property[u"攻擊力"] *= 2
player_property[u"護甲"] *= 2
elif good == u"聖劍":
print u"你獲得一把聖劍,攻擊力增加100!"
player_property[u"攻擊力"] += 100
elif good == u"聖盾":
print u"你獲得一個聖盾,護甲增加100!"
player_property[u"護甲"] += 100
elif good == u"鑰匙":
print u"你獲得一把鑰匙!"
else:
print u"物品名稱錯誤!"
display.py
# -*- coding:utf-8 -*-
import copy
import fight
u"""
這個模組提供一些實現顯示功能的函式。
"""
def display_pos(room):
u"""
函式功能:顯示當前所在位置
輸入引數:room--房間號 ,型別--字串
"""
room_dict = {'room1':1,'room2':2,'room3':3,'room4':4,'room5':5,
'room6':6,'room7':7,'room8':8,'room9':9,
'palace':0,'basement':10}
pos = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
pos[room_dict.get(room)] = 2
print u"""
================
| 神秘房間 |
| %c |
==========---===========
| 房間1 | 房間2 | 房間3 |
| %c | %c | %c |
| | | |
|=======|=======|=======|
| 房間4 | 房間5 | 房間6 |
| %c | %c | %c |
| | | |
|=======|=======|=======|
| 房間7 | 房間8 | 房間9 |
| %c | %c | %c |
| | | |
===========---===========
| 地下室 |
| %c |
================
""" % (pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7],pos[8],pos[9],pos[10])
def display_room(player_pos,monster,monster_pos,door_pos,goods):
u"""
函式功能:顯示房間內的景象。
輸入引數:
玩家的位置--player_pos,型別--字串,有效的值--"up","left","right","down","middle",0(不存在)
怪物的種類--monster,型別--字串
有效的值: "史萊姆" ~0Y0~
"巫師" >@Y@<
"騎士" \OYO/
"小魔王" ~/-Y-\~
"大魔王" ~~//-Y-\\\~~
"公主" ^o^
"" 不存在怪物
怪物的位置--monster_pos,型別--串列 空串列表示不存在怪物
串列中每個元素,代表一個怪物的位置,型別--字串,有效的值--"up","left","right","down","middle"
門的位置--door_pos,型別--串列 空串列表示不存在門
串列中每個元素--代表一個門的位置,型別--字串,有效的值--"up","left","right","down"
物品的種類--goods,型別--串列 空串列表示不存在物品
串列中每個元素--代表一種物品,型別--字串
有效的值:"Excalibur" 聖劍 +---
"聖盾" [+]
"鑰匙" C=--
"小血瓶" (o)
"中血瓶" (oo)
"大血瓶" (ooo)
"超大血瓶" (oooo)
"聖水" [o]
"地圖" [|]
備註:玩家圖形: (^_^)
"""
drawlist = []
good_pos = [6,7,9]
for i in range(14):
drawlist.append(' ')
if player_pos == "up": #根據輸入引數,讓玩家圖形在對應的位置顯示
drawlist[2] = convert_sign(fight.player_property[u"符號"])
elif player_pos == "down":
drawlist[12] = convert_sign(fight.player_property[u"符號"])
elif player_pos == "left":
drawlist[5] = convert_sign(fight.player_property[u"符號"])
elif player_pos == "right":
drawlist[10] = convert_sign(fight.player_property[u"符號"])
elif player_pos == "middle":
drawlist[7] = convert_sign(fight.player_property[u"符號"])
else:
print u"玩家位置錯誤!"
if monster == u"史萊姆": #根據輸入引數,設定怪物圖形
monster_sign = convert_sign(fight.Slime_property[u"符號"])
elif monster == u"巫師":
monster_sign = convert_sign(fight.wizard_property[u"符號"])
elif monster == u"騎士":
monster_sign = convert_sign(fight.knight_property[u"符號"])
elif monster == u"公主":
monster_sign = convert_sign(fight.princess_property[u"符號"])
elif monster == u"小魔王":
monster_sign = convert_sign(fight.s_devil_property[u"符號"])
elif monster == u"大魔王":
monster_sign = convert_sign(fight.g_devil_property[u"符號"])
elif monster == "":
pass
else:
print u"怪物名稱錯誤!"
monster_sign = convert_sign(" ")
for mon in monster_pos: #根據輸入引數,讓怪物圖形在對應的位置顯示
if mon == "up":
drawlist[2] = monster_sign
elif mon == "down":
drawlist[12] = monster_sign
elif mon == "left":
drawlist[5] = monster_sign
elif mon == "right":
drawlist[10] = monster_sign
elif mon == "middle":
drawlist[7] = monster_sign
else:
print u"怪物位置錯誤!"
for door in door_pos: #根據輸入引數,讓門圖形在對應的位置顯示
if door == "up":
drawlist[0] = '_ '
drawlist[1] = '\b| |'
elif door == "down":
drawlist[13] = '\b|_|'
elif door == "left":
drawlist[3] = '|\\'
drawlist[4] = ' \\'
elif door == "right":
drawlist[8] = '/|'
drawlist[11] = '/ '
else:
print u"門位置錯誤!"
for good in goods: #根據輸入引數判斷是什麼物品,並顯示在指定的位置
if good == u"聖劍":
gooddraw = convert_sign("+---")
elif good == u"聖盾":
gooddraw = convert_sign("[+]")
elif good == u"鑰匙":
gooddraw = convert_sign("C=--")
elif good == u"小血瓶":
gooddraw = convert_sign("(o)")
elif good == u"中血瓶":
gooddraw = convert_sign("(oo)")
elif good == u"大血瓶":
gooddraw = convert_sign("(ooo)")
elif good == u"超大血瓶":
gooddraw = convert_sign("(oooo)")
elif good == u"聖水":
gooddraw = convert_sign("[o]")
elif good == u"地圖":
gooddraw = convert_sign("[|]")
else:
print u"物品名稱錯誤!"
continue
drawlist[good_pos.pop(0)] = gooddraw
draw = (drawlist[0],drawlist[1],drawlist[2],drawlist[3],drawlist[4],
drawlist[5],drawlist[6],drawlist[7],drawlist[8],drawlist[9],drawlist[10],
drawlist[11],drawlist[12],drawlist[13])
print """
==================================
|- -|
| - %s - |
| - %s - |
| -------------------------- |
| - %s - |
|%s - - |
|%s - %s %s - |
| - %s - %s|
| - %s %s - %s|
| - %s - |
| -------------------------- |
| - %s - |
==================================
""" % draw
##輸入圖形格式轉換函式,保證%s所在位置就是字串的最右端,用於對齊格式
def convert_sign(sign):
return '\b'*(len(sign)-2) + sign
def display_property(monster):
u"""
函式功能:顯示玩家和怪物屬性
輸入引數:monster--怪物名字,型別:字串
有效的值: u"史萊姆",u"巫師",u"騎士",u"小魔王",u"大魔王",""(不存在怪物)
"""
monster_exist = True
#判斷是什麼怪物
if monster == fight.Slime_property[u"名字"]:
monster_property = copy.deepcopy(fight.Slime_property)
elif monster == fight.wizard_property[u"名字"]:
monster_property = copy.deepcopy(fight.wizard_property)
elif monster == fight.knight_property[u"名字"]:
monster_property = copy.deepcopy(fight.knight_property)
elif monster == fight.s_devil_property[u"名字"]:
monster_property = copy.deepcopy(fight.s_devil_property)
elif monster == fight.g_devil_property[u"名字"]:
monster_property = copy.deepcopy(fight.g_devil_property)
elif monster == "":
monster_exist = False
else:
print u"怪物名字輸入錯誤!"
exit()
if monster_exist == True:
draw = (convert_sign(fight.player_property[u"符號"]),convert_sign(fight.player_property[u"名字"]),
fight.player_property[u"生命值"],fight.player_property[u"攻擊力"],fight.player_property[u"護甲"],
convert_sign(monster_property[u"符號"]),convert_sign(monster_property[u"名字"]),
monster_property[u"生命值"],monster_property[u"攻擊力"],monster_property[u"護甲"],
fight.forecast_fight(monster_property[u"名字"]))
else:
draw = (convert_sign(fight.player_property[u"符號"]),convert_sign(fight.player_property[u"名字"]) ,
fight.player_property[u"生命值"],fight.player_property[u"攻擊力"],
fight.player_property[u"護甲"],"","","","","","")
print u"""
_________________________________________________
生命值 攻擊力 護甲 戰鬥預測
%s
%s %s %s %s
%s
%s %s %s %s %s
_________________________________________________
""" % draw
# #測試
# display_property(0)
# display_property(u"史萊姆")
# display_property(u"騎士")
# display_property(u"大魔王")
# # #房間1
# # print u"房間1"
# display_room("right",0,[],["right"],[u"聖劍",u"鑰匙",u"中血瓶"])
# # #房間2
# # print u"房間2"
# display_room("down",u"巫師",["left","right","up","middle"],["left","up","down"],[])
# # #房間3
# print u"房間3"
# display_room("down",0,[],["down"],[u"大血瓶",u"聖盾"])
# # #房間4
# print u"房間4"
# display_room("right",u"史萊姆",["left","down","up","middle"],["right","down"],[])
# # #房間5
# print u"房間5"
# display_room("middle",0,[],["up","left","right","down"],[])
# # #房間4
# print u"房間6"
# display_room("left",u"小魔王",["middle"],["left","up"],[])
# # #房間1
# print u"房間7"
# display_room("up",0,[],["up"],[u"地圖",u"小血瓶"])
# # #房間8
# print u"房間8"
# display_room("up",u"騎士",["left","down","right","middle"],["up","right","down"],[])
# print u"房間9"
# display_room("left",0,[],["left"],[u"聖水",u"大血瓶"])
# # #恐怖之地
# print u"恐怖之地"
# display_room("down",u"大魔王",["middle"],["down"],[])
# # #地下室
# print u"地下室"
# display_room("up",u"公主",["middle"],["up"],[])
game.py
# -*- coding:utf-8 -*-
import display
import fight
class Room(object):
def choose_do(self,lastroom):
#根據上個房間的號碼和當前房間號碼的差得到當前玩家在房間裡的位置
compare = lastroom - self.roomnum
if compare == -3:
self.player_pos = "up"
elif compare == -1:
self.player_pos = "left"
elif compare == 1:
self.player_pos = "right"
elif compare == 3:
self.player_pos = "down"
else:
pass
#顯示房間場景
display.display_room(self.player_pos,self.monster,
self.monster_pos,self.door_pos,self.goods)
while True:
print u"你可以選擇:1、搜尋房間 2、離開房間 3、檢視小地圖 4、檢視自己的屬性 5、消滅怪物"
choice = raw_input(">")
if choice == '1':
if self.goods != []:
print self.word
fight.use_goods(self.goods)
self.goods = []
else:
print u"\t什麼也沒有!\n"
elif choice == '2':
while True:
print u"請選擇你要進入的房間:W:上 A:左 S:下 D:右 Q:傳回"
choose_room = raw_input('>')
if choose_room == "w":
choose_room = "up"
elif choose_room == "a":
choose_room = "left"
elif choose_room == "s":
choose_room = "down"
elif choose_room == "d":
choose_room = "right"
elif choose_room == 'q':
break
else:
pass
if choose_room not in self.door_pos:
print u"選擇錯誤!"
else:
if choose_room in self.monster_pos:
print u"你必須打敗門口的守衛,才能進入這個房間!"
else:
if choose_room == "up":
return self.roomnum - 3
elif choose_room == "left":
return self.roomnum - 1
elif choose_room == "down":
return self.roomnum + 3
elif choose_room == "right":
return self.roomnum + 1
else:
pass
elif choice == '3':
display.display_pos(self.roomnum)
elif choice == '4':
display.display_property(self.monster)
elif choice == '5':
if self.monster_pos == []:
print u"沒有怪物!"
else:
print u"請選擇你要攻擊的怪物:W:上 A:左 S:下 D:右 E:中 Q:傳回"
choose_monster = raw_input('>')
if choose_monster == "w":
choose_monster = "up"
elif choose_monster == "a":
choose_monster = "left"
elif choose_monster == "s":
choose_monster = "down"
elif choose_monster == "d":
choose_monster = "right"
elif choose_monster == "e":
choose_monster = "middle"
elif choose_monster == 'q':
continue
else:
pass
if choose_monster not in self.monster_pos:
print u"這個地方沒有怪物!"
continue
fight.fight(self.monster)
self.monster_pos.remove(choose_monster)
else:
print u"輸入錯誤!"
class Room1(Room):
def __init__(self):
self.roomnum = 1
self.player_pos = "right"
self.goods = [u"聖劍",u"鑰匙",u"中血瓶"]
self.monster = 0
self.monster_pos = []
self.door_pos = ["right"]
self.word = u"""
得到一把聖劍 +---,攻擊力上升100!
得到一個中血瓶 (oo),生命值上升200!
得到一把鑰匙 C=--!
"""
def story_room(self,lastroom):
u"""
函式功能:交代room1劇情,顯示房間場景,獲取使用者選擇,做相應處理。
輸入引數:無
傳回引數:self.nextroom--下個房間的號碼,型別--數字型別
0--神秘房間 10--地下室
"""
print u"房間1"
if self.goods != []:
print u"你進入了一個昏暗的房間,房間裡似乎有一些東西在發光!"
else:
print u"你進入了一個昏暗的房間,房間裡空無一物。"
#進入遊戲迴圈
return Room.choose_do(self,lastroom)
class Room2(Room):
def __init__(self):
self.roomnum = 2
self.player_pos = " "
self.goods = []
self.monster = u"巫師"
self.monster_pos = ["up","middle","left","right"]
self.door_pos = ["left","up","down"]
self.word = u"""
"""
def story_room(self,lastroom):
u"""
函式功能:交代room2劇情,顯示房間場景,獲取使用者選擇,做相應處理。
輸入引數:無
傳回引數:self.nextroom--下個房間的號碼,型別--數字型別
-1--神秘房間 11--地下室
"""
#劇情交代
print u"房間2"
if self.monster_pos != []:
print u"你進入了一個昏暗的房間,房間裡似乎有一些可怕的生物!"
else:
print u"你進入了一個昏暗的房間,房間裡空無一物。"
#進入遊戲迴圈
return Room.choose_do(self,lastroom)
# room1 = Room1(1)
# room1.story_room1()
#啟動遊戲的類
class Game(object):
def start(self):
last_room = 1
room1 = Room1()
room2 = Room2()
nextroom = room1.story_room(last_room)
while True:
if nextroom == 1:
nextroom = room1.story_room(last_room)
last_room = 1
elif nextroom == 2:
nextroom = room2.story_room(last_room)
last_room = 2
else:
pass
playgame = Game()
playgame.start()
執行結果