【发布时间】:2021-04-13 00:34:54
【问题描述】:
我已经完成了大部分工作,但目前我的字典将房间链接在一起,然后将其中的项目链接在一起时遇到了问题。第 21 行(以 'Main Fair ground': {'South': 开头)太长且无效。以下是我到目前为止所做的代码,我将不胜感激任何帮助,因为我是 Python 新手并且有点挣扎。
# Sample function showing the goal of the game and move commands
def show_instructions():
# print a main menu and the commands
print("Killer Wolf Text Adventure Game")
print("Collect 6 items to win the game, or be eaten by the killer Wolf.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
def player_stat():
print("-" * 20)
print('You are in the {}'.format(currentRoom))
print("-" * 20)
def main():
pass
rooms = {
'Main Fair ground': {'South': 'Food stand', 'item' : '1LB of meat', 'North': 'Arcade', 'item' : 'real sword', 'East': 'Corn field', 'item' : 'wolf repellent', 'West': 'Candy shop', 'item' : 'candy'},
'Security': {'item': 'protective gear', 'East': 'Food stand'},
'Gift shop': {'item': 'map', 'West': 'Arcade'},
'Petting area': {'item': 'killer wolf', 'South': 'Corn field'} # villain
}
currentRoom = 'Main Fair ground'
player_move = ''
while currentRoom != 'Exit':
player_stat()
player_move = input('Enter your move:\n').lower()
if player_move in ['Exit', 'exit']:
currentRoom = 'Exit'
print('Play again soon')
continue
try:
currentRoom = rooms[currentRoom][player_move]
except Exception:
print("invalid move")
continue
if 'Petting' == currentRoom:
print("Kill the wolf")
我得到的错误信息是:
文件“wolf.py”,第 41 行 继续 ^ SyntaxError: 'continue' 在循环中不正确
这是一个来自龙文字游戏的字典示例,这基本上就是我正在做的,所以这是一个很好的例子。
#A dictionary linking a room to other rooms
#and linking one item for each room except the Start room (Great Hall) and the room containing the villain
rooms = {
'Great Hall' : { 'South' : 'Bedroom', 'North': 'Dungeon', 'East' : 'Kitchen', 'West' : 'Library' },
'Bedroom' : { 'North' : 'Great Hall', 'East' : 'Cellar', 'item' : 'Armor' },
'Cellar' : { 'West' : 'Bedroom', 'item' : 'Helmet' },
'Dining Room' : { 'South' : 'Kitchen', 'item' : 'Dragon' } #villain
}
玩家应输入命令在房间之间移动或从房间中获取物品(如果存在)。游戏循环应该继续循环,允许玩家移动到不同的房间并获取物品,直到玩家赢得或输掉游戏。请记住,玩家通过在遇到与恶棍的房间之前取回所有物品来赢得游戏。玩家在收集所有物品之前与恶棍一起移动到房间,从而输掉了游戏。请务必在两种可能的情况下为玩家提供输出:赢得和输掉游戏。
【问题讨论】:
-
你的问题到底是什么?
-
我需要帮助重新格式化房间和项目的字典。那个 python 代码是无效的,第 21 行太长了,我没有做对。请帮帮我。
-
python
dict中的每个键只能有 1 个。所以你是对的,你的rooms['Main Fair ground']嵌套字典中不能有 4 个值键入“项目”。您可以考虑使用与房间相同的主要方向键设置单独的dict项目。
标签: python scripting computer-science text-based