所以我正在为我的班级制作一个游戏。它是基于文本的,使用各种房间和物品来完成游戏。我已经找到了一种在房间之间移动和获取物品的方法。我还有一个清单功能,可以从清单中删除物品。
我遇到了麻烦,因为房间没有条件验证。如果你在开始房间'spawn room',那么唯一有效的方向是南。当我输入北它应该给予一个语句,并重置提示,但相反的代码与KeyError中断:“North”。错误语句位于第89行和第56行。
另一个小问题是我的库存系统。如果你不输入“Set Down”,它仍然会提示下一个if语句。
我试过让方向不设置为特定房间的方向,但似乎不起作用。例如第92行
对于清单,我还尝试了不同的缩进方式,这样第二个print语句只有在用户键入'SetDown'时才出现,但是if和while语句不起作用。
import sys
def show_instructions():
#print a main menu and the commands
print("Batman Text Based Game")
print("The Riddler has tied Batman up. You must solve the riddles to win the fight against him.")
print("Move commands: South, North, East, West, Confirm")
#Rooms no longer contain items because I want the player to be able to drop the items at any point in time.
rooms = {
'Spawn Room' : { 'South' : 'top of the Hallway'},
'top of the Hallway' : { 'East' : 'Riddle One', 'West' : 'Riddle Two', 'South' : 'Hallway Two'},
'Hallway Two' : { 'East' : 'Riddle Three', 'West' : 'Riddle Four', 'North' : 'top of the Hallway', 'South' : 'Hallway Three'},
'Hallway Three' : { 'East' : 'Riddle Five', 'West' : 'Riddle Six', 'North' : 'Hallway Two', 'South' : 'Hallway Bottom'},
'Hallway Bottom' : { 'East' : 'Riddle Seven', 'West' : 'Riddle Eight', 'North' : 'Hallway Three', 'South' : 'Boss Room'},
'Riddle One' : { 'West' : 'top of the Hallway'},
'Riddle Two' : { 'East' : 'top of the Hallway'},
'Riddle Three' : { 'West' : 'Hallway Two'},
'Riddle Four' : { 'East' : 'Hallway Two'},
'Riddle Five' : { 'West' : 'Hallway Three'},
'Riddle Six' : { 'East' : 'Hallway Three'},
'Riddle Seven' : { 'West' : 'Hallway Bottom'},
'Riddle Eight' : { 'East' : 'Hallway Bottom'},
'Boss Room': { 'item' : 'The Riddler'} #villain
}
R1 = 'R1' #False
R2 = 'R2' #True
R3 = 'R3' #True
R4 = 'R4' #False
R5 = 'R5' #False
R6 = 'R6' #True
R7 = 'R7' #False
R8 = 'R8' #False
#Functions for directional inputs
def moveroom1(direction, currentroom="Spawn Room"):
print('Batman leaves the', currentroom)
if direction == 'South':
return rooms[currentroom]['South']
def moveroom2(direction, currentroom="Spawn Room"):
print('Batman leaves the', currentroom)
if direction == 'East':
return rooms[currentroom]['East']
def moveroom3(direction, currentroom="Spawn Room"):
print('Batman leaves the', currentroom)
if direction == 'West':
return rooms[currentroom]['West']
def moveroom4(direction, currentroom="Spawn Room"):
print('Batman leaves the', currentroom)
if direction == 'North':
return rooms[currentroom]['North']
show_instructions() #Introduction and instructions
print()
currentroom = 'Spawn Room' #Set the starting room
inventory = [] #Blank inventory for the player to pick up items
direction = '' #input for room change
while direction != 'exit': # What directions are available to travel?
possible_moves = rooms[currentroom].keys()
print('Possible moves:', *possible_moves)
print()
# Prompt a direction, process the answer and inform
direction = input('Move which direction? ').strip()
print('You entered: ', direction)
print()
if direction == 'South':
currentroom = moveroom1('South', currentroom)
#print('You are in the', currentroom)
if direction == 'East':
currentroom = moveroom2('East', currentroom)
#print('You are in the', currentroom)
if direction == 'West':
currentroom = moveroom3('West', currentroom)
#print('You are in the', currentroom)
if direction == 'North':
currentroom = moveroom4('North', currentroom)
#print('You are in the', currentroom)
if direction != 'North' or 'West' or 'East' or 'South' or 'Inventory' or 'Confirm':
print('That is not a valid command, try again!')
#Riddles and items are appended in this section. Doesn't break loop
if currentroom == 'Riddle One':
print('Riddle One here')
if 'R1' not in inventory:
inventory.append('R1')
if currentroom == 'Riddle Two':
print('Insert Riddle two here')
if 'R2' not in inventory:
inventory.append('R2')
if currentroom == 'Riddle Three':
print('Insert Riddle three here')
if 'R3' not in inventory:
inventory.append('R3')
if currentroom == 'Riddle Four':
print('Insert Riddle four here')
if 'R4' not in inventory:
inventory.append('R4')
if currentroom == 'Riddle Five':
print('Insert Riddle five here')
if 'R5' not in inventory:
inventory.append('R5')
if currentroom == 'Riddle Six':
print('Insert Riddle six here')
if 'R6' not in inventory:
inventory.append('R6')
if currentroom == 'Riddle Seven':
print('Insert Riddle seven here')
if 'R7' not in inventory:
inventory.append('R7')
if currentroom == 'Riddle Eight':
print('Insert Riddle eight here')
if 'R8' not in inventory:
inventory.append('R8')
#This section is prompted by the player inputting Inventory as a direction
if direction == 'Inventory':
print(inventory)
itemdrop = input('Set Down to set down some items, or enter anything to exit inventory.') #Creates a new input for the decision to drop an item or not
if itemdrop != 'Set Down' or 'set down':
print('Are you sure you want to exit the inventory?') #I'm not sure what happens here but there are two inputs that are required to get back into the direction loop
if itemdrop == 'Set Down' or 'set down':
tyty = input()
if tyty == 'R1':
inventory.remove('R1')
print('You dropped R1')
if tyty == 'R2':
inventory.remove('R2')
print('You dropped R2')
if tyty == 'R3':
inventory.remove('R3')
print('You dropped R3')
if tyty == 'R4':
inventory.remove('R4')
print('You dropped R4')
if tyty == 'R5':
inventory.remove('R5')
print('You dropped R5')
if tyty == 'R6':
inventory.remove('R6')
print('You dropped R6')
if tyty == 'R7':
inventory.remove('R7')
print('You dropped R7')
if tyty == 'R8':
inventory.remove('R8')
print('You dropped R8')
#while currentroom == 'Boss Room':
#if inventory != #the right items:
#print('Batman killed The Riddler with excessive force. You lose')
#if inventory == #any order of the right combination of things:
#print('Batman fights the Riddler to victory')
#Game loop
if direction == 'exit':
print('You have quit the game')
sys.exit()
1条答案
按热度按时间6mzjoqzu1#
首先,你只需要一个
moveroom()
函数,如下所示:它可以像这样使用: