【问题标题】:Andventure game problems, moving between rooms Python冒险游戏问题,在房间之间移动 Python
【发布时间】:2013-11-13 00:36:40
【问题描述】:

到目前为止,我可以到达所有房间,除了从战斗到楼梯。我得到了要显示的楼梯的描述,但是当我尝试给它下一个方向时,什么都没有被识别出来。

代码如下:

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.")  

等等

【问题讨论】:

  • 您在此处复制的代码缩进错误。请检查原件中的缩进。子句elif room == "Stairs": 似乎有 5 个空格而不是 4 个,这可能是它不起作用的原因(尽管如果是这样,那么程序应该会抛出错误)
  • 粘贴的程序会在到达之前引发IndentationError,因为Inventory = createInventory()缩进了1个空格……

标签: python text-based


【解决方案1】:

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

 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":

...但您在其他很多地方也会出错,您必须将它们全部修复。

【讨论】:

    【解决方案2】:

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

    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,以处理之前未返回的所有剩余情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多