1 Star 0 Fork 1

左路谢桥/python-core

forked from luohanye/python-core 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rpg.py 2.67 KB
一键复制 编辑 原始数据 按行查看 历史
luohanye 提交于 2019-08-23 22:16 +08:00 . first commit
#! python3
"""
author: laoxu
"""
# 游戏说明
def showInstructions():
print('''
RPG Game
========
完成游戏条件:拿到钥匙和药水到达花园并躲避怪物。
命令:
go [direction]
get [item]
'''
)
# 打印当前房间和背包信息
def showCurrentRoom(room, bag):
print('You are in the %s' % currentRoom)
print('Inventory: ', bag)
rooms = {
'Hall': {
'south': 'Kitchen',
'east': 'Dinning Room',
'item': 'key'
},
'Kitchen': {
'north': 'Hall',
'item': 'monster'
},
'Dinning Room': {
'west': 'Hall',
'south': 'Garden',
'item': 'potion'
},
'Garden': {
'north': 'Dinning Room'
}
}
# 初始化房间
currentRoom = 'Hall'
# 初始化物品栏
inventory = []
# 打印游戏帮助
showInstructions()
print('You are in the %s' % currentRoom)
print('Inventory: ', inventory)
print('You see a key')
while True:
# 玩家进入厨房,游戏结束
if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
print('你被怪物抓住了...游戏结束!')
break
# 玩家拿到钥匙和药水进入花园,游戏结束
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
print('你逃脱了房子!你赢了!')
break
# 接收操作步骤
step = input()
# 客厅->厨房
if currentRoom == 'Hall' and step == 'go south':
currentRoom = 'Kitchen'
continue
# 客厅->餐厅
elif currentRoom == 'Hall' and step == 'go east':
currentRoom = 'Dinning Room'
# 厨房->客厅
elif currentRoom == 'Kitchen' and step == 'go north':
currentRoom = 'Hall'
# 餐厅->客厅
elif currentRoom == 'Dinning Room' and step == 'go west':
currentRoom = 'Hall'
# 餐厅->花园
elif currentRoom == 'Dinning Room' and step == 'go south':
currentRoom = 'Garden'
# 花园->餐厅
elif currentRoom == 'Garden' and step == 'go north':
currentRoom = 'Dinning Room'
# 拿到钥匙
elif currentRoom == 'Hall' and 'key' not in inventory and step == 'get key':
inventory.append('key')
print('key got!')
# 拿到药水
elif currentRoom == 'Dinning Room' and 'potion' not in inventory and step == 'get potion':
inventory.append('potion')
print('potion got!')
# 打印房间和物品栏
showCurrentRoom(currentRoom, inventory)
if currentRoom == 'Hall' and 'key' not in inventory:
print('You see a key')
if currentRoom == 'Dinning Room' and 'potion' not in inventory:
print('You see a potion')
continue
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/caodifeng/python-core.git
git@gitee.com:caodifeng/python-core.git
caodifeng
python-core
python-core
master

搜索帮助