首页 > 解决方案 > 移动和拾取基于项目的游戏(可能的 if-else 语句问题)

问题描述

我对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 件物品,我想在到达“藏宝室”后赢得比赛。

标签: dictionaryif-statementtokenitemstext-based

解决方案


您的帖子格式错误,因此您应该真正尝试清理它。

一般来说,您的问题是语法之一。这一行:

 if tokens[0] == 'move' or 'Move':

总是会评估为 True,因为or是在 之后评估的==,并且非空字符串的逻辑值是True 修复它的方法如下:

if tokens[0] == 'move' or tokens[0] == 'Move':

或者

 if tokens[0] in ('move', 'Move'):

还有其他解决方案,但以上任何一个都应该没问题。

一旦这个问题得到解决,我希望你会发现更多。尝试尽可能地解决它们,并在必要时重新发布新问题。但请先尝试解决问题,并确保发布格式正确、完整的代码。


推荐阅读