【发布时间】:2021-10-17 20:21:03
【问题描述】:
我对 python 有点陌生,我在课堂上做作业和作业,制作一个基于文本的游戏,该游戏从一个房间移动到另一个房间,包含东南西北和西北。除了第一个房间外,所有房间里都有玩家必须捡起的物品。移动的命令是'move "direction"',获取项目的命令是'get "item"'。我将房间设置在字典和每个房间的嵌套字典中,这些字典与相邻房间和每个房间中的项目相关。这是我当前的工作代码。
def show_instructions():
print('Collect 8 items to win the game and defeat the beast')
print('Move Commands: move North, move South, move West, move East, or Exit to leave the game')
print('Pickup items: get item, ex. get sword\n')
#Dictionary for rooms and Items
def main():
rooms = {
'Shed' : {'West': 'Well Room', 'item' : 'well room West of you'},
'Well Room' : {'South' : 'Sewers', 'East' : 'Shed', 'item' : 'Healing-Potion'},
'Sewers' : {'East' : 'Crawl Space','North' :'Well Room', 'item' : 'Armor'},
'Crawl Space' : {'East' : 'Cavern', 'West' : 'Sewers','item' : 'Sword'},
'Cavern' : {'South' : 'Dungeon Hall', 'West' : 'Crawl Space', 'item' : 'Shield'},
'Dungeon Hall' : {'East' : 'Hidden Room', 'West' : 'War Room', 'North' : 'Cavern', 'item' : 'Sword-Enchantment'},
'Hidden Room' : {'South' : 'Treasure Room', 'East' : 'Dungeon Hall', 'item' : 'Flame-Resistance-Potion'},
'War Room' : {'West' : 'Laboratory', 'East' : 'Dungeon Hall', 'item' : 'Armor-Enchantment'},
'Laboratory' : {'South' : 'Treasure Room', 'East' : 'War Room', 'Item' : 'Enchantment-Table'},
'Treasure Room' : {'item' : 'Dragon'}
}
#Helps with Decision branching
directions = ['North','South','East','West']
#Sets the player in the first room
currentroom = 'Shed'
inventory = []
print(show_instructions())
while currentroom != 'Exit':
def showStatus():
print('You are in the', currentroom)
print('Inventory:', inventory)
print('You see a', rooms[currentroom]['item'])
showStatus()
#Win Condition checkpoint
if currentroom == 'Treasure Room':
if len(inventory) < 8:
print('You get eaten by the Dragon')
break
else:
print('You slay the dragon and return to your boss victorious')
#input from player
c = input('What would you like to do?:\n')
#exit condition
if c == 'Exit':
currentroom = 'Exit'
#split to have tokens dignify if move or get item
tokens = c.split()
if tokens[0] == 'move' or 'Move':
if tokens[1] in directions:
try:
currentroom = rooms[currentroom][tokens[1]]
except KeyError:
print('You see a Wall')
else:
print('Not a Valid Direction')
elif tokens[0] == 'get' or 'Get':
if tokens[1] == rooms[currentroom]['item']:
inventory.append(rooms[currentroom]['item'])
else:
print('Not a valid Item')
else:
print('Not a Valid Entry')
print(main())
我遇到的问题是我是否输入 get 或 move 它会启动“if tokens[0] == 'move' or 'Move'”行。因此,键入 Get 只会打印“Not a Valid Direction”。谁能帮我看看我是如何弄乱我的陈述的? 一天结束时,我希望我的移动可以从一个房间到另一个房间,打印房间里的物品,能够将物品添加到库存中,然后移动到下一个房间。一旦收集了 8 件物品,我想在到达“藏宝室”时赢得比赛。
【问题讨论】:
标签: dictionary if-statement token items text-based