我正在尝试创建一个基于文本的游戏,在收集物品时必须避开怪物



房间的代码有效,但更改房间不起作用,即使获得了继续前进所需的部分,代码也会循环到开头。游戏结束时将与怪物的动作代码一起放入,这样当怪物和玩家在同一个房间时,游戏就结束了。怪物的移动将是随机的,所以游戏结束将是随机。

'Entrance Hall': {'name': 'Entrance Hall', 'exits': {'North': 'Great Hall', 'West': 'Bathroom'}},
'Great Hall': {'name': 'Great Hall', 'exits': {'West': 'Bedroom', 'East': 'Kitchen', 'North': 'Throne Room',
'South': 'Entance Hall'}},
'Bedroom': {'name': 'Bedroom', 'exits': {'North': 'Great Hall', 'East': 'Cellar', 'West': 'Bathroom'}},
'Cellar': {'name': 'Cellar', 'exits': {'West': 'Bedroom'}},
'Attic': {'name': 'Attice', 'exits': {'South': 'Throne Room'}},
'Kitchen': {'name': 'Kitchen', 'exits': {'North': 'Celler', 'West': 'Great Hall'}},
'Bathroom': {'name': 'Bathroom', 'exits': {'East': 'Bedroom', 'North': 'Entarnce Hall'}},
'Throne Room': {'name': 'Throne Room', 'exits': {'South': 'Great Hall', 'North': 'Attic'}},
}
def game():
"""starts the game"""
game_over = False
quit = False
answer = input('Start game? y/n?')
if answer.lower() == 'y':
print('Welcome to a world of unknown creatures and mazes')
print('You wake up in the Entrance hall of an Abandoned Castle')
current_room = rooms['Entrance Hall']
inventory = []
while current_room == rooms['Entrance Hall']:
print(current_room)
print('You see two doors one has an ornate gold trim around it the other is plain')
print('the door with the ornate trim leads to the Great Hall')
print('The other door leads to the bathroom')
answer = input('Which room do you want to check out first?')
if answer.lower() == 'Bathroom':#to change the current room to the bathroom
current_room = rooms['Bathroom']
print(current_room)
break
elif answer.lower() == 'Great Hall':#same as above but for the great hall
current_room = rooms['Great Hall']
break
elif answer.lower() == 'q' or 'Quit':#first idea for the quit
pass
while current_room == rooms['Bathroom']:
print(current_room)
if 'Key Fragment 1' is not in inventory:
print('You see the shine of an object')
answer = input('do you want to inspect it? Yes? No?')
if answer.lower() == 'yes' or 'y':
print('its a key fragment')
answer = input('Do you take it? Yes? No?')
if answer.lower() == 'yes' or 'y':
print('You have taken the key fragment')
inventory.append('Key Fragment 1')
print(inventory)
else:
print('You left the key fragment)
else:
print('You decided to leave the mysterious object alone')
while 'Key fragment 1' in inventory:
print('There are two doors')
print('The first is the door to the Entrance Hall')
print('The other goes to a Bedroom')
pass
else:
print('Maybe later')

game()

虽然我可以让一些代码工作,但我无法让游戏从入口大厅更改到其他房间,然后我需要发出退出命令。我只是在学习Python,这本书对我帮助不大。在测试代码时,它一直循环一部分,即使键入了"否"答案,也会强制给出"是"答案。最初的输入很好,但其余的则不然。调试屏幕上没有显示任何问题,我也不会崩溃。修复代码使其工作但不循环或强制回答的最佳方法是什么?此外,将其他房间添加到代码中的最佳方式是什么,以便在收集房间中的项目时可以移动到其他房间。最后,我需要代码方面的帮助,以便在收集完所有物品后结束游戏。

在分支检查中调用的方法str.lower()返回转换为小写的字符串,因此用于更衣室的所有检查都将始终为false。尝试更换

if answer.lower() == 'Bathroom'

带有

if answer.lower() == 'bathroom'

等等

最新更新