gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:58:45 27 4
gpt4 key购买 nike

正在学习 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com