簡體   English   中英

'Button'對象沒有屬性'prep_msg'

[英]'Button' object has no attribute 'prep_msg'

通過python速成課程,由於某種原因,我無法弄清楚我在第一個項目的這一部分,Alien入侵時做錯了什么。 它說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)

看起來你的意思是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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM