基于Python文本的游戏房间到房间的移动房子游戏

8xiog9wr  于 2023-04-22  发布在  Python
关注(0)|答案(1)|浏览(155)

需要帮助在调整我的代码,我无法移动到其他房间,我只能移动北和南没有其他房间正在识别.目标是能够在房间之间来回.正在使用的房间如下:阁楼,客厅,门厅,前廊,卧室,地下室,厨房,餐厅。

设计模式如下:(从阁楼开始)

阁楼(开始)-向南到客厅
客厅-向西到餐厅,向北回到阁楼,向东到门厅
门厅-向西到客厅,向北到卧室,向南到前门廊
卧室-向南到门厅
前廊-向北至门厅
餐厅-东到客厅,北到厨房,南到地下室
厨房-向南到餐厅
地下室-向北到餐厅
我的代码如下:

rooms = {
'Attic': {'South': ' Living Room'},

'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
      
'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
     
'Bedroom': {'South': 'Foyer'},
    
'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
     
'Kitchen': {'South': 'Dining Room'},
     
'Basement': {'North': 'Dining Room'},
    
'Front Porch': {'North': 'Foyer'},
    
}

def possible_moves():
  moves = rooms[location].keys()
  print("Option:", *moves)

location = 'Attic'

Direction = ''

while True:
  print('You are in the', location)

  possible_moves()

  direction = input("Which way would you like to go? ")

  if direction == 'Exit' or Direction == 'exit':

    print('Thank you for playing!')
    break
  elif direction == 'South' or direction == 'south':
      location = 'Living Room'
      print('You are in the', location)

      possible_moves()

      direction = input("Which way would you like to go? ")

      if direction == 'East' or direction == 'east':

        location = 'Foyer'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  elif direction == 'North' or direction == 'north':

        location = 'Attic'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  else:
   print('Invalid')
wfsdck30

wfsdck301#

你的代码应该看起来像这样。我注意到if语句是不必要的。

rooms = {
    'Attic': {'South': 'Living Room'},

    'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
  
    'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
 
    'Bedroom': {'South': 'Foyer'},
 
    'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
 
    'Kitchen': {'South': 'Dining Room'},
  
    'Basement': {'North': 'Dining Room'},

    'Front Porch': {'North': 'Foyer'},
}

def possible_moves():
    moves = ", ".join(list(rooms[location].keys()))
    print("Options: ", *moves)

location = 'Attic'

Direction = ''

while True:
    print('You are in the ', location)

    possible_moves()

    direction = input("Which way would you like to go?\n>> ")
    while direction.title() not in list(rooms[location].keys()):
        direction = input('invalid direction\n>> ')
    location = rooms[location][direction.title()]

紧凑而简单的解决方案。
如果你想要if语句,只需用下面的代码替换location == rooms[location][direction.title()]

if direction.title() == "South":
    location = rooms[location]["South"]
elif direction.title() == "North":
    location = rooms[location]["North"]
elif direction.title() == "East":
    location = rooms[location]["East"]
elif direction.title() == "West":
    location = rooms[location]["West"]

但我建议做前面的例子。

相关问题