基于文本的 RPG 中的字典和函数

问题描述 投票:0回答:1

大家。我试图通过制作基于文本的角色扮演游戏来了解更多关于字典和函数的知识,但我遇到了一个我自己似乎无法解决的问题。我在脚本的“主要”部分为地图制作了一个字典,它不在任何内容之内,如下所示:

the_map = {
    'Town': {'Next room': 'Ruins', 'Rest': 'Fill HP'},
    'Ruins': {'Next room': 'Destroyed Village', 'Previous room': 'Town', 'Enemy': 'Minotaur'},
    'Destroyed Village': {'Next room': 'Forbidden Forest', 'Previous room': 'The Ruins', 'Enemy': 'Giant'},
    'Forbidden Forest': {'Previous room': 'Destroyed Village', 'Enemy': 'Ancient Tree'}
}


# STARTING ROOM
current_room = "Town"

我的脚本让玩家在选项之间进行选择,而不是输入响应。事情是这样的:

def start_game():

    while True:

        # DISPLAY HERO INFO
        print(f"\n You are in the {current_room}.\n{'#' * 22}")

        if current_room == "Town":
            print("\nYou can either go to the NEXT ROOM or REST.")
            print("NEXT ROOM takes you to an enemy encounter.")
            print("REST fills up your HP bar.")
            print("What would you like to do?")
            print("1 - GO TO THE NEXT ROOM\n2 - REST")

            # USER INPUT
            user_input = input("> ")

            if user_input == "1":
                next_room()

            elif user_input == "2":
                pass

            else:
                print("Invalid command. Try something else.\n")

如您所见,我使用函数“next_room()”。我想做的是:玩家从“城镇”开始,“下一个房间”,如字典中所示,是“废墟”。如果玩家输入“1”进入下一个房间,我希望更新 current_room,因为下一个房间成为当前房间。我怎样才能使用字典做到这一点?这是我到目前为止所写的:

def next_room():

    if "Next room" in the_map[current_room].keys():

        room_after = the_map[current_room]["Next room"]

        print(f"\nYou reached the {room_after}!")

我不确定如何使其更新到 current_room。它打印了这段代码中的语句,但原始帖子的第一个仍然显示“You are in the Town”,这是起始房间。

python function dictionary
1个回答
0
投票

通过将

the_map
current_room
放入
start_game
来避免使用全局变量。然后将变量传递给
next_room
,拥有它
return room_after
,然后使用
current_room = next_room(current_room, the_map)
进行更新。

(我还加上了

else: break
,因为我不知道你之后的行为应该是什么。)

def start_game():
    
    the_map = {
        'Town': {'Next room': 'Ruins', 'Rest': 'Fill HP'},
        'Ruins': {'Next room': 'Destroyed Village', 'Previous room': 'Town', 'Enemy': 'Minotaur'},
        'Destroyed Village': {'Next room': 'Forbidden Forest', 'Previous room': 'The Ruins', 'Enemy': 'Giant'},
        'Forbidden Forest': {'Previous room': 'Destroyed Village', 'Enemy': 'Ancient Tree'}
    }
    
    
    # STARTING ROOM
    current_room = "Town"

    while True:

        # DISPLAY HERO INFO
        print(f"\n You are in the {current_room}.\n{'#' * 22}")

        if current_room == "Town":
            print("\nYou can either go to the NEXT ROOM or REST.")
            print("NEXT ROOM takes you to an enemy encounter.")
            print("REST fills up your HP bar.")
            print("What would you like to do?")
            print("1 - GO TO THE NEXT ROOM\n2 - REST")

            # USER INPUT
            user_input = input("> ")

            if user_input == "1":
                current_room = next_room(current_room, the_map)

            elif user_input == "2":
                pass

            else:
                print("Invalid command. Try something else.\n")
        else:
            break

def next_room(current_room, the_map):

    if "Next room" in the_map[current_room].keys():

        room_after = the_map[current_room]["Next room"]

        print(f"\nYou reached the {room_after}!")

    return room_after
© www.soinside.com 2019 - 2024. All rights reserved.