当用Python开发出魔塔游戏的时候,小伙伴都惊呆…

2018-06-18 03:21:17来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

《魔塔》是一种策略类的固定数值RPG游戏。游戏需要动很多脑筋,任何一个轻率的选择都可能导致游戏的失败。魔塔游戏虽不大,但是制作精美,道具很多,而且难度不低,对智商是一次艰巨的考验。

虽然魔塔的界面很像是一般的地牢游戏,貌似随便的打打杀杀就可以过关,但事实上玩这个游戏需要动很多脑筋,任何一个轻率的选择都可能导致游戏的失败,该游戏有属性攻击、防御、生命、金币、经验。对怪物的伤害次数计算公式,是敌人的生命/(自己的攻击-敌人的防御);而伤害的数目计算是怪物伤害次数(敌人的攻击-自己的防御)。

这个是Python2.7版本的

这个游戏内含的元素

故事背景

怪物(名称、生命、攻击、防御、击杀获取经验值)

房间

物品名称

人物属性:经验值、生命值、攻击、防御

main.py

  1 # -*- coding:utf-8 -*-
  2 import display,fight
  3 import re
  4 u"""
  5 魔王抓走了公主,玩家要进入魔塔打败魔王,救出公主。
  6 魔塔由很多房间组成,房间里有物品、怪物,还要公主和魔王。
  7 玩家要打败怪物升级,搜索房间获得物品,让自己属性变得更强,才能打败魔王。
  8 要用一个引擎让这个充满房间的魔塔运行。
  9 Python学习交流群:125240963,群内每天分享干货,包括最新的python企业案例学习资料和零基础入门教程,欢迎各位小伙伴入群学习交流
 10 """
 11 #游戏引擎类
 12 class Engine(object):
 13     def __init__(self,tower):
 14         self.tower = tower
 15     def play(self):
 16 #游戏介绍
 17         print u"""
 18         欧洲中世纪时期,一位叫玛丽的公主被恶魔掳走了。
 19         国王悲痛欲绝,许下诺言说谁要是能救回公主,就把国家的国土分他一半。
 20         可是并没有人敢答应,因为大家都知道恶魔十分恐怖,它所住的地方--魔塔,
 21         更是一个阴森恐怖,极度危险的地方,到过那的人没有一个能活着回来。
 22         这个时候,一个叫卡尔的年轻人主动上前,表示愿意试一试。
 23         国王说,年轻人那就拜托你了,魔塔离这里路途遥远,不过我可以用时光之杖直接送你过去。
 24         只见一道白光,你被传送到了魔塔。
 25         """
 26         raw_input('>')
 27         print u"""
 28         你来到了魔塔,这里阴森恐怖,有很多怪物在这出没,也有一些地方藏着很厉害的武器,
 29         你要试着去找到那些武器让自己变得更加强大,挑战不同的怪物让自己升级,才能让自己
 30         有足够能力打败魔王!
 31         在游戏中,Q键可以查看你和怪物的属性,战斗预测可以告诉你打败怪物会消耗的血量。
 32         E键可以查看小地图,了解你在地图中的位置。
 33         游戏开始了,加油!
 34         """
 35         raw_input('>')
 36         now_room = None
 37         next_room = self.tower.first_room
 38         while True:
 39             room = self.tower.enter_room(next_room,now_room)
 40             now_room = next_room
 41             next_room = room
 42 
 43 #魔塔类
 44 class Tower(object):
 45     def __init__(self,first_room):
 46         self.room = {"room1":Room1(),"room2":Room2(),"room3":Room3(),
 47                 "room4":Room4(),"room5":Room5(),"room6":Room6(),
 48                 "room7":Room7(),"room8":Room8(),"room9":Room9(),
 49                 "basement":basement(),"palace":palace()}
 50         self.first_room = first_room
 51 
 52     def enter_room(self,next_room,now_room):
 53         next_room = self.room.get(next_room)
 54         return next_room.enter(now_room)
 55 
 56 #房间类
 57 class Room(object):
 58     room_dict = {'room1':1,'room2':2,'room3':3,'room4':4,
 59                 'room5':5,'room6':6,'room7':7,'room8':8,
 60                 'room9':9,'palace':-1,'basement':11}
 61     toward_dict = {"w":"up","s":"down",
 62                    "a":"left","d":"right","f":"middle"}
 63     open_flag = 0
 64     def enter(self,last_room):
 65 #显示房间剧情
 66         print "---------------------------------------------\n"
 67         num = re.findall(r"\d",self.room_name)
 68         if num == []:
 69             if self.room_name == "palace":
 70                 print u"宫殿"
 71             else:
 72                 print u"地下室"
 73         else:
 74             print u"进入房间%s" % num[0]
 75         print self.words
 76 
 77 #获取玩家在房间的位置
 78         self.player_pos = self.get_playerpos(last_room)
 79 #显示房间样子
 80         display.display_room(self.player_pos,self.monster,self.monster_pos,
 81         self.door_pos,self.goods)
 82 #如果去过地下室,则可以进入宫殿
 83         if self.room_name == "basement":
 84             Room.open_flag = 1
 85 #进入玩家选择循环
 86         while True:
 87             print u"请选择:1、搜索物品  2、消灭怪物  3、离开房间"
 88             print u"Q键查看玩家属性  E键显示小地图"
 89             choose0 = raw_input('>')
 90             if choose0 == "q":
 91                 display.display_property(self.monster)
 92             elif choose0 == "e":
 93                 display.display_pos(self.room_name)
 94             elif choose0 == "1":
 95                 fight.find_goods(self.goods)
 96                 self.goods = []
 97             elif choose0 == "2":
 98                 print u"请选择你要挑战的怪物:W、上 S、下 A、左 D、右  F、中  R、返回"
 99                 choose1 = raw_input('>')
100                 if choose1 == "r":
101                     continue
102                 monster_toward = Room.toward_dict.get(choose1)
103                 if monster_toward not in self.monster_pos:
104                     print u"那里没有怪物。"
105                     continue
106                 fight.fight(self.monster)
107                 self.monster_pos.remove(monster_toward)
108             elif choose0 == "3":
109                 print u"请选择你要进入的房间:W、上 S、下 A、左 D、右    R、返回"
110                 choose2 = raw_input('>')
111                 if choose2 == "r":
112                     continue
113                 door_toward = Room.toward_dict.get(choose2)
114                 if door_toward not in self.door_pos:
115                     print u"那是一堵墙。"
116                     continue
117                 if door_toward in self.monster_pos:
118                     print u"你必须打败门前的怪物,才能进去。"
119                     continue
120                 #根据方向得到下个房间的名字
121                 num_dict = {"up":-3,"down":3,
122                                "left":-1,"right":1}
123                 next_roomnum = Room.room_dict.get(self.room_name)
124                 next_roomnum += num_dict.get(door_toward)
125                 for key,value in Room.room_dict.items():
126                     if value == next_roomnum:
127                         next_room =  key
128                         break
129                 if next_room == "palace" and Room.open_flag == 0:
130                     print u"这个地方感觉很危险,我还是等会再进去吧~"
131                     continue
132                 return next_room
133             else:
134                 print u"指令错误。"
135 
136     def get_playerpos(self,last_room):
137         if last_room == None:
138             return "middle"
139         last_roomnum = Room.room_dict.get(last_room)
140         now_roomnum = Room.room_dict.get(self.room_name)
141         num = now_roomnum - last_roomnum
142         if num == 3:
143             return "up"
144         elif num == 1:
145             return "left"
146         elif num == -1:
147             return "right"
148         elif num == -3:
149             return "down"
150         else:
151             print u"无法确定玩家位置!"
152 
153 #房间1
154 class Room1(Room):
155     def __init__(self):
156         self.room_name = "room1"
157         self.goods = [u"圣剑",u"中血瓶"]
158         self.monster = ""
159         self.monster_pos = []
160         self.door_pos = ["right"]
161         self.words = u"这是个存放物品的房间,说不定藏着什么好东西!"
162 
163 
164 
165 #房间2
166 class Room2(Room):
167     def __init__(self):
168         self.room_name = "room2"
169         self.goods = []
170         self.monster = u"巫师"
171         self.monster_pos = ["left","middle","up","right"]
172         self.door_pos = ["left","up","down"]
173         self.words = u"房间里似乎有一些可怕的生物,你要打败门口的怪物,才能打开相应的门。"
174 
175 #房间3
176 class Room3(Room):
177     def __init__(self):
178         self.room_name = "room3"
179         self.goods = [u"圣盾",u"超大血瓶"]
180         self.monster = ""
181         self.monster_pos = []
182         self.door_pos = ["down"]
183         self.words = u"这是个存放物品的房间,说不定藏着什么好东西!"
184 
185 #房间4
186 class Room4(Room):
187     def __init__(self):
188         self.room_name = "room4"
189         self.goods = []
190         self.monster = u"史莱姆"
191         self.monster_pos = ["left","middle","up","down"]
192         self.door_pos = ["right","down"]
193         self.words = u"房间里似乎有一些可怕的生物,你要打败门口的怪物,才能打开相应的门。"
194 
195 #房间5
196 class Room5(Room):
197     def __init__(self):
198         self.room_name = "room5"
199         self.goods = []
200         self.monster = ""
201         self.monster_pos = []
202         self.door_pos = ["left","right","up","down"]
203         self.words = u"这是一个空荡荡的房间,可以通向四面八方。"
204 
205 #房间6
206 class Room6(Room):
207     def __init__(self):
208         self.room_name = "room6"
209         self.goods = []
210         self.monster = u"小魔王"
211         self.monster_pos = ["up"]
212         self.door_pos = ["left","up"]
213         self.words = u"你走进了小恶魔的房间,这是魔塔里十分强大的怪物,最好不要惹他。"
214 
215 #房间7
216 class Room7(Room):
217     def __init__(self):
218         self.room_name = "room7"
219         self.goods = [u"小血瓶"]
220         self.monster = ""
221         self.monster_pos = []
222         self.door_pos = ["up"]
223         self.words = u"这是个存放物品的房间,说不定藏着什么好东西!"
224 
225 #房间8
226 class Room8(Room):
227     def __init__(self):
228         self.room_name = "room8"
229         self.goods = []
230         self.monster = u"骑士"
231         self.monster_pos = ["left","down","right","middle"]
232         self.door_pos = ["down","up","right"]
233         self.words = u"房间里似乎有一些可怕的生物,你要打败门口的怪物,才能打开相应的门。"
234 
235 #房间9
236 class Room9(Room):
237     def __init__(self):
238         self.room_name = "room9"
239         self.goods = [u"大血瓶",u"圣水"]
240         self.monster = ""
241         self.monster_pos = []
242         self.door_pos = ["left"]
243         self.words = u"这是个存放物品的房间,说不定藏着什么好东西!"
244 
245 #地下室
246 class basement(Room):
247     def __init__(self):
248         self.room_name = "basement"
249         self.goods = []
250         self.monster = u"公主"
251         self.monster_pos = ["middle"]
252         self.door_pos = ["up"]
253         self.words = u"""
254         这是一个的地下室,有个人被囚禁在其中。原来是公主!
255         你叫醒了公主,说到"公主,我是你父王派来救你的,快跟我走吧!"
256         公主揉了揉眼睛,说:"非常感谢你英雄,可是我被魔王用法术禁锢在这里了,
257         只有打败大魔王,才能离开。大魔王的位置在地图最上边的神秘房间里,
258         那个地方十分危险,在进去之前一定要做好充分的准备!"
259         "恩,等我的好消息吧!",你说到。
260         """
261 ""
262 #魔王宫殿
263 class palace(Room):
264     def __init__(self):
265         self.room_name = "palace"
266         self.goods = []
267         self.monster = u"大魔王"
268         self.monster_pos = ["middle"]
269         self.door_pos = []
270         self.words = u"""你走进了大魔王的宫殿。
271         "年轻人,你终于来了,我等你很久了。我要告诉你一个不幸的消息,这个房子不打败我
272         是无法离开的。哈哈哈,乖乖受死吧!"
273         """
274 
275 #角色类
276 class Role(object):
277     pass
278 
279 
280 
281 
282 tower = Tower("room5")
283 game = Engine(tower)
284 game.play()

figth.py

  1 # -*- coding:utf-8 -*-
  2 import copy
  3 u"""
  4 本模块用于配置玩家和怪物的属性,提供战斗函数。另外提供一个物品使用函数。
  5 """
  6 #配置玩家属性
  7 player_property = {u"名字":u"卡尔",u"生命值":100,u"攻击力":10,u"护甲":10,u"经验":0,u"符号":"(^_^)"}
  8 ##配置怪物属性
  9 Slime_property = {u"名字":u"史莱姆",u"生命值":25,u"攻击力":15,u"护甲":15,u"符号":"~0Y0~"}
 10 wizard_property = {u"名字":u"巫师",u"生命值":50,u"攻击力":40,u"护甲":10,u"符号":">@Y@<"}
 11 knight_property = {u"名字":u"骑士",u"生命值":350,u"攻击力":30,u"护甲":50,u"符号":"\OYO/"}
 12 s_devil_property = {u"名字":u"小魔王",u"生命值":400,u"攻击力":100,u"护甲":100,u"符号":"~/-Y-\~"}
 13 g_devil_property = {u"名字":u"大魔王",u"生命值":3000,u"攻击力":300,u"护甲":300,u"符号":"~~//-Y-\\\~~"}
 14 princess_property = {u"名字":u"公主",u"符号":"^o^"}
 15 
 16 
 17 def fight(monster):
 18     u"""
 19     函数功能:让玩家和怪物进行战斗,若玩家胜利,则设置玩家剩余生命值,若失败,则退出游戏。
 20     输入参数:monster--怪物,有效的值:u"史莱姆",u"巫师",u"骑士",u"小魔王",u"大魔王"
 21     """
 22     global player_property,Slime_property,wizard_property
 23     global knight_property,s_devil_property,g_devil_property
 24 
 25     #判断是什么怪物
 26     if monster == Slime_property[u"名字"]:
 27         monster_property = copy.deepcopy(Slime_property)
 28     elif monster == wizard_property[u"名字"]:
 29         monster_property = copy.deepcopy(wizard_property)
 30     elif monster == knight_property[u"名字"]:
 31         monster_property = copy.deepcopy(knight_property)
 32     elif monster == s_devil_property[u"名字"]:
 33         monster_property = copy.deepcopy(s_devil_property)
 34     elif monster == g_devil_property[u"名字"]:
 35         monster_property = copy.deepcopy(g_devil_property)
 36     elif monster == princess_property[u"名字"]:
 37         print u"是不是傻!公主你也杀。"
 38         exit()
 39     else:
 40         print u"怪物名字输入错误!"
 41         exit()
 42 
 43 #开始战斗
 44     who_round = "player"
 45     while player_property[u"生命值"] > 0 and monster_property[u"生命值"] > 0:
 46         if who_round == "player":
 47             monster_property[u"生命值"] -= (player_property[u"护甲"] + player_property[u"攻击力"])
 48             who_round = "monster"
 49         else:
 50             player_property[u"生命值"] -= (monster_property[u"护甲"] + monster_property[u"攻击力"])
 51             who_round = "player"
 52 
 53 #战斗结果处理
 54     if player_property[u"生命值"] <= 0:
 55         print u'"不自量力的家伙,哼!"  怪物将你大卸八块,吃掉了!'
 56         print u"你被打败了,游戏结束!"
 57         exit()
 58     else:
 59         print u"你打败了怪物,赢得了胜利,获得25点经验!"
 60         player_property[u"经验"] += 25
 61 #打败小魔王升3级
 62     if monster == s_devil_property[u"名字"]:
 63             print u"打败小魔王,连升3级!生命值+300,攻击力加30,护甲加30!"
 64             player_property[u"生命值"] += 300
 65             player_property[u"攻击力"] += 30
 66             player_property[u"护甲"] += 30
 67 #打败大魔王游戏结束
 68     if monster == g_devil_property[u"名字"]:
 69             print u"""
 70             "啊~不,不可能!我竟然被打败了!",大魔王怒吼道,
 71             "既然你打败了我,那么我就要你陪葬,你们谁都跑不了,哈哈哈哈哈~"
 72             这时地动山摇,墙壁开裂,屋顶的石头不断落下,魔塔开始坍塌。
 73             你趁机从墙壁的裂缝里溜了出去,救出了公主。
 74             从此,英雄卡尔的名字成了一个传说~
 75             恭喜你,通关!
 76 Python学习交流群:125240963,群内每天分享干货,包括最新的python企业案例学习资料和零基础入门教程,欢迎各位小伙伴入群学习交流
 77             """
 78             exit()
 79 #经验达到100升级属性
 80     if player_property[u"经验"] == 100:
 81         print u"恭喜你,升了1级,生命值+100,攻击力加10,护甲加10!"
 82         player_property[u"生命值"] += 100
 83         player_property[u"攻击力"] += 10
 84         player_property[u"护甲"] += 10
 85         player_property[u"经验"] = 0
 86 
 87 def forecast_fight(monster):
 88     u"""
 89     函数功能:预测玩家和怪物战斗需要的生命值
 90     输入参数:monster--怪物名字,有效的值:u"史莱姆",u"巫师",u"骑士",u"小魔王",u"大魔王"
 91     返回参数:health--消耗玩家的生命值
 92     """
 93     global player_property,Slime_property,wizard_property
 94     global knight_property,s_devil_property,g_devil_property
 95 
 96     #判断是什么怪物
 97     if monster == Slime_property[u"名字"]:
 98         monster_property = copy.deepcopy(Slime_property)
 99     elif monster == wizard_property[u"名字"]:
100         monster_property = copy.deepcopy(wizard_property)
101     elif monster == knight_property[u"名字"]:
102         monster_property = copy.deepcopy(knight_property)
103     elif monster == s_devil_property[u"名字"]:
104         monster_property = copy.deepcopy(s_devil_property)
105     elif monster == g_devil_property[u"名字"]:
106         monster_property = copy.deepcopy(g_devil_property)
107     else:
108         print u"怪物名字输入错误!"
109         exit()
110     player_health =  player_property[u"生命值"]
111 #模拟战斗
112     who_round = "player"
113     while monster_property[u"生命值"] > 0:
114         if who_round == "player":
115             monster_property[u"生命值"] -= (player_property[u"护甲"] + player_property[u"攻击力"])
116             who_round = "monster"
117         else:
118             player_health -= (monster_property[u"护甲"] + monster_property[u"攻击力"])
119             who_round = "player"
120 #计算消耗的血量
121     health = player_property[u"生命值"] - player_health
122     return health
123 
124 def find_goods(goods):
125     u"""
126     函数功能:搜索物品
127     输入参数:goods--物品,类型--列表
128     """
129     if goods == []:
130         print u"什么都没找到!"
131     else:
132         for good in goods:
133             if good == u"小血瓶":
134                 print u"你获得一个小血瓶,生命值增加100点!"
135                 player_property[u"生命值"] += 100
136             elif good == u"中血瓶":
137                 print u"你获得一个中血瓶,生命值增加200点!"
138                 player_property[u"生命值"] += 200
139             elif good == u"大血瓶":
140                 print u"你获得一个大血瓶,生命值增加400点!"
141                 player_property[u"生命值"] += 400
142             elif good == u"超大血瓶":
143                 print u"你获得一个超大血瓶,生命值增加600点!"
144                 player_property[u"生命值"] += 600
145             elif good == u"圣水":
146                 print u"你获得一瓶圣水,生命值增加一倍!攻击力增加一倍!护甲增加一倍!"
147                 player_property[u"生命值"] *= 2
148                 player_property[u"攻击力"] *= 2
149                 player_property[u"护甲"] *= 2
150             elif good == u"圣剑":
151                 print u"你获得一把圣剑,攻击力增加100!"
152                 player_property[u"攻击力"] += 100
153             elif good == u"圣盾":
154                 print u"你获得一个圣盾,护甲增加100!"
155                 player_property[u"护甲"] += 100
156             elif good == u"钥匙":
157                 print u"你获得一把钥匙!"
158             else:
159                 print u"物品名称错误!"

display.py

  1 # -*- coding:utf-8 -*-
  2 import copy
  3 import fight
  4 
  5 u"""
  6 这个模块提供一些实现显示功能的函数。
  7 """
  8 
  9 def display_pos(room):
 10     u"""
 11     函数功能:显示当前所在位置
 12     输入参数:room--房间号 ,类型--字符串
 13     """
 14     room_dict = {'room1':1,'room2':2,'room3':3,'room4':4,'room5':5,
 15                  'room6':6,'room7':7,'room8':8,'room9':9,
 16                  'palace':0,'basement':10}
 17     pos = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
 18     pos[room_dict.get(room)] = 2
 19     print u"""
 20         ================
 21         |   神秘房间   |
 22         |      %c       |
 23     ==========---===========
 24     | 房间1 | 房间2 | 房间3 |
 25     |  %c    |  %c    |  %c    |
 26     |       |       |       |
 27     |=======|=======|=======|
 28     | 房间4 | 房间5 | 房间6 |
 29     |  %c    |  %c    |  %c    |
 30     |       |       |       |
 31     |=======|=======|=======|
 32     | 房间7 | 房间8 | 房间9 |
 33     |  %c    |  %c    |  %c    |
 34     |       |       |       |
 35     ===========---===========
 36         |    地下室    |
 37         |      %c       |
 38         ================
 39     """ % (pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7],pos[8],pos[9],pos[10])
 40 
 41 
 42 
 43 def display_room(player_pos,monster,monster_pos,door_pos,goods):
 44     u"""
 45     函数功能:显示房间内的景象。
 46     输入参数:
 47     玩家的位置--player_pos,类型--字符串,有效的值--"up","left","right","down","middle",0(不存在)
 48 
 49     怪物的种类--monster,类型--字符串
 50         有效的值:  "史莱姆"   ~0Y0~
 51                    "巫师"   >@Y@<
 52                    "骑士"   \OYO/
 53                    "小魔王"  ~/-Y-\~
 54                    "大魔王"  ~~//-Y-\\\~~
 55                    "公主"   ^o^
 56                    ""     不存在怪物
 57 
 58     怪物的位置--monster_pos,类型--列表  空列表表示不存在怪物
 59     列表中每个元素,代表一个怪物的位置,类型--字符串,有效的值--"up","left","right","down","middle"
 60 
 61     门的位置--door_pos,类型--列表     空列表表示不存在门
 62     列表中每个元素--代表一个门的位置,类型--字符串,有效的值--"up","left","right","down"
 63 
 64     物品的种类--goods,类型--列表     空列表表示不存在物品
 65     列表中每个元素--代表一种物品,类型--字符串
 66         有效的值:"Excalibur" 圣剑   +---
 67                  "圣盾"       [+]
 68                  "钥匙"    C=--
 69                  "小血瓶"    (o)
 70                  "中血瓶"    (oo)
 71                  "大血瓶"    (ooo)
 72                  "超大血瓶"    (oooo)
 73                  "圣水"   [o]
 74                  "地图"    [|]
 75 
 76     备注:玩家图形:  (^_^)
 77     """
 78     drawlist = []
 79     good_pos = [6,7,9]
 80     for i in range(14):
 81         drawlist.append('  ')
 82 
 83     if player_pos == "up":          #根据输入参数,让玩家图形在对应的位置显示
 84         drawlist[2] = convert_sign(fight.player_property[u"符号"])
 85     elif player_pos == "down":
 86          drawlist[12] = convert_sign(fight.player_property[u"符号"])
 87     elif player_pos == "left":
 88          drawlist[5] = convert_sign(fight.player_property[u"符号"])
 89     elif player_pos == "right":
 90          drawlist[10] = convert_sign(fight.player_property[u"符号"])
 91     elif player_pos == "middle":
 92          drawlist[7] = convert_sign(fight.player_property[u"符号"])
 93     else:
 94         print u"玩家位置错误!"
 95 
 96     if monster == u"史莱姆":      #根据输入参数,设置怪物图形
 97         monster_sign = convert_sign(fight.Slime_property[u"符号"])
 98     elif monster == u"巫师":
 99         monster_sign = convert_sign(fight.wizard_property[u"符号"])
100     elif monster == u"骑士":
101         monster_sign = convert_sign(fight.knight_property[u"符号"])
102     elif monster == u"公主":
103         monster_sign = convert_sign(fight.princess_property[u"符号"])
104     elif monster == u"小魔王":
105         monster_sign = convert_sign(fight.s_devil_property[u"符号"])
106     elif monster == u"大魔王":
107         monster_sign = convert_sign(fight.g_devil_property[u"符号"])
108     elif monster == "":
109         pass
110     else:
111         print u"怪物名称错误!"
112         monster_sign = convert_sign("  ")
113 
114     for mon in monster_pos:           #根据输入参数,让怪物图形在对应的位置显示
115         if mon == "up":
116             drawlist[2] = monster_sign
117         elif mon == "down":
118              drawlist[12] = monster_sign
119         elif mon == "left":
120              drawlist[5] = monster_sign
121         elif mon == "right":
122              drawlist[10] = monster_sign
123         elif mon == "middle":
124              drawlist[7] = monster_sign
125         else:
126             print u"怪物位置错误!"
127 
128     for door in door_pos:           #根据输入参数,让门图形在对应的位置显示
129         if door == "up":
130             drawlist[0] = '_ '
131             drawlist[1] = '\b| |'
132         elif door == "down":
133              drawlist[13] = '\b|_|'
134         elif door == "left":
135              drawlist[3] = '|\\'
136              drawlist[4] = ' \\'
137         elif door == "right":
138              drawlist[8] = '/|'
139              drawlist[11] = '/ '
140         else:
141             print u"门位置错误!"
142 
143     for good in goods:             #根据输入参数判断是什么物品,并显示在指定的位置
144         if good == u"圣剑":
145             gooddraw = convert_sign("+---")
146         elif good == u"圣盾":
147             gooddraw = convert_sign("[+]")
148         elif good == u"钥匙":
149             gooddraw = convert_sign("C=--")
150         elif good == u"小血瓶":
151             gooddraw = convert_sign("(o)")
152         elif good == u"中血瓶":
153             gooddraw = convert_sign("(oo)")
154         elif good == u"大血瓶":
155             gooddraw = convert_sign("(ooo)")
156         elif good == u"超大血瓶":
157             gooddraw = convert_sign("(oooo)")
158         elif good == u"圣水":
159             gooddraw = convert_sign("[o]")
160         elif good == u"地图":
161             gooddraw = convert_sign("[|]")
162         else:
163             print u"物品名称错误!"
164             continue
165         drawlist[good_pos.pop(0)] = gooddraw
166 
167 
168     draw = (drawlist[0],drawlist[1],drawlist[2],drawlist[3],drawlist[4],
169     drawlist[5],drawlist[6],drawlist[7],drawlist[8],drawlist[9],drawlist[10],
170     drawlist[11],drawlist[12],drawlist[13])
171 
172     print """
173     ==================================
174     |-                              -|
175     | -                   %s       - |
176     |  -                  %s      -  |
177     |   --------------------------   |
178     |   -                  %s    -   |
179     |%s -                        -   |
180     |%s -    %s       %s         -   |
181     |   -             %s         - %s|
182     |   -             %s      %s - %s|
183     |   -      %s                -   |
184     |   --------------------------   |
185     | -       %s                   - |
186     ==================================
187     """ % draw
188 ##输入图形格式转换函数,保证%s所在位置就是字符串的最右端,用于对齐格式
189 def convert_sign(sign):
190     return '\b'*(len(sign)-2) + sign
191 
192 def display_property(monster):
193     u"""
194     函数功能:显示玩家和怪物属性
195     输入参数:monster--怪物名字,类型:字符串
196         有效的值: u"史莱姆",u"巫师",u"骑士",u"小魔王",u"大魔王",""(不存在怪物)
197     """
198     monster_exist = True
199     #判断是什么怪物
200     if monster == fight.Slime_property[u"名字"]:
201         monster_property = copy.deepcopy(fight.Slime_property)
202     elif monster == fight.wizard_property[u"名字"]:
203         monster_property = copy.deepcopy(fight.wizard_property)
204     elif monster == fight.knight_property[u"名字"]:
205         monster_property = copy.deepcopy(fight.knight_property)
206     elif monster == fight.s_devil_property[u"名字"]:
207         monster_property = copy.deepcopy(fight.s_devil_property)
208     elif monster == fight.g_devil_property[u"名字"]:
209         monster_property = copy.deepcopy(fight.g_devil_property)
210     elif monster == "":
211         monster_exist = False
212     else:
213         print u"怪物名字输入错误!"
214         exit()
215 
216     if monster_exist == True:
217         draw = (convert_sign(fight.player_property[u"符号"]),convert_sign(fight.player_property[u"名字"]),
218         fight.player_property[u"生命值"],fight.player_property[u"攻击力"],fight.player_property[u"护甲"],
219         convert_sign(monster_property[u"符号"]),convert_sign(monster_property[u"名字"]),
220         monster_property[u"生命值"],monster_property[u"攻击力"],monster_property[u"护甲"],
221         fight.forecast_fight(monster_property[u"名字"]))
222     else:
223         draw = (convert_sign(fight.player_property[u"符号"]),convert_sign(fight.player_property[u"名字"]) ,
224         fight.player_property[u"生命值"],fight.player_property[u"攻击力"],
225         fight.player_property[u"护甲"],"","","","","","")
226     print u"""
227     _________________________________________________
228               生命值    攻击力    护甲    战斗预测
229         %s
230       %s      %s       %s       %s
231 
232         %s
233       %s      %s       %s       %s      %s
234     _________________________________________________
235     """ % draw
236 
237 
238 # #测试
239 # display_property(0)
240 # display_property(u"史莱姆")
241 # display_property(u"骑士")
242 # display_property(u"大魔王")
243 # # #房间1
244 # # print u"房间1"
245 # display_room("right",0,[],["right"],[u"圣剑",u"钥匙",u"中血瓶"])
246 # # #房间2
247 # # print u"房间2"
248 # display_room("down",u"巫师",["left","right","up","middle"],["left","up","down"],[])
249 # # #房间3
250 # print u"房间3"
251 # display_room("down",0,[],["down"],[u"大血瓶",u"圣盾"])
252 # # #房间4
253 # print u"房间4"
254 # display_room("right",u"史莱姆",["left","down","up","middle"],["right","down"],[])
255 # # #房间5
256 # print u"房间5"
257 # display_room("middle",0,[],["up","left","right","down"],[])
258 # # #房间4
259 # print u"房间6"
260 # display_room("left",u"小魔王",["middle"],["left","up"],[])
261 # # #房间1
262 # print u"房间7"
263 # display_room("up",0,[],["up"],[u"地图",u"小血瓶"])
264 # # #房间8
265 # print u"房间8"
266 # display_room("up",u"骑士",["left","down","right","middle"],["up","right","down"],[])
267 # print u"房间9"
268 # display_room("left",0,[],["left"],[u"圣水",u"大血瓶"])
269 # # #恐怖之地
270 # print u"恐怖之地"
271 # display_room("down",u"大魔王",["middle"],["down"],[])
272 # # #地下室
273 # print u"地下室"
274 # display_room("up",u"公主",["middle"],["up"],[])

game.py

  1 # -*- coding:utf-8 -*-
  2 import display
  3 import fight
  4 class Room(object):
  5     def choose_do(self,lastroom):
  6 
  7         #根据上个房间的号码和当前房间号码的差得到当前玩家在房间里的位置
  8         compare = lastroom - self.roomnum
  9         if compare == -3:
 10             self.player_pos = "up"
 11         elif compare == -1:
 12             self.player_pos = "left"
 13         elif compare == 1:
 14             self.player_pos = "right"
 15         elif compare == 3:
 16             self.player_pos = "down"
 17         else:
 18             pass
 19 
 20         #显示房间场景
 21         display.display_room(self.player_pos,self.monster,
 22                         self.monster_pos,self.door_pos,self.goods)
 23 
 24         while True:
 25             print u"你可以选择:1、搜索房间  2、离开房间  3、查看小地图  4、查看自己的属性 5、消灭怪物"
 26             choice = raw_input(">")
 27             if choice == '1':
 28                 if self.goods != []:
 29                     print self.word
 30                     fight.use_goods(self.goods)
 31                     self.goods = []
 32                 else:
 33                     print u"\t什么也没有!\n"
 34             elif choice == '2':
 35                 while True:
 36                     print u"请选择你要进入的房间:W:上 A:左 S:下 D:右 Q:返回"
 37                     choose_room = raw_input('>')
 38 
 39                     if choose_room == "w":
 40                         choose_room = "up"
 41                     elif choose_room == "a":
 42                         choose_room = "left"
 43                     elif choose_room == "s":
 44                         choose_room = "down"
 45                     elif choose_room == "d":
 46                         choose_room = "right"
 47                     elif choose_room == 'q':
 48                         break
 49                     else:
 50                         pass
 51 
 52                     if choose_room not in self.door_pos:
 53                         print u"选择错误!"
 54                     else:
 55                         if choose_room in self.monster_pos:
 56                             print u"你必须打败门口的守卫,才能进入这个房间!"
 57                         else:
 58                             if choose_room == "up":
 59                                 return self.roomnum - 3
 60                             elif choose_room == "left":
 61                                 return self.roomnum - 1
 62                             elif choose_room == "down":
 63                                 return self.roomnum + 3
 64                             elif choose_room == "right":
 65                                 return self.roomnum + 1
 66                             else:
 67                                 pass
 68             elif choice == '3':
 69                 display.display_pos(self.roomnum)
 70             elif choice == '4':
 71                 display.display_property(self.monster)
 72             elif choice == '5':
 73                 if self.monster_pos == []:
 74                     print u"没有怪物!"
 75                 else:
 76                     print u"请选择你要攻击的怪物:W:上 A:左 S:下 D:右  E:中 Q:返回"
 77                     choose_monster = raw_input('>')
 78 
 79                     if choose_monster == "w":
 80                         choose_monster = "up"
 81                     elif choose_monster == "a":
 82                         choose_monster = "left"
 83                     elif choose_monster == "s":
 84                         choose_monster = "down"
 85                     elif choose_monster == "d":
 86                         choose_monster = "right"
 87                     elif choose_monster == "e":
 88                         choose_monster = "middle"
 89                     elif choose_monster == 'q':
 90                         continue
 91                     else:
 92                         pass
 93                     if choose_monster not in self.monster_pos:
 94                         print u"这个地方没有怪物!"
 95                         continue
 96                     fight.fight(self.monster)
 97                     self.monster_pos.remove(choose_monster)
 98             else:
 99                 print u"输入错误!"
100 
101 class Room1(Room):
102     def __init__(self):
103         self.roomnum = 1
104         self.player_pos = "right"
105         self.goods = [u"圣剑",u"钥匙",u"中血瓶"]
106         self.monster = 0
107         self.monster_pos = []
108         self.door_pos = ["right"]
109         self.word = u"""
110 得到一把圣剑 +---,攻击力上升100!
111 得到一个中血瓶 (oo),生命值上升200!
112 得到一把钥匙 C=--!
113         """
114     def story_room(self,lastroom):
115         u"""
116         函数功能:交代room1剧情,显示房间场景,获取用户选择,做相应处理。
117         输入参数:无
118         返回参数:self.nextroom--下个房间的号码,类型--数字类型
119         0--神秘房间   10--地下室
120         """
121         print u"房间1"
122         if self.goods != []:
123             print u"你进入了一个昏暗的房间,房间里似乎有一些东西在发光!"
124         else:
125             print u"你进入了一个昏暗的房间,房间里空无一物。"
126         #进入游戏循环
127         return Room.choose_do(self,lastroom)
128 
129 class Room2(Room):
130     def __init__(self):
131         self.roomnum = 2
132         self.player_pos = " "
133         self.goods = []
134         self.monster = u"巫师"
135         self.monster_pos = ["up","middle","left","right"]
136         self.door_pos = ["left","up","down"]
137         self.word = u"""
138         """
139     def story_room(self,lastroom):
140         u"""
141         函数功能:交代room2剧情,显示房间场景,获取用户选择,做相应处理。
142         输入参数:无
143         返回参数:self.nextroom--下个房间的号码,类型--数字类型
144         -1--神秘房间   11--地下室
145         """
146         #剧情交代
147         print u"房间2"
148         if self.monster_pos != []:
149             print u"你进入了一个昏暗的房间,房间里似乎有一些可怕的生物!"
150         else:
151             print u"你进入了一个昏暗的房间,房间里空无一物。"
152         #进入游戏循环
153         return Room.choose_do(self,lastroom)
154 # room1 = Room1(1)
155 # room1.story_room1()
156 
157 #启动游戏的类
158 class Game(object):
159     def start(self):
160         last_room = 1
161         room1 = Room1()
162         room2 = Room2()
163         nextroom = room1.story_room(last_room)
164         while True:
165             if nextroom == 1:
166                 nextroom = room1.story_room(last_room)
167                 last_room = 1
168             elif nextroom == 2:
169                 nextroom = room2.story_room(last_room)
170                 last_room = 2
171             else:
172                 pass
173 
174 playgame = Game()
175 playgame.start()

 

运行效果

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python3面向对象基础

下一篇:Python 简单分页思路