冒险游戏问题,在房间Python之间移动

时间:2013-11-13 00:36:41

标签: python text-based

到目前为止,我能够到达所有房间,除了从战斗到楼梯。我得到了楼梯的描述,但是当我尝试给它下一个方向时,没有人认出来。

继承人代码:

def playGame():
   location = "Porch"
   showIntroduction()
   while not (location == "Exit"):
       showRoom(location)
       direction = requestString("Which direction")
       printNow("You typed: " + direction)
       location = pickRoom(direction, location)

# The instructions for the game      
def showIntroduction():
  printNow ("Welcome to the Adventure House!")
  printNow ("In each room, you will be told which directions you can go.")
  printNow ("You can move north, south, east, or west by typing")
  printNow ("that direction into the box.")
  printNow ("you can also pick up items that you will need on your journey")
  printNow ("They will present themselves in the text, to pick them up simplily type    there name")
  printNow ("Type help to replay this introduction.")
  printNow ("Type quit or exit to end the program.")

#This is called in the global defination of Inventory , makes and array
def createInventory():
   inv = range(11)
   inv[10] = "made"
   return inv

 # Main Inventory
 Inventory = createInventory()

 # Populating given inventory aray with items to be used throughout the game.
 def createItems(inv):
   items = inv
   items[1] = "Axe"
   items[2] = "Gas"
   items[3] = "keys"
   items[4] = "gun"
   items[5] = "note"
   items[9] = "full" 

# Check if the item passed to it is still in the inventory array
def checkItems(item):
    items = Inventory
    for i in range(len(items)):
        if items[i] == item:
            return "yes"
        return "no"



 #Put an item in the inventory (Key words : grab /  item name)
 def stockInventory(item):
    inv = Inventory
    for i in range (11):
       if inv[i] == 0:
          inv[i] = item
          break
    return inv

 #Check for an item to make text appear.
 def checkInventory(item):
    inv = Inventory
    for i in range(0, 11):
        if item == inv[i]:
           return "yes"
        return "no"


# A helper function that calls specific room description 
# functions based on the value of the parameter room 
def showRoom(room):
   printNow("============")
   if room == "Porch":
      showPorch()
   elif room == "Entryway":
      showEntryway()
   elif room == "Kitchen":
      showKitchen()
   elif room == "LivingRoom":
      showLR()
   elif room == "DiningRoom":
      showDR()
   elif room == "Stairs" :
      showStairs()
   elif room == "Nurcery":
      showNurcery()
   elif room == "Hole":
      showHoleFalling()
   elif room == "Basement":
      showBasement()
  elif room == "BasementW":
    showBasementW()
  elif room == "BasementN":
     showBasementN()
  elif room == "BasementS":
     showBasementS()
  elif room == "BasementE":
     showBasementE()
  elif room == "frontYardFight":
      showFight()
  elif room == "Car":
     showCar()
  elif room == "win":
     Winning()
  elif room == "Fight":
     showFight1()
  # Add rooms above this line!
  else:  #This clause should never execute
       printNow("I don't recognize the room")

  # This function specifies what room is entered based on the current room
  # and the direction requested by the user.
def pickRoom (direction, room):
if (direction == "quit"  or direction == "exit"):
    printNow("Goodbye!")
    return "Exit"
elif direction == "help" or direction == "Help":
    showIntroduction()
    return room

elif room == "Porch":
    if direction == "north":
       return "Entryway"
    elif direction == "truck":
        printNow ("The truck is out of gas and your keys seem to be missing. you return to the porch")
    else:
       printNow("There's no way to go that direction.")
       return room

elif room == "Entryway":
    if direction == "north":
       return "Kitchen"
    elif direction == "south":
       return "Porch"
    elif direction == "east":
       return "LivingRoom"
    elif direction == "read":
        printNow ("This house will be your doom")
    else:
       printNow ("The is not the correct word")
       return room

elif room == "Kitchen":
    if direction == "south":
       return "Entryway"
    elif direction == "east":
       return "DiningRoom"
    elif direction == "west":
        answer = checkInventory("Axe")
        if answer == "yes":
            printNow (" You feel the urge to get shinning  on this door, you decide to swing your axe and break it")
            return "Fight"
        else:
            printNow (" the door is locked, find a a way to open it.")
            return room




elif room == "Fight":
  answer = checkInventory("Axe")
  if direction ==  "swing":
       printNow ("You landed your blow! it's down!")
       return "Stairs"
  else:
       printNow(" wrong keyword ")
       return room
  return "Stairs"

elif room == "LivingRoom":
    if direction == "north":
       return "DiningRoom"
    elif direction == "west":
       return "Entryway"
    elif direction == ("axe" or "Axe"):
       stockInventory("Axe")
       printNow(" You grabbed the Axe ")
       return room
    else:
       printNow ("You have the wrong keyword.")
       return room

elif room == "DiningRoom":
    if direction == "south":
       return "LivingRoom"
    elif direction == "west":
       return "Kitchen"
    elif direction == ("Gas" or "gas"):
       stockInventory("Gas")
       printNow (" you grabbed the gas ")
    else:
       printNow ("You have the wrong keyword.")
       return room

elif room == "Stairs":
     if direction == "up":
        return "TopHall"
     elif direction == ("keys" or "Keys"):
        stockInventory("Keys")
        printNow (" You got the Keys!")
     else:
        printNow ("You have the wrong keyword.")
        return room

elif room == "TopHall":
    if direction == "north":
       return "Hole"
    elif direction == "west":
        return "Nurcery"
    else:
        printNow(" You have the wrong keyword. ")
        return room

elif room == "Nurcery":
    if direction == "west":
       return "TopHall"
    else:
       printNow(" You have the wrong keyword. ")
       return room

elif room == "Hole":
   return "Basement"

elif room == "Basement":
   if direction == "west":
      return "BasementW"
   elif direction == "north":
      return "BasementN"
   elif direction == "south":
      return "BasementS"
   elif direction == "east":
      return "BasementE"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementW":
   if direction == "east":
      return "Basement"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementN":
   if direction == "south":
      return "Basement"
   elif direction == "wall":
      return "FrontYard"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "basementS":
   if direction == "north":
     return "Basement"
   elif direction == ("gun" or "Gun"):
     stockInventory("gun") 
     printNow (" you got the Gun!")
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "BasementE":
   if direction == "west":
     return "Basement"
   else:
     printNow(" That is the wrong word ")
     return room

elif room == "frontYardFight":
   if direction == "shoot":
       answer = checkInventory("gun")
       if answer == "yes" or "Yes":
           printNow(" You shoot the monster straight in the head" )
           return "Car"
       else:
          return "quit"
   else:
     printNow(" That is the wrong word ")
     return room


elif room == "Car":
   answer1 = checkInventory("Keys")
   answer2 = checkInventory("Gas")
   if answer1 and answer2 == "Yes" or "yes":
       return "win"
   else:
       printNow("your missing something")
       return "Porch"




#add rooms above this line!
else:  # this clause should never execute!
    printNow("An undefined room has been detected.")
    return "Porch"


######  a group of functions that print room descriptions    

def showPorch():
   printNow("You wake from a deep slumber, you're lieing on the porch of an abandonded house")
   printNow("The windows are broken.  It's a dark and stormy night")
   printNow("Your truck is sitting in the parking lot, when you examine it, it is out of gas and your keys are gone")
   printNow("You can go north into the house if you dare.")  

等等

2 个答案:

答案 0 :(得分:0)

我在这里猜测,但我猜这个错误是你担心的:

 elif direction == ("keys" or "Keys"):

这不符合你的想法。 ("keys" or "Keys")"keys"相同。因此,这与elif direction == "keys":相同。

你想要其中一个:

elif direction == "keys" or direction == "Keys":
elif direction in ("keys", "Keys"):
elif direction.lower() == "keys":

或者,更好的是,更改playGame以致电pickRoom(direction.lower(), location),然后在整个pickRoom中进行所有操作,您只需与小写字符串进行比较。

你多次这样做,就像这里一样:

elif direction == "help" or direction == "Help":

...但你在其他各个地方也弄错了,你必须修复所有这些。

答案 1 :(得分:0)

一个问题是在函数pickRoom中,并非长if...elif...else的所有分支都返回一个值。在下一节中,如果用户键入“键”,则不会返回任何空间,程序应输出“已检测到未定义的房间”。在随后的循环中。

elif room == "Stairs":
    if direction == "up":
        return "TopHall"
    elif direction == ("keys" or "Keys"):
        stockInventory("Keys")
        printNow (" You got the Keys!")
    else:
        printNow ("You have the wrong keyword.")
        return room

处理此问题的一种简单方法是在函数末尾添加return room,以处理之前未返回的所有剩余案例。