我做了一个简单的应用程序,列出了我正在做的一个项目的学分,这只是我想做的一些有趣的事情。但是,当我运行程序时,会出现一个错误。它说"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()发布于 2022-01-29 06:33:39
在这条线上:
running, playing, credits_menu, options_menu = False, False, False, False您将credits_menu设置为False
这会将其先前的定义重写为一个类。
发布于 2022-01-30 07:05:10
因此,这类问题的主要答案是检查你所指的是什么。在本例中,我意外地引用了变量 credits_menu,而不是类 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)https://stackoverflow.com/questions/70902859
复制相似问题