基于文本的游戏。主功能
Text based game. main function
所以我是个白痴,几乎在 main 函数之外编写了 mina 代码。我仍然是新手,正在学习,所以当我将代码移入 main 函数时,我失去了对已定义变量的访问权限。我的位置和库存因为它们是在主要功能中定义的,所以当我尝试调用 status() 时无法正常工作。但是当我将库存和位置移到主要功能之外时,库存无法访问房间。谁能指出我解决这个问题的正确方向。谢谢
def instructions():
print("\n Super Humans Adventure Game")
print("-------------Instructions-------------")
print("Collect the 6 items within the rooms to")
print("defeat the scientist and win.")
print("To move type: North, South, West, or East")
print('-' * 38)
def status():
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
#calls instructions
instructions()
def main():
rooms = {
'Main Entrance': {
'name': 'Main Entrance',
'item': [],
'East': 'Dining Room'},
'Dining Room': {
'name': 'Dining Room',
'item': ['potion'],
'West': 'Main Entrance',
'North': 'Laboratory',
'East': 'Break Room',
'South': 'Holding Cells'},
'Laboratory': {
'name': 'Laboratory',
'item': ['shield'],
'East': 'Office',
'South': 'Dining Room'},
'Office': {
'name': 'Office',
'item': [],
'West': 'Laboratory'}, # Villian
'Break Room': {
'name': 'Break Room',
'item': ['key'],
'West': 'Dining Room',
'East': 'Bathroom'},
'Bathroom': {
'name': 'Bathroom',
'item': ['suit'],
'West': 'Break Room'},
'Holding Cells': {
'name': 'Holding Cells',
'item': [],
'East': 'Armory',
'North': 'Dining Room'},
'Armory': {
'name': 'Armory',
'item': ['weapon'],
'North': 'Power Room',
'West': 'Holding Cells'},
'Power Room': {
'name': 'Power Room',
'item': ['power'],
'South': 'Armory'}
}
location = rooms['Holding Cells']
directions = ['North', 'East', 'South', 'West']
inventory = []
while True:
if location == rooms['Office'] and len(inventory) > 5:
print('')
print('You have defeated the scientist and escaped! Congratulations')
elif location == rooms['Office'] and len(inventory) < 6:
print('')
print('You have reached the scientist but you are too weak!')
print('You have died')
break
# shows current location
status()
# user input
cmd = input('Enter move: ').capitalize().strip()
if cmd in directions:
if cmd in location:
location = rooms[location[cmd]]
print('You successfully moved locations.')
else:
print('')
print('You can not go that way!')
# quit game
elif cmd.lower in ('q', 'quit'):
print('You have quit the game, thanks for playing!')
break
# get item
elif cmd.lower().split()[0] == 'get':
item = cmd.lower().split()[1]
if item in location['item']:
location['item'].remove(item)
inventory.append(item)
else:
print('There is no item here.')
else:
print('That is not a valid input')
main()
你可以
- 在 Main 中创建一个名为“仓库”的对象,每个项目及其库存都可以在仓库对象中维护,因此您不需要它是一个子例程,and/or
- 您可以在主程序或其他子程序之外创建“全局变量”以在代码启动时定义,或者
- 您可以将变量传递给您的子例程(从 Main),并在子例程完成时将 return 值传回给 main。
将它们作为参数传递实际上是解决方案。有人提到了这一点,这正是我所需要的。这是更新代码
def status(location, inventory, rooms):
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
然后在调用函数的时候我只填了参数就可以了
# shows current location
status(location, inventory, rooms)
所以我是个白痴,几乎在 main 函数之外编写了 mina 代码。我仍然是新手,正在学习,所以当我将代码移入 main 函数时,我失去了对已定义变量的访问权限。我的位置和库存因为它们是在主要功能中定义的,所以当我尝试调用 status() 时无法正常工作。但是当我将库存和位置移到主要功能之外时,库存无法访问房间。谁能指出我解决这个问题的正确方向。谢谢
def instructions():
print("\n Super Humans Adventure Game")
print("-------------Instructions-------------")
print("Collect the 6 items within the rooms to")
print("defeat the scientist and win.")
print("To move type: North, South, West, or East")
print('-' * 38)
def status():
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
#calls instructions
instructions()
def main():
rooms = {
'Main Entrance': {
'name': 'Main Entrance',
'item': [],
'East': 'Dining Room'},
'Dining Room': {
'name': 'Dining Room',
'item': ['potion'],
'West': 'Main Entrance',
'North': 'Laboratory',
'East': 'Break Room',
'South': 'Holding Cells'},
'Laboratory': {
'name': 'Laboratory',
'item': ['shield'],
'East': 'Office',
'South': 'Dining Room'},
'Office': {
'name': 'Office',
'item': [],
'West': 'Laboratory'}, # Villian
'Break Room': {
'name': 'Break Room',
'item': ['key'],
'West': 'Dining Room',
'East': 'Bathroom'},
'Bathroom': {
'name': 'Bathroom',
'item': ['suit'],
'West': 'Break Room'},
'Holding Cells': {
'name': 'Holding Cells',
'item': [],
'East': 'Armory',
'North': 'Dining Room'},
'Armory': {
'name': 'Armory',
'item': ['weapon'],
'North': 'Power Room',
'West': 'Holding Cells'},
'Power Room': {
'name': 'Power Room',
'item': ['power'],
'South': 'Armory'}
}
location = rooms['Holding Cells']
directions = ['North', 'East', 'South', 'West']
inventory = []
while True:
if location == rooms['Office'] and len(inventory) > 5:
print('')
print('You have defeated the scientist and escaped! Congratulations')
elif location == rooms['Office'] and len(inventory) < 6:
print('')
print('You have reached the scientist but you are too weak!')
print('You have died')
break
# shows current location
status()
# user input
cmd = input('Enter move: ').capitalize().strip()
if cmd in directions:
if cmd in location:
location = rooms[location[cmd]]
print('You successfully moved locations.')
else:
print('')
print('You can not go that way!')
# quit game
elif cmd.lower in ('q', 'quit'):
print('You have quit the game, thanks for playing!')
break
# get item
elif cmd.lower().split()[0] == 'get':
item = cmd.lower().split()[1]
if item in location['item']:
location['item'].remove(item)
inventory.append(item)
else:
print('There is no item here.')
else:
print('That is not a valid input')
main()
你可以
- 在 Main 中创建一个名为“仓库”的对象,每个项目及其库存都可以在仓库对象中维护,因此您不需要它是一个子例程,and/or
- 您可以在主程序或其他子程序之外创建“全局变量”以在代码启动时定义,或者
- 您可以将变量传递给您的子例程(从 Main),并在子例程完成时将 return 值传回给 main。
将它们作为参数传递实际上是解决方案。有人提到了这一点,这正是我所需要的。这是更新代码
def status(location, inventory, rooms):
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
然后在调用函数的时候我只填了参数就可以了
# shows current location
status(location, inventory, rooms)