繁体   English   中英

用完整的物品清单击败基于文本的游戏

[英]Beating TextBased Game with a full inventory of items

所以这是我在这里发布的第一个问题,如果我搞砸了所有这些的格式,我很抱歉。 我正在尝试编写一个基于文本的游戏,从一个房间移动到另一个房间并将物品添加到您的库存中。 我目前的设置是,如果您进入恶棍房间并且没有所有物品,那么您就输了,但是如果您拥有所有物品,那么您就赢了。 有没有办法对其进行编码,一旦您的库存达到 6(所有项目),那么您将自动获胜并且您不必 go 到反派房间? 它只是更适合我的叙述,我想你可以说,如果我从来不需要 go 到恶棍房间。 例如,在被恶棍抓住之前试图逃跑,不一定要与恶棍战斗。

此外,这是我的第一个 class,我对 Python 还是很陌生,所以我现在拥有的当前代码上的任何指针也将不胜感激!提前致谢!

rooms = {
    'Dining Room': {'South': 'Bedroom', 'North': 'Shop', "East": 'Kitchen', 'West': 'Study', 'item': 'none'},
    'Bedroom': {'North': 'Dining Room', 'East': "Innkeepers Suite", 'item': 'Longbow'},
    'Garden': {'West': 'Shop', 'item': 'Book'},
    'Shop': {'East': 'Garden', 'South': 'Dining Room', 'item': 'Staff'},
    'Study': {'East': 'Dining Room', 'item': 'Gold Coins'},
    'Kitchen': {'West': 'Dining Room', 'North': 'Stables', 'item': 'Chest'},
    'Stables': {'South': 'Kitchen', 'item': 'Horse'},
    'Innkeepers Suite': {'West': 'Bedroom', 'item': 'Seeker'}
}
def main():
def instructions():
    print('Welcome to the Wandering Lotus Inn Text Based Game')
    print("Collect your 6 items hidden around")
    print("the inn before you're caught by the Seeker!")
    print('Move directions: West, East, North, South')
    print("Add item to inventory: 'get item'")

def player_location():
    print("-" * 40)
    print('You are in the {}'.format(currentRoom))
    print('Inventory:', Inventory)
    print('Item:', rooms[currentRoom]['item'])

currentRoom = 'Dining Room'
player_move = ''
Inventory = []

while currentRoom in rooms:
    player_location()
    player_move = input('Enter your move:\n')
    if player_move in rooms[currentRoom]:
        currentRoom = rooms[currentRoom][player_move]
        if currentRoom == 'Innkeepers Suite':
            if len(Inventory) == 6:
                print('You win!')
            if len(Inventory) != 6:
                print('You lose..')
            break
    elif player_move == 'get item':
        if rooms[currentRoom]['item'] != 'none':
            Inventory.append(rooms[currentRoom]['item'])
            print("You aquired : ", rooms[currentRoom]['item'])
            rooms[currentRoom]['item'] = 'none'
        elif rooms[currentRoom]['item'] == 'none':
            print("There isn't anything in here!")
    else:
        print("Not a valid instruction, try again")

main()
print('Game Over! Thanks for playing')

您的代码对我来说并没有完全运行,所以我很难检查这是否正确。 无论如何,您可以在 append 一个项目后获取您的Inventory长度。 如果Inventory的长度等于 6,则打印“You win!” 并跳出while循环。

所以像:

Inventory.append(rooms[currentRoom]['item'])
print("You aquired : ", rooms[currentRoom]['item'])

if len(Inventory) == 6:
    print("You win!")
    break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM