Python文字冒险游戏,结局循环
Python text adventure game, ending loop
我坚持要结束我的游戏并打破循环。我尝试过重新措辞,将其放在不同的位置等。游戏结束参数是,如果您到达佛罗里达州 并且 所有六个项目您都赢了,否则您输了。相反,我可以到达最后一个房间,它要么继续提示输入,要么宣布你已经赢得了比赛,即使你没有。
print("\nMovement commands : North, South, East, or West")
print("Add to inventory: Get item\n")
introduction() # I just cut my long-winded intro. it works.
rooms = {
'House': {'north': 'Drug Store', 'south': 'Clinic', 'east': 'Kitchen', 'west': 'Craft Store'},
'Drug Store': {'south': 'House', 'east': 'Electronics Store', 'item': 'Hand Sanitizer'},
'Electronics Store': {'west': 'Drug Store', 'item': 'ANC Headphones'},
'Craft Store': {'east': 'House', 'item': 'A Mask'},
'Clinic': {'north': 'House', 'east': 'CDC', 'item': 'A Vaccine'},
'CDC': {'west': 'Clinic', 'item': 'Dr Fauci Candle'},
'Kitchen': {'west': 'House', 'north': 'State of Florida', 'item': 'Anti-viral Spray'},
'State of Florida': {'item': 'COVID-19'} # VILLAIN, final room
}
current_room = 'House' # location variable that will change as player moves
inventory = [] # empty list that will fill as you collect items
directions = ('north', 'south', 'east', 'west') # possible movements
item = ('hand sanitizer', 'anc headphones', 'a mask', 'a vaccine', 'dr fauci candle',
'anti-viral spray', 'covid-19')
while True:
print('\nYou are in the {}'.format(current_room)) # current game status
print('Inventory: {}'.format(inventory))
if 'item' not in rooms[current_room]:
pass
else:
print('You see {}'.format(rooms[current_room]['item']))
print('-' * 25)
command = input('Enter your move:\n').lower().strip()
我最初把我的游戏结束声明放在这里,但它只是
再次循环然后你永远被困在佛罗里达因为那里
在字典中没有附加空间
if command in directions:
if command in rooms[current_room]:
current_room = rooms[current_room][command]
if current_room in ['State of Florida']:
if len(inventory) == 6:
print('You have contracted COVID-19! G A M E O V E R')
else:
print('You have defeated COVID-19!')
print('Thank you for protecting your fellow teammates.')
break
无论库存要求如何,当你到达这里时你就赢得了比赛
(我试过这个:)
如果 len(库存) == 6 和 'item' == 'COVID-19'
打印赢
如果 len(库存) != 6 和 'item' == 'COVID-19'
打印丢失
else:
print('You cannot go that way, please stop running into brick walls.')
elif command == 'get item':
if 'item' in rooms[current_room]:
print('{} retrieved! Gold star!'.format(rooms[current_room]['item']))
inventory.append(rooms[current_room]['item'])
del rooms[current_room]['item']
else:
print('FOCUS! There is nothing in here.')
else:
print('What did you just call me? RUDE. Try again.')
在此代码中:
if current_room in ['State of Florida']:
if len(inventory) == 6:
print('You have contracted COVID-19! G A M E O V E R')
else:
print('You have defeated COVID-19!')
print('Thank you for protecting your fellow teammates.')
break
(另外:你应该只做 current_room == 'State of Florida'
而不是使用 in
)
您的 break
语句只会打破 innermost 循环。如果您只有一个循环(即 while True:
),那应该可以解决问题。由于它不起作用,我怀疑您在该循环中有一个循环。内部循环是被破坏的循环,之后执行包含该循环之后的任何内容(然后最终回到外部循环的开始)。
如果是这种情况,一个简单的解决方案是将所有这些代码放入一个函数中,这样您就可以 return
-- return
语句立即退出该函数,无论有多少您当前所在的嵌套循环级别。
另请注意,如果你的目的是 赢得 游戏,而不是 输 游戏,你应该反转你的 if
语句,例如使 if len(inventory) != 6
你输了,而不是相反(条件是 ==
而不是 !=
),就像你当前的代码
我坚持要结束我的游戏并打破循环。我尝试过重新措辞,将其放在不同的位置等。游戏结束参数是,如果您到达佛罗里达州 并且 所有六个项目您都赢了,否则您输了。相反,我可以到达最后一个房间,它要么继续提示输入,要么宣布你已经赢得了比赛,即使你没有。
print("\nMovement commands : North, South, East, or West")
print("Add to inventory: Get item\n")
introduction() # I just cut my long-winded intro. it works.
rooms = {
'House': {'north': 'Drug Store', 'south': 'Clinic', 'east': 'Kitchen', 'west': 'Craft Store'},
'Drug Store': {'south': 'House', 'east': 'Electronics Store', 'item': 'Hand Sanitizer'},
'Electronics Store': {'west': 'Drug Store', 'item': 'ANC Headphones'},
'Craft Store': {'east': 'House', 'item': 'A Mask'},
'Clinic': {'north': 'House', 'east': 'CDC', 'item': 'A Vaccine'},
'CDC': {'west': 'Clinic', 'item': 'Dr Fauci Candle'},
'Kitchen': {'west': 'House', 'north': 'State of Florida', 'item': 'Anti-viral Spray'},
'State of Florida': {'item': 'COVID-19'} # VILLAIN, final room
}
current_room = 'House' # location variable that will change as player moves
inventory = [] # empty list that will fill as you collect items
directions = ('north', 'south', 'east', 'west') # possible movements
item = ('hand sanitizer', 'anc headphones', 'a mask', 'a vaccine', 'dr fauci candle',
'anti-viral spray', 'covid-19')
while True:
print('\nYou are in the {}'.format(current_room)) # current game status
print('Inventory: {}'.format(inventory))
if 'item' not in rooms[current_room]:
pass
else:
print('You see {}'.format(rooms[current_room]['item']))
print('-' * 25)
command = input('Enter your move:\n').lower().strip()
我最初把我的游戏结束声明放在这里,但它只是 再次循环然后你永远被困在佛罗里达因为那里 在字典中没有附加空间
if command in directions:
if command in rooms[current_room]:
current_room = rooms[current_room][command]
if current_room in ['State of Florida']:
if len(inventory) == 6:
print('You have contracted COVID-19! G A M E O V E R')
else:
print('You have defeated COVID-19!')
print('Thank you for protecting your fellow teammates.')
break
无论库存要求如何,当你到达这里时你就赢得了比赛 (我试过这个:) 如果 len(库存) == 6 和 'item' == 'COVID-19' 打印赢 如果 len(库存) != 6 和 'item' == 'COVID-19' 打印丢失
else:
print('You cannot go that way, please stop running into brick walls.')
elif command == 'get item':
if 'item' in rooms[current_room]:
print('{} retrieved! Gold star!'.format(rooms[current_room]['item']))
inventory.append(rooms[current_room]['item'])
del rooms[current_room]['item']
else:
print('FOCUS! There is nothing in here.')
else:
print('What did you just call me? RUDE. Try again.')
在此代码中:
if current_room in ['State of Florida']:
if len(inventory) == 6:
print('You have contracted COVID-19! G A M E O V E R')
else:
print('You have defeated COVID-19!')
print('Thank you for protecting your fellow teammates.')
break
(另外:你应该只做 current_room == 'State of Florida'
而不是使用 in
)
您的 break
语句只会打破 innermost 循环。如果您只有一个循环(即 while True:
),那应该可以解决问题。由于它不起作用,我怀疑您在该循环中有一个循环。内部循环是被破坏的循环,之后执行包含该循环之后的任何内容(然后最终回到外部循环的开始)。
如果是这种情况,一个简单的解决方案是将所有这些代码放入一个函数中,这样您就可以 return
-- return
语句立即退出该函数,无论有多少您当前所在的嵌套循环级别。
另请注意,如果你的目的是 赢得 游戏,而不是 输 游戏,你应该反转你的 if
语句,例如使 if len(inventory) != 6
你输了,而不是相反(条件是 ==
而不是 !=
),就像你当前的代码