基于文本的游戏。主功能
Posted
技术标签:
【中文标题】基于文本的游戏。主功能【英文标题】:Text based game. main function 【发布时间】:2022-01-24 01:09:30 【问题描述】:所以我是个白痴,几乎在 main 函数之外编写了 mina 代码。我还是新手,还在学习,所以当我将代码移动到主函数时,我无法访问已定义的变量。我的位置和库存,因为它们是在主函数中定义的,所以当我尝试调用 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()
【问题讨论】:
您应该将它们作为参数传递给函数。 啊啊啊是的。完全忘记了 ab 参数大声笑。谢谢,这解决了我的问题。 【参考方案1】:将它们作为参数传递实际上是解决方案。有人提到了这一点,这正是我所需要的。这是更新代码
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)
【讨论】:
【参考方案2】:你可以
在 Main 中创建一个名为“仓库”的 OBJECT,每个项目及其库存都可以在仓库对象中维护,因此您不需要将其作为子例程,和/或 您可以在 main 或其他子例程之外创建“全局变量”,以便在代码启动时定义,或者 您可以将变量传递给子例程(从 Main),并在子例程完成后将返回值传回给 main。【讨论】:
感谢您的建议,但参数是我最容易理解的解决方案。因为我正处于学习 python 的早期阶段。以上是关于基于文本的游戏。主功能的主要内容,如果未能解决你的问题,请参考以下文章