微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决奇怪的AttributeError?

如何解决如何解决奇怪的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 举报,一经查实,本站将立刻删除。