“按钮”对象没有属性“ prep_msg”

时间:2019-03-14 16:57:49

标签: python pygame attributes attributeerror

在python崩溃过程中,由于某种原因,我无法弄清楚在第一个项目的这一部分“外星人入侵”中做错了什么。它说Button对象没有属性'prep_msg'。任何帮助将不胜感激!

附加的是回溯和模块:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "alien_invasion.py", line 63, in <module>
    run_game()
  File "alien_invasion.py", line 25, in run_game
    play_button = Button(ai_settings, screen, "Play")
  File "C:\Users\eslog\OneDrive\Desktop\alien_invasion\button.py", line 21, in __init__
    self.prep_msg(msg)
AttributeError: 'Button' object has no attribute 'prep_msg'

和按钮mod

import pygame.font

class Button():

    def __init__(self, ai_settings, screen, msg):
        """initialize button attrributes"""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        #set the dimesions and properties of the button
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)

        #build the button's rect object and center it
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        #the button message needs to be prepped only once
        self.prep_msg(msg)

        def prep_msg(self, msg):
            """turn msg into a rendered image and center text on the button"""
            self.msg_image = self.font.render(msg, True, self.text_color,
                                              self.button_color)
            self.msg_image_rect = self.msg_image.get_rect()
            self.msg_image.center = self.rect.center


        def draw_button(self):
            #draw blank button and then draw message
            self.screen.fill(self.button_color, self.rect)
            self.screen.blit(self.msg_image, self.msg_image_rect)

1 个答案:

答案 0 :(得分:0)

看起来像您要让prep_msg()成为Button类的方法一样。问题是您的缩进。 prep_msg()和draw_button()方法都在init()方法内缩进 ,从而使它们本质上是嵌套函数。

这应该可以解决问题:

import pygame.font

class Button():

    def __init__(self, ai_settings, screen, msg):
        """initialize button attrributes"""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        #set the dimesions and properties of the button
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)

        #build the button's rect object and center it
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        #the button message needs to be prepped only once
        self.prep_msg(msg)

    def prep_msg(self, msg):
        """turn msg into a rendered image and center text on the button"""
        self.msg_image = self.font.render(msg, True, self.text_color,
                                          self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image.center = self.rect.center


    def draw_button(self):
        #draw blank button and then draw message
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)