AttributeError: 'bool' object has no attribute 'show_credits_heading'

AttributeError: 'bool' object has no attribute 'show_credits_heading'

我制作了一个简单的应用程序,其中列出了我正在从事的项目的致谢名单,这只是我想做的一些开箱即用的有趣的事情。但是,当我 运行 程序出现错误时。它说“AttributeError:'bool' 对象没有属性 'show_credits_heading'”,我不知道为什么会这样。这是代码:

# Menu
Menu_Font = pygame.font.Font("8-BIT WONDER.TTF", 45)
Menu_HeadingX = 290
Menu_HeadingY = 70

Text_Font = pygame.font.Font("8-BIT WONDER.TTF", 25)

start_x, start_y = Menu_HeadingX, Menu_HeadingY+150
options_x, options_y = Menu_HeadingX, Menu_HeadingY+200
credits_x, credits_y = Menu_HeadingX, Menu_HeadingY+250

class credits_menu():
def show_credits_heading():
    Credits_Heading = Menu_Font.render("Credits", True, (255, 255, 255))
    screen.blit(Credits_Heading, (Menu_HeadingX, Menu_HeadingY))

def show_code_credit():
    Code_Credit = Text_Font.render("All Code: By Me", True, (255, 255, 255))
    screen.blit(Sound_Option, (start_x, start_y))

def show_idea_credit():
    Idea_Credit = Text_Font.render("Idea By Me", True, (255, 255, 255))
    screen.blit(Sound_Option, (options_x, options_y))
     
def show_img_credit():
    Img_Credit = Text_Font.render("Images: Taken from google", True, (255, 255, 255))
    screen.blit(Sound_Option, (credits_x, credits_y))

主要问题在于:

while credits_menu:
    screen.fill((0,0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running, playing, credits_menu, options_menu = False, False, False, False

    screen.fill((0,0,0))
    credits_menu.show_credits_heading()
    credits_menu.show_code_credit()
    credits_menu.show_idea_credit()
    credits_menu.show_img_credit()

这一行:

        running, playing, credits_menu, options_menu = False, False, False, False

您将 credits_menu 设置为 False

这会覆盖其早期定义为 class

因此,此类问题的主要答案是检查您所引用的内容。在这种情况下,我不小心引用了 变量 credits_menu 而不是 class credits_menu 因为两者都是标记相同,所以 python 跑步者感到困惑并取而代之的是没有定义函数的变量。

因此更正后的代码为:

class credits():
def show_credits_heading():
    Credits_Heading = Menu_Font.render("Credits", True, (255, 255, 255))
    screen.blit(Credits_Heading, (Menu_HeadingX, Menu_HeadingY))

def show_code_credit():
    Sound_Option = Credit_Font.render("All Code: By Me", True, (255, 255, 255))
    screen.blit(Sound_Option, (SCREEN_WIDTH/2, start_y))

def show_idea_credit():
    Sound_Option = Credit_Font.render("Idea By Me(my mom helped a bit too)", True, (255, 255, 255))
    screen.blit(Sound_Option, (SCREEN_WIDTH/2, options_y))
     
def show_img_credit():
    Sound_Option = Credit_Font.render("Images: Taken from google", True, (255, 255, 255))
    screen.blit(Sound_Option, (SCREEN_WIDTH/2, credits_y))

while credits_menu:
screen.fill((0,0,0))

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running, playing, credits_menu, options_menu = False, False, False, False

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RETURN:
            if state == "Back":
                running, playing, credits_menu, options_menu = False, True, False, False

screen.fill((0,0,0))
credits.show_credits_heading()
credits.show_code_credit()
credits.show_idea_credit()
credits.show_img_credit()

#Update Screen
pygame.display.update()
clock.tick(60)