首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于文本的游戏获取项目和整理游戏

基于文本的游戏获取项目和整理游戏
EN

Stack Overflow用户
提问于 2022-02-19 18:34:14
回答 1查看 956关注 0票数 0

好的,所以,我目前正在尝试创建一个基于文本的游戏为一个班级和我有一些小问题。第一个是我创建的承载值,它不想捡起物品,我也不知道如何使循环在最后的房间中断。我对这件事很陌生,所以我不能百分之百肯定该怎么问,但这是我的代码

代码语言:javascript
复制
import time

rooms = {
    'Great Hall': {'name': 'Great Hall', 'South': 'Dining Room', 'North': 'Armory',
                   'East': 'Hallway', 'West': 'Bedroom', 'text': 'The Great Hall, begin your quest'},

    'Bedroom': {'name': 'Bedroom', 'East': 'Great Hall', 'contents': 'Magic wand',
                'text': 'A Bedroom. There appears to be a Magic wand in the corner'},

    'Hallway': {'name': 'Hallway', 'West': 'Great Hall', 'North': 'Cellar', 'contents': 'Sword',
                'text': 'A Hallway. There appears to be a Sword on the wall'},

    'Cellar': {'name': 'Cellar', 'South': 'Hallway', 'contents': 'Book',
               'text': 'A Cellar. There is a Book on a shelf'},

    'Dining Room': {'name': 'Dining Room', 'North': 'Great Hall', 'East': 'Kitchen', 'contents': 'Potion',
                    'text': 'The Dining Room. It looks like somebody left their Potion behind...'},

    'Kitchen': {'name': 'Kitchen', 'West': 'Dining Room', 'contents': 'Shield',
                'text': 'A Kitchen. What a strange place to find a Shield'},

    'Armory': {'name': 'Armory', 'South': 'Great Hall', 'East': 'Proving Grounds', 'contents': 'Armor',
               'text': 'The Armory. That Armor looks like it would fit you...'},

    'Proving Grounds': {'name': 'Proving Grounds', 'contents': 'Minotaur',
                        'text': 'The Proving Grounds. Are you ready for your showdown?'}
}
directions = ['North', 'South', 'East', 'West']
currentRoom = rooms['Great Hall']
carrying = []


def show_instructions():
    # print a main menu and the commands
    print('-------------------------------------------------------')
    print("Text Adventure Game")
    print("Collect 6 items to win the game, or be eaten by the minotaur.")
    print("Move commands: South, North, East, West")
    print("Add to Inventory: get 'item name'")
    print('-------------------------------------------------------')


print('''--------------------------------------------------------------------------------------------------------
A great beast has taken over your lands. You are a young Viking, tasked with defeating the minotaur 
and becoming the village hero. The grand hall holds everything you need. To defeat the beast, you must find the 
six treasures located within the grand hall and defeat the beast that plagues your land!
--------------------------------------------------------------------------------------------------------''')
time.sleep(4)
show_instructions()
time.sleep(4)
while True:
    # display current location
    print()
    print('You are in {}.'.format(currentRoom['text']))
    # get user input
    command = input('\nWhat do you do? ').strip()
    # movement
    if command in directions:
        if command in currentRoom:
            currentRoom = rooms[currentRoom[command]]
        else:
            # bad movement
            print("You can't go that way.")
    # quit game
    elif command.lower() in ('q', 'quit'):
        break
    # bad command
    elif command.lower().split()[0] == 'get':
        item = command.lower().split()[1]
        if item in currentRoom['contents']:
            carrying.append(item)
            print('You grabbed {}.'.format(currentRoom['contents']))
        else:
            print("I don't see that here.")
while currentRoom in ['Proving Grounds']:
    if len(carrying) in 6:
        print('You collected all of the items, and defeated the minotaur!')
    else:
        print('It looks like you have not found everything, you have been defeated!')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-19 19:18:17

你有很多问题。正如@Blackaurd所说,检查当前房间内容的代码是错误的,因为每个房间中的contents值是一个字符串,所以您将item与字符串中的每个字母进行比较。我希望您想要的是每个房间中的项目列表,因为除了您的代码预期之外,我还假设您希望能够在一个房间中拥有多个项目。

另一个问题是,当您将输入的项目名称与房间中的项目进行比较时,确保输入的项目名称是小写的,但是房间的contents中的项目以大写开头。在某些方面,你应该确保大写/小写不重要。

下面是如何解决这两个问题的方法。首先,更改对每个房间的描述,以便contents值是一个项目列表,如下所示:

代码语言:javascript
复制
'Armory': {'name': 'Armory', 'South': 'Great Hall', 'East': 'Proving Grounds',
    'contents': ['Armor'], 'text': 'The Armory. That Armor looks like it would fit you...'},

然后,您可以修改与房间内容匹配的支票,如下所示:

代码语言:javascript
复制
item = command.lower().split()[1]
if item in [x.lower() for x in currentRoom['contents']]:
    carrying.append(item)

您也可以永久地将房间内容转换为小写,但我认为您可能希望保留大写版本,以便显示。这样就行了。它不能改变任何东西。相反,它只是以一种以小写的方式进行测试。

我不知道你想要什么“最后的房间”,但似乎你想要完成当你到达“试验场”。我在你的程序末尾看到了一些代码,当你的用户到达“试验场”,然后你想退出的时候,我猜这就是你想要运行的代码。所以你可以这么做。将当前位置设置为新房间后,检查是否位于最终位置,如果是,执行结束逻辑并退出循环:

代码语言:javascript
复制
if command in currentRoom:
    currentRoom = rooms[currentRoom[command]]
    if currentRoom in ['Proving Grounds']:
        if len(carrying) == 6:
            print('You collected all of the items, and defeated the minotaur!')
        else:
            print('It looks like you have not found everything, you have been defeated!')
        break

注意,我将carrying中有多少项的测试从使用in改为使用==in用于对列表进行测试。如果您正在针对一个特定的值进行测试,如本例所示,请使用==

您需要删除代码底部的代码,从while行开始。当您将代码移到代码的其他位置时,不仅不需要代码,而且当您到达“试验场”时,正如所写的那样,while是一个无限循环。想想看.你要一直循环,直到现在的位置不是“试验场”,但是循环中的任何东西都不会改变当前的位置。所以你会一而再、再而三地打印同样的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71187965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档