【发布时间】:2022-01-13 01:49:48
【问题描述】:
我是一个初学者,尝试编写一个简单的基于文本的游戏,用户在不同的房间之间移动,从每个房间中挑选一个物品,并以遇到老板结束。事情似乎进展顺利,除非我尝试在我的主函数中调用函数 status()。我不断收到未定义变量的错误。任何帮助将不胜感激。
# Create a function for the opening menu
def show_menu():
print('You are about to play a text based game.\n')
print('The goal of the game is to collect all seven items before encountering the ghost.\n')
print('These items contain information about the ghost that can finally put them to rest.\n')
print('If you approach the ghost without all of the items you will be possessed and the game will end.\n')
print('The commands are: move North, move South, move East, move West, menu, and quit.\n')
print('When you find an item, to add it to your inventory enter: get Item')
# Create a function that outputs current room, current room inventory and users current inventory
def status():
print('You are currently in the: {}.'.format(current_room))
print('The item currently in this room is {}.'.format(rooms[current_room]['item]']))
print('Your current inventory is: {}.'.format(inventory))
control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
return control
def movement_error():
print('You look around and realize that you wanted to move through a wall, try a different direction.')
control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
return control
def main():
# Create Dictionary linking rooms and items
rooms = {
'Foyer': {'East': 'Office', 'South': 'Cafeteria', 'item': 'Newspaper Article'},
'Office': {'West': 'Foyer', 'item': 'Student Report'},
'Cafeteria': {'North': 'Foyer', 'West': 'English Classroom', 'South': 'Math Classroom', 'East': 'Band Room',
'item': 'Crumpled Paper'},
'English Classroom': {'East': 'Cafeteria', 'item': 'Student Journal'},
'Math Classroom': {'East': 'Girls Bathroom', 'item': 'Itinerary'},
'Girls Bathroom': {'West': 'Math Classroom', 'item': 'Notebook'},
'Band Room': {'East': 'Cafeteria', 'North': 'Basement', 'item': 'Picture'},
'Basement': {'South': 'Band Room'}, # Ghost Room
}
directions = ['North', 'East', 'South', 'West']
control = ''
current_room = 'Foyer'
while control != 'exit':
inventory = []
while current_room != ['Basement']:
status()
if control in directions and contol in current_room:
current_room = rooms[current_room][control]
【问题讨论】:
标签: python variables function-call