[英]'Button' object has no attribute 'prep_msg'
Working through python crash course, and for some reason I cannot figure out what I have done wrong at this portion of the first project, Alien invasion. 通过python速成课程,由于某种原因,我无法弄清楚我在第一个项目的这一部分,Alien入侵时做错了什么。 It says that the Button object has no attribute 'prep_msg'.
它说Button对象没有属性'prep_msg'。 any help would be appreciated!
任何帮助,将不胜感激!
Attached is the traceback and module: 附件是追溯和模块:
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'
and button mod 和按钮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)
Looks like you meant for prep_msg() to be a method of the Button class. 看起来你的意思是prep_msg()是Button类的一个方法。 The problem is your indentation.
问题是你的缩进。 both the prep_msg() and draw_button() methods are indented inside the init() method, making them essentially nested functions.
prep_msg()和draw_button()方法都在 init()方法中缩进,使它们基本上是嵌套函数。
This should fix the problem: 这应该可以解决问题:
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.