房间和物品问题字典,基于 Python 文本的游戏

问题描述

我已经完成了大部分工作,但目前我的字典连接房间和其中的项目时遇到问题。第 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
}

玩家应输入命令在房间之间移动或从房间中获取物品(如果存在)。游戏循环应该继续循环,允许玩家移动到不同的房间并获取物品,直到玩家赢得或输掉游戏。请记住,玩家通过在遇到坏人的房间之前取回所有物品来赢得游戏。玩家在收集所有物品之前移动到与恶棍所在的房间,从而输掉游戏。确保为玩家提供两种可能情况的输出:赢得和输掉比赛。

解决方法

我不太明白你想做什么,但在示例 dict 中,看起来每个房间都有一个项目或多个路径依次成为房间字典中的键,值是另一个字典键是方向或项目,值是邻居房间的名称或项目的名称。 如果是这样,我认为唯一的问题是在 PID:%%a[1] 字典内部,正如我所说,每个内部字典最多只能有一个 "Main Fair Ground" 键,要做到这一点,您应该将 {{1} }} 仅在 "item" 字典中添加路线和添加新条目以添加项目,例如:

"Main Fair Ground"

如果你想要的是 1 磅的肉是食品摊内的物品,而拱廊内的真剑这可能应该可行。

,

字典必须具有唯一键,但您已在第一个房间中指定了多个“项目”键。您还指定了主要展览场地内的其他几个房间,这些房间在其他地方没有定义。例如。您指定“South”:“Food stand”,但您没有“Food stand”房间。您应该将它们拉出到单独的房间听写中并将项目移入其中,例如:

rooms = {
    "Main fair ground": {"South": "Food Stand","North": "Arcade","East": "Corn field","West": "Candy Shop"},"Food stand": {"item": "1LB of meat"},"Arcade": {"item": "real sword"},"Corn field": {"item": "wolf repellent"},"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'}
}

然后大概您想为这些新房间添加一些可能的主要方向,以便有人可以在它们之间移动和其他地方。你需要弄清楚你的地图在那里是什么样子。

此外,您在 while 循环之外还有一个 continue:

try:
    currentRoom = rooms[currentRoom][player_move]
except Exception:
    print("invalid move")
    continue

这个块没有在while循环内缩进。第 44 行和第 45 行也不是。您最终会永久停留在 while 循环中,而不会碰到第 39 行,直到您键入“exit”,此时您将在脚本引发 语法错误:“继续”在循环中不正确。

你需要像这样缩进:

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

您还应该重新考虑捕获那个过于广泛的异常。在这种情况下它会起作用,但这是不好的做法。您应该确定如果用户输入无效移动并准确捕获该异常,则实际会引发什么异常。在这种情况下,如果玩家的移动与定义的方向不匹配,则为 KeyError

制定过于宽泛的例外可能会隐藏代码中的其他错误。有时间和地点可以捕获 Exception,例如,如果您正在编写一个库并且需要捕获所有异常,然后使用附加上下文重新引发自定义异常,但这可能不是那种情况。

,

您的代码有一个不在循环中的 continue 语句。我认为您需要缩进 try: 行和之后的行,以便将它们视为位于其上方的 while 循环内。否则,Python 会认为这些行在循环之后而不是内部