gpt4 book ai didi

python - 基于文本的游戏项目删除

转载 作者:行者123 更新时间:2023-12-05 02:28:22 26 4
gpt4 key购买 nike

<分区>

在我的大学类(class)中,我们必须创建一个基于文本的游戏,您可以在其中进出房间收集元素。我仍然在努力掌握 python 和一般编码的窍门,所以我一直在为这个项目而苦苦挣扎。在大多数情况下,除了我可以多次添加一个项目之外,我的代码是有效的。我如何将它添加到 list 中,然后将其从房间中删除,以防止用户多次添加它?

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!')

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com