当前位置: 首页 > 知识库问答 >
问题:

基于文本的游戏项目删除[重复]

黄逸清
2023-03-14

在我的大学课程中,我们必须创建一个基于文本的游戏,在这个游戏中,你可以进出房间收集物品。我仍在尝试掌握python和一般编码的诀窍,因此我一直在努力完成这个项目。在大多数情况下,我的代码可以正常工作,但我可以多次添加一个项目。如何将其添加到库存中,然后从文件室中删除,以防止用户多次添加?

import time
#Shows rooms available to move and items in each room
rooms = {
        'Foyer': {'North' : 'Dining Hall', 'East' : 'Admin Office' },
        'Admin Office': {'West': 'Foyer', 'item': 'Key', },
        'Dining Hall': {'North' : 'Exam Room 1', 'South' : 'Foyer', 'East' : 'Bathroom', 'West' : 'Kitchen', 'item' : 'Flashlight', },
        'Bathroom': {'West': 'Dining Hall', 'item': 'Proton Pack' },
        'Kitchen': {'East': 'Dining Hall', 'item': 'Satchel' },
        'Exam Room 1': {'South' : 'Dining Hall', 'East' : 'Isolation Room', 'West': 'Patient Room 1', 'item' : 'Journal' },
        'Patient Room 1': {'East': 'Exam Room 1', 'item': 'Ghost Capsule' },
        'Isolation Room': {'West': 'Exam Room 1', 'item': 'Dr. Finkelstein' },
    }
#Game Instructions
def game_instructions():
   print('Collect 6 items to win the game, or be defeated by the ghost of Dr. Finkelstein.')
   print('Move Commands: North , South , East , or West.')
   print("Add to Inventory: get 'item name'")

print('You’ve been tasked with getting rid of the ghost of Dr. Finkelstein in the old insane asylum up on Academy Hill.')
time.sleep(1)
print('The ghost has taken over the asylum for three centuries and has begun opening a portal for more ghost to pass through.')
time.sleep(1)
print('During your last visit to the asylum, you dropped multiple items during a quick escape.')
time.sleep(1)
print('You need to go back into the asylum, collect all the items, and capture his ghost to close the portal!')
time.sleep(1)
print('The fate of the town is in your hands')
time.sleep(1)
game_instructions()
time.sleep(1)

current_room = 'Foyer' # starts player in the Foyer
inventory = [] # Adds an inventory, starts empty

def get_new_room(current_room, direction):
    new_room = current_room
    for i in rooms:
        if i == current_room:
            if direction in rooms[i]:
                new_room = rooms[i][direction]
    return new_room

def get_item(current_room):
    if 'item' in rooms[current_room]:
        return rooms[current_room]['item']
    else:
        return 'This room has no item!'

while (current_room): # gameplay loop
    print('\n-------------------------')
    print('You are in', current_room)  # tells player what room they are in.
    print('Inventory:', inventory)  # shows player their inventory
    item = get_item(current_room)  # defines item as get item
    print('Item:', item)  # tells the player what item they have found
    print('-------------------------')
    if 'item' == 'Dr. Finkelstein':
        if len(inventory) == 6:
            print('Congratulations!! You have collected all the necessary items and have successfully captured the ghost of Dr. Finkelstein and saved the town!')
        else:
            print('You did not have all the necessary items to defeat the ghost of Dr. Finkelstein, you are defeated.') # notifies player game has ended.
        break  # ends game
    direction = input('Enter direction you would like to move. >>')
    direction = direction.capitalize() # Capitalizes the players input to match what is in the dictionary.

    if (direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West'):
        new_room = get_new_room(current_room, direction)
        if new_room == current_room:
            print('That is a wall not an exit. Try Again!')
        else:
            current_room = new_room
    elif direction == str('get ' + item ).capitalize():
        if item in inventory:
            print('You have already collected this item. Move to another room!')
        else:
            inventory.append(item)
    else:
        print('Not a valid direction!')

共有2个答案

宰父冠玉
2023-03-14

使用popcurrent_room中删除项目。

def get_item(current_room):
    return rooms[current_room].pop('item', 'This room has no item!')

pop的第二个参数是回退值,在我看来,在这种情况下,最好返回None,而不是字符串。

仇飞鹏
2023-03-14

可以使用del语句从字典中删除键/值对:

del rooms[current_room]['item']

但是有一个快捷方式,允许同时删除和返回项目。这是一种叫做dict.pop的字典方法。使用它,您可以按如下方式重写get_item():

def get_item(current_room):
    return rooms[current_room].pop('item', 'This room has no item!')
 类似资料:
  • 我一直在做一个类似Zork的基于文本的冒险游戏,作为一个个人项目来自学python。一般来说,我对编码是新手,所以我对基本原理不太清楚。 在我的游戏中,我已经达到了可以成功导航我创建的地图,并可以自由添加新房间/物品的地步。我目前正在努力使“库存”系统正常工作。 目前,我的Room类中有一本名为“roominv”的字典,它作为每个特定房间的目录。我在player类中还有一个名为“bag”的字典,它

  • 我对编程游戏相当陌生;我已经3/4完成了,我有一个关于我制作的一个基于文本的小游戏的问题。。。所以在这个游戏中,我的

  • 问题内容: 我使用以下方式列出了重复项: 现在,如何删除除一条消息以外的所有消息(我正在尝试删除重复项,以便可以在上应用唯一索引)。 问题答案: 使用和分配行号,以便删除重复对中除一个以外的所有行。

  • 所以我是Java编码的新手,我对C#有很好的经验,我知道它们非常相似。我目前正在通过创建一个文本冒险游戏来处理Java,游戏使用案例(案例1、案例2、默认等),目前我正在开发一个保存和加载功能,但我不知道如何保存一个使用案例来进一步编码的分支游戏,有人有什么想法吗?

  • 在我的游戏中有几个类我写过,包括房间,灯,胸,爪哇,玩家,钥匙和地图。这些都经过了测试,并且是正确的,所以现在我正在编写我的adventure类,它是程序的驱动程序。我需要设置球员的房间位置[0][0],但我不知道怎么做。这是我到目前为止在我的房间和冒险课。

  • 我试图制作一个简单的基于文本的游戏,在这个游戏中,你在一个由“#”组成的网格周围移动“@”,并试图找到出口。我已经改变了代码,使我更容易使网格更大或更小,而无需添加或删除大量代码,它一直给我这样的输出: 我不知道这是怎么回事!只有一个“@”应该出现:(我只是python的新手,所以如果您有任何改进的建议,请不要犹豫,然后发布它们!提前感谢,