pygame - 错误对象不可调用

时间:2013-03-06 04:33:00

标签: python class pygame typeerror

我遇到问题的代码如下。有人可以帮我弄清楚如何解决这个错误吗?我想做的是当我点击开始游戏时,它会调用if语句下的main()中的行。

错误:

    Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 122, in <module>
    main()
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 75, in main
    controller = Manager(screen)
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 23, in __init__
    self.go_to(TitleMenu(screen))
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 47, in __init__
    self.main_loop()
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 54, in main_loop
    self.menu.update(events)
  File "C:\Users\Administrator\PycharmProjects\untitled\menumaker.py", line 46, in update
    self.options[self.option][1]()
  File "C:/Users/Administrator/PycharmProjects/untitled/Game.py", line 38, in <lambda>
    self.menu = makeMenu(["START GAME", lambda: run()], ["CONTROLS", lambda: Help(screen)], ["QUIT GAME", sys.exit])
TypeError: 'bool' object is not callable

代码:

class Manager(object):
    def __init__(self, screen):
        self.go_to(TitleMenu(screen))

    def go_to(self, scene):
        self.scene = scene
        self.scene.manager = self
#////////////////End Manager//////////////#
def run():
    global run
    run = True
    pass

#////////////////Title Menu and End Menu//////////////#
class TitleMenu(object):
    def __init__(self, screen):
        self.screen = screen
        self.menu = makeMenu(["START GAME", lambda: run()], ["CONTROLS", lambda: Help(screen)], ["QUIT GAME", sys.exit])
        self.menu.set_highlight_color((255, 0, 0))
        self.menu.set_normal_color((255, 255, 255))
        self.menu.center_at(300, 400)
        self.titleBackground = pygame.image.load("titleBackground.png")
        self.clock = pygame.time.Clock()
        events = pygame.event.get()
        self.menu.update(events)
        self.menu.draw(self.screen)
        self.main_loop()
        self.run = False

    def main_loop(self):
        while 1:
            self.clock.tick(60)
            events = pygame.event.get()
            self.menu.update(events)
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    return

            self.menu.draw(self.screen)
            pygame.display.flip()

    def go_(self):
        self.manager.go_to(GameLevel(0))

class EndMenu(object):
    pass
#////////////////End Title Menu and End Menu//////////#

def main():
    globals()
    pygame.init()
    screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
    pygame.display.set_caption("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    controller = Manager(screen)
    timer = pygame.time.Clock()
    running = True

    while running:
        timer.tick(60)
        if pygame.event.get(QUIT):
            running = False
            return

        controller.scene.handle_events(pygame.event.get())
        controller.scene.update()
        controller.scene.render(screen)
        pygame.display.flip()

        if run:
            controller.scene.go_()

    return Rect(left, bottom, width, height)

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:3)

问题是您的全局变量runrun函数混淆:

def run():
    global run
    run = True
    pass

这意味着当您尝试拨打run()时:

    self.menu = makeMenu(["START GAME", lambda: run()], ["CONTROLS", lambda: Help(screen)], ["QUIT GAME", sys.exit])

它试图调用布尔变量。要解决此问题,请重命名全局变量is_running,或重命名函数start_running