为什么我的类变量状态没有更新?

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

我这里有一个错误,我的变量“current_room”没有在“GameState”类中更新。 我是在堆栈上发帖的新手,所以当我输入这篇文章时,我不确定如何使其看起来像代码。

class GameState:
    def __init__(self, current_room=None, player_inventory=None):
        self.current_room = current_room if current_room else "Chapel"  # Start in the Chapel by default
        self.player_inventory = player_inventory if player_inventory else []

    def to_dict(self):
        return {
            "current_room": self.current_room,
            "player_inventory": self.player_inventory
        }

    @classmethod
    def from_dict(cls, state_dict):
        return cls(state_dict["current_room"], state_dict["player_inventory"])
def move(game_state, game_map):
    current_room_state = game_state.current_room
    current_room_info = game_map[current_room_state]

    # Get player's input for movement command
    move_command_input = input("Which direction would you like to move?: ").strip().lower()

    # Check if the move command is valid
    if move_command_input in directions:
        direction = directions[move_command_input]
        if direction in current_room_info["Exits"]:
            new_room = current_room_info["Exits"][direction]
            print(f"You moved to the {new_room}.")
            game_state.current_room = new_room  # Update the current room
        else:
            print("You can't move in that direction.")
    else:
        print("Invalid move command.")

    return game_state
game_map = {
    "Chapel": {
        "Description": " I am standing in a majestic chapel."
                       "\n             There is a coffin in front of me.",
        "Exits": {"north": "Window room", "east": "Tunnel", "west": "Ballroom (Fireplace)", "south": "Stairs"},
        "Items": [],
        "Objects": ["coffin"]
    },
    "Window": {
        "Description": "There is a big window at the top of the stairs."
                       "\nThe view from here is amazing...",
        "Exits": {"s": "Chapel"},
        "Items": [],
        "Objects": ["window"]
    },
    "Ballroom (Fireplace)": {
        "Description": "\n I am standing in the grand ballroom..."
                       "\n Sure will you had a girl to dance with don't you?",
        "Exits": {"e": "Chapel"},
        "Items": [],
        "Objects": []
    },
    "Tunnel": {
        "Description": "\nI am in a tunnel with limestone walls."
                       "\nThere is a large stone door with a Sapphire in the middle.",
        "Exits": {"w": "Chapel"},
        "Items": ["bloody knife"],
        "Objects": []
    },
    "Stairs": {
        "Description": "\nI am in a dingy looking stairwell."
                       "\nBroken glass is everywhere, looks like an explosion happened?",
        "Exits": {"n": "Chapel", "e": "Kettle Room", "s": "Room", "w": "Dungeon"},
        "Items": ["broken glass"],
        "Objects": []
    },
    "Room": {
        "Description": "\nYou are in a plain, nondescript room.",
        "Exits": {"n": "Armory"},
        "Items": [],
        "Objects": []
    },
    "Kettle Room": {
        "Description": "\nThis looks like their version of a kitchen."
                       "\nLost of pots, pans, and kettles are in the sink...",
        "Exits": {"n": "Head Room", "w": "Stairs"},
        "Items": [],
        "Objects": []
    },
    "Head Room": {
        "Description": "\nIm standing in the room filled with animal heads."
                       "\nIt looks like these guys loved to hunt...",
        "Exits": {"e": "Pantry", "s": "Kettle Room"},
        "Items": [],
        "Objects": []
    },
    "Pantry": {
        "Description": "\nFound the pantry, Might as well take a bite!"
                       "\nI wonder why they need a pantry in a chapel?",
        "Exits": {"w": "Head Room", "e": "Lab"},
        "Items": ["Food"],
        "Objects": ["Basket"]
    },
    "Lab": {
        "Description": "\nA Lab? What kind of a chapel is this?"
                       "\nAnyways, I wonder what they might be doing in here?",
        "Exits": {"w": "Pantry"},
        "Items": [],
        "Objects": ["chemicals"]
    },
    "Dungeon": {
        "Description": "\nThe dungeon seems to be a bit dusty,"
                       "\nThere are skeletons on the ground...",
        "Exits": {"e": "Stairs", "s": "Torture Chamber"},
        "Items": ["metal chain"],
        "Objects": ["gate"]
    },
    "Torture Chamber": {
        "Description": "\nOne of the worst rooms in the whole castle",
        "Exits": {"n": "Dungeon", "e": "Armory"},
        "Items": ["bones", "Sword"],
        "Objects": ["Skeleton", "Bones"]
    },
    "Armory": {
        "Description": "\nLooks like you found the weapons hub of this whole place,"
                       "\nReally Staring to look more like a castle rather than a chapel",
        "Exits": {"w": "Torture Chamber"},
        "Items": [],
        "Objects": []
    }
}

游戏地图查看:

                  Window
            |
Ballroom -------- Chapel ----- Tunnel
(Fireplace)         |
            |              Head Room -- Pot Room -- Lab
            |                 |
Dungeon --------- Stairs --------- Kettle Room
   |                |
Torture---Armory    |
                    |
                  Room
moves = ["n", "e", "s", "w", "take", "slide", "move", "turn", "climb", "push", "circle"]
items = []
# Items you can pick up

edible_items = []
objects = []

我尝试了几种不同的方法来更新 current_room 变量,但主要问题是 current_room 的值没有传递到游戏状态。我的一些失败的尝试包括一个名为“new_room”的变量。其中退出方向将存储在变量中,new_room变量将存储在game_state.current_room中。但这是可行的,因为由于某种原因,该值不会被转移。我使用Python大约一年了,这是我过去没有遇到过的问题。

python class variables
1个回答
0
投票

您发布的代码没有任何问题,但我怀疑问题出在您的

directions
字典和您在下面定义的
Exits

"Chapel": {
    "Description": " I am standing in a majestic chapel."
                   "\n             There is a coffin in front of me.",
    "Exits": {"north": "Window room", "east": "Tunnel", "west": "Ballroom (Fireplace)", "south": "Stairs"},
    "Items": [],
    "Objects": ["coffin"]
},
"Window": {
    "Description": "There is a big window at the top of the stairs."
                   "\nThe view from here is amazing...",
    "Exits": {"s": "Chapel"},
    "Items": [],
    "Objects": ["window"]
}

查看第一个

Exits
如何具有键
north, east, west, south
但下一个条目如何具有键
s
。您的代码将用户输入的任何内容映射到以下键之一:

direction = directions[move_command_input]
if direction in current_room_info["Exits"]:
    new_room = current_room_info["Exits"][direction]

例如,如果用户输入是

down
,我假设您的
directions
字典会将其映射到
s
south
,但不能同时映射到两者。

我建议您将所有

Exits

中的键更改为第一个(
south
)或第二个(
s
)。

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