首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'bool‘对象没有属性'show_credits_heading’

AttributeError:'bool‘对象没有属性'show_credits_heading’
EN

Stack Overflow用户
提问于 2022-01-29 06:29:24
回答 2查看 898关注 0票数 1

我做了一个简单的应用程序,列出了我正在做的一个项目的学分,这只是我想做的一些有趣的事情。但是,当我运行程序时,会出现一个错误。它说"AttributeError:'bool‘对象没有属性'show_credits_heading'“,我不知道为什么会这样。以下是代码:

代码语言:javascript
复制
# 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))

主要问题是:

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-29 06:33:39

在这条线上:

代码语言:javascript
复制
        running, playing, credits_menu, options_menu = False, False, False, False

您将credits_menu设置为False

这会将其先前的定义重写为一个类。

票数 1
EN

Stack Overflow用户

发布于 2022-01-30 07:05:10

因此,这类问题的主要答案是检查你所指的是什么。在本例中,我意外地引用了变量 credits_menu,而不是 credits_menu,因为两者都被标记为相同,因此python被混淆了,取而代之的是没有定义函数的变量。

因此,修正后的代码是:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70902859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档