如何解决奇怪的AttributeError?

问题描述

所以我想为我的游戏编写一个功能,该功能可以切换程序的当前状态,并且根据当前状态,显示菜单一个不同的菜单

当我调用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

这是State Object类:

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,state):
        super().__init__(self,state)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)