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

Question:

I have made a simple app that lists the credits of a project I’m working on, it’s just something fun I wanted to do that was out of the box. However when I run the program an error shows up. It says that "AttributeError: ‘bool’ object has no attribute ‘show_credits_heading’" Which I have no idea why that is coming. Here is the code:

# 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))

Main Problem is in:

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()

Answers:

On this line:

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

you set credits_menu to False

This overrides its earlier definition as a class

Answered By: Jiří Baum

So the main answer for such kinds of questions is to check what you are referencing. In this case, I accidentally referenced the variable credits_menu instead of the class credits_menu because both were labeled the same so the python runner got confused and took the variable instead which did not have the defined functions.

So the corrected code would be:

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)
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.