2

所以我想为我的游戏编写一个函数来切换程序的当前状态,并根据当前状态显示不同的菜单。

当我调用 switch_to("Main_Menu") 时,我收到以下错误:

File "d:\PythonProjects\MenuSystem\Menu.py", line 12, in switch_to
    for s in self.state.gamestates:
AttributeError: 'Game' object has no attribute 'gamestates'

这是一些代码,但实际上一点也不复杂。当游戏对象什么都不做时,为什么游戏对象会出错?

我尝试了很长时间才知道为什么会发生这种情况,但我遇到了这个问题......我希望你能帮助我。

提前谢谢了 !

这是来自主类的代码:

game = Game()
    
gamestates ={
               0:  "Game",
               1:  "Main_Menu",
               2:  "Settings_Menu",
               3:  "Sound_Menu",
               4:  "Gamepause_Menu"
            }
state = State(gamestates, "Main_Menu")

main_menu = Main_Menu(game, state)
main_menu.switch_to("Settings_Menu") # THIS CAUSES THE ERROR

这是状态对象类:

class State:

    def __init__(self, gamestates: dict, current_state: str):
        self.gamestates = gamestates
        self.current_state = current_state

最后是菜单:

class Menu:

    def __init__(self, game, state: State):
        self.game = game
        self.state = state


    def switch_to(self, gamestate: str):
        for s in self.state.gamestates:

            if s == gamestate:
                self.state.current_state = gamestate
                return
        raise Exception("No gamestate named '" + gamestate + "'")
            
         
class Main_Menu(Menu):

    def __init__(self, game, state):
        super().__init__(self, game, state)
4

0 回答 0