├── README.md └── game.py /README.md: -------------------------------------------------------------------------------- 1 | # python-wenzimaoxian 2 | python文字冒险游戏 3 | -------------------------------------------------------------------------------- /game.py: -------------------------------------------------------------------------------- 1 | #这是一个文字冒险游戏 2 | #作者:孙健 3 | #联系方式:QQ 28472517 4 | #开始创建于2018.09.26 5 | 6 | import random 7 | import math 8 | def change_player(mapSize): 9 | player['locationX']=math.floor((mapSize-1)/2) 10 | player['locationY']=math.floor((mapSize-1)/2) 11 | player['locationID']=math.floor(math.pow(mapSize,2)/2)+1 12 | #玩家数据中心 13 | player={ 14 | 'name':'小明', 15 | 'hp':10, 16 | 'maxhp':10, 17 | 'gold':0, 18 | 'items':[], 19 | 'level':1, 20 | 'xp':0, 21 | 'xpForNextLevel':100, 22 | 'locationX':0, 23 | 'locationY':0, 24 | 'locationID':0, 25 | 'exploredAreas':1 26 | } 27 | 28 | #初始化游戏地图和游戏区域 29 | gameMap ={} 30 | mapSize = 5 31 | map_Num = math.floor(math.pow(mapSize,2)) 32 | areaTpye = ["沙漠","平原","山丘","山脉","森林","沼泽","荒地","地牢"] 33 | change_player(mapSize) 34 | #在游戏地图中生成一个区域 35 | def makeArea(id,x,y): 36 | area={ 37 | 'mapID':id, 38 | 'x':x, 39 | 'y':y, 40 | 'type': random.choice(areaTpye), 41 | 'explored':False 42 | } 43 | return area 44 | 45 | #生成游戏地图 46 | def makeMap(rows,columns): 47 | thisRow=0 48 | thisCol=0 49 | thisArea = 1 50 | for r in range(0,rows): 51 | for c in range(0,columns): 52 | gameMap[thisArea]=makeArea(thisArea,thisRow,thisCol) 53 | if gameMap[thisArea]["mapID"]==player['locationID']: 54 | gameMap[thisArea]['type']='老家' 55 | gameMap[thisArea]['explored']=True 56 | thisArea += 1 57 | thisCol += 1 58 | thisRow += 1 59 | thisCol = 0 60 | #生成玩家 61 | def makePlayer(): 62 | player["name"] = input("请输入游戏主角名字: ") 63 | print ("\n你的游戏主角名字叫 {0}. 你的生命值是满的 ({1} 点), 你有 {2} 个金币. 你身上没有任何东西.".format(player["name"], player["hp"],player["gold"])) 64 | print ("\t%s 是一个一级角色, 需要获得 %s 点经验才能升级. 这样做我们会获得更多最大生命值. 你可以在探索和战斗中获得经验." % (player["name"],player["xpForNextLevel"])) 65 | print ("\t你的冒险开始于你的家乡,去探索更多土地吧...") 66 | def hitEnter(): 67 | input("\n回车键继续.") 68 | # 获取地区描述 69 | def description(): 70 | isExplored = "已探索" 71 | if gameMap[player["locationID"]]["explored"] == False: 72 | isExplored = "未探索" 73 | print ("\n当前位置: " + gameMap[player["locationID"]]["type"]) 74 | print ("(X: " + str(player["locationX"]) + ", Y: " + str(player["locationY"]) + ") 该区域状态: " + isExplored + "." + str(player["locationID"])+"号地区") 75 | 76 | # 绕地图移动. 77 | def movePlayer(direction): 78 | if direction == 'n': 79 | if player['locationY']+1 <= mapSize-1: 80 | player['locationY'] += 1 81 | player['locationID'] += mapSize 82 | else: 83 | player['locationY'] = 0 84 | player['locationID'] = (player['locationID']+mapSize)-map_Num 85 | elif direction == 's': 86 | if player['locationY'] -1 >= 0: 87 | player['locationY'] -=1 88 | player['locationID'] -= mapSize 89 | else: 90 | player["locationY"] = mapSize-1 91 | player["locationID"] = (player['locationID']-mapSize)+map_Num 92 | elif direction == "e": 93 | if player["locationX"] + 1 <= mapSize-1: 94 | player["locationX"] += 1 95 | player["locationID"] += 1 96 | else: 97 | player["locationX"] = 0 98 | player["locationID"] -= mapSize-1 99 | 100 | else: 101 | if player["locationX"] - 1 >= 0: 102 | player["locationX"] -= 1 103 | player["locationID"] -= 1 104 | else: 105 | player["locationX"] = mapSize-1 106 | player["locationID"] += mapSize-1 107 | description() 108 | #添加经验 109 | def addXp(num): 110 | player['xp'] += num 111 | print('\n%s 获得了 %s 点经验值。'% (player['name'],num)) 112 | if player['xp'] >= player['xpForNextLevel']: 113 | player['level'] += 1 114 | player['maxhp'] += 5 115 | player['hp'] = player['maxhp'] 116 | print('恭喜你,升级了 -- %s 现在是 %s 级了!'% (player['name'],player['level'])) 117 | #添加金币 118 | def addGold(num): 119 | player['gold'] += num 120 | print('\n%s 拾取了 %s 个金币。'% (player['name'],num)) 121 | def healhp(): 122 | print('你增加了 %s 点生命值。'%(player['maxhp']-player['hp'])) 123 | player['hp'] = player['maxhp'] 124 | def dofight(): 125 | monsterType = ["哥布林","兽人","强盗","僵尸","骷髅","史莱姆","小鬼","巨型蝙蝠","大甲虫","狼","熊"] 126 | myRace = random.choice(monsterType) 127 | theMonster = { 128 | 'race':myRace, 129 | 'hit_points':random.randrange(1,4), 130 | 'treasure':random.randrange(0,5) 131 | } 132 | print('\n%s 出现在你的面前。\n'%theMonster['race']) 133 | return theMonster 134 | 135 | def getAction(): 136 | print('\n可用命令') 137 | actionString = "" 138 | if gameMap[player["locationID"]]["explored"] == False: 139 | actionString += "\nx = 探索该地区。" 140 | if player["locationID"] == 13 and player["hp"] < player["maxhp"]: 141 | actionString += "\nr = 恢复休息。" 142 | print(actionString) 143 | print('n = 去北面 / s = 去南面 / w = 去西边 / e = 去东边.') 144 | print('i = 查看物品和金币') 145 | print('c = 查看角色状态') 146 | choice = input('请输入命令并回车').lower() 147 | print ("--------------------------------这是美丽的分割线--------------------------------") 148 | if choice == "i": 149 | print ("你目前携带: " + "%s" % ", ".join(map(str, player["items"]))) 150 | print ("金币: " + str(player["gold"])+" 个") 151 | #移动 152 | elif choice == "n" or choice == "e" or choice == "w" or choice == "s": 153 | movePlayer(choice) 154 | #探索地区 155 | elif choice == "x": 156 | if gameMap[player["locationID"]]["explored"] == False: 157 | player["exploredAreas"] += 1 158 | sumNum = random.randrange(1,4) 159 | if sumNum == 1: 160 | print('\n你找到了金币') 161 | gold=random.randrange(5,10) 162 | addGold(gold) 163 | addXp(int(math.floor(gold/12))) 164 | elif sumNum ==2: 165 | print ("天哪,你遇到了怪物!") 166 | theMonster=dofight() 167 | damage = theMonster['hit_points'] 168 | print ("你受到了 " + str(damage) + " 点伤害.") 169 | player["hp"] -= damage 170 | if player["hp"] > 0: 171 | addXp(damage*5) 172 | if theMonster['treasure'] >0: 173 | addGold(theMonster['treasure']) 174 | else: 175 | print('你看看了四周,没有发现什么有趣的东西。') 176 | addXp(1) 177 | gameMap[player["locationID"]]["explored"] = True 178 | else: 179 | print('这里已经探索了,没什么好探索的。') 180 | elif choice == "r" and player["locationID"] == 13 and player["hp"] < player["maxhp"]: 181 | healhp() 182 | elif choice == 'c': 183 | print(player['name'] + ' /等级: '+ str(player['level'])+ "级 经验值: " + str(player["xp"]) + "点 (还需要" + str(player["xpForNextLevel"]) + "点升级)") 184 | print ("生命值: " + str(player["hp"]) + " 点 (最大生命值: " + str(player["maxhp"]) + " 点)") 185 | elif choice == 'm': 186 | 187 | for i in range(0,mapSize): 188 | for j in range(map_Num-(i+1)*mapSize,map_Num-i*mapSize): 189 | print(gameMap[j+1]['type']+'('+str(gameMap[j+1]['mapID']).zfill(2)+'号) ',end='') 190 | print('\n') 191 | else: 192 | print('抱歉,我不知道你在说什么。') 193 | print ("--------------------------------这是美丽的分割线--------------------------------") 194 | makeMap(mapSize,mapSize) 195 | makePlayer() 196 | still_alive = True 197 | description() 198 | hitEnter() 199 | #游戏循环 200 | while still_alive: 201 | getAction() 202 | if player["exploredAreas"] == map_Num: 203 | print ("你已经探索了该地图所有区域!\n现在进入一个全新的地图...") 204 | hitEnter() 205 | mapSize = mapSize+2 206 | map_Num = math.floor(math.pow(mapSize,2)) 207 | change_player(mapSize) 208 | gamemap = {} 209 | makeMap(mapSize,mapSize) 210 | player["exploredAreas"] = 1 211 | 212 | description() 213 | if player["hp"] <= 0: # 玩家死亡 214 | print ("\n你挂了.") 215 | still_alive = False 216 | --------------------------------------------------------------------------------