python - 'Button' 对象没有属性 'prep_msg'

标签 python pygame attributes attributeerror

正在学习 python 速成类(class),出于某种原因,我无法弄清楚我在第一个项目外星人入侵的这一部分做错了什么。它表示 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'

和按钮模组

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)

最佳答案

看起来您的意思是让 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)

关于python - 'Button' 对象没有属性 'prep_msg',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168186/

相关文章:

python - 我无法在Python太空入侵者游戏中射击时移动我的角色(使用Zelle图形包)

python - 为什么pygame中的模块事件有内置函数?

jquery - 编写更高效的属性选择器函数

php - 压缩 HTML 标记中属性之间的空格

javascript - 从 jQuery 中的所有元素中获取属性值

python - Django - 在模型中加载大默认值的正确方法

python - 如何在最小最大缩放后将二维数组转换为一维数组

python - 无法将 Sprite 绘制到屏幕

Python 对象矩阵 - 定义属性

python - 从单个 python 列表中删除特殊字符