【发布时间】:2020-12-07 17:08:34
【问题描述】:
我一直在尝试构建一个外星入侵游戏,但是当我运行它时,它给了我以下信息:“AttributeError: 'Settings' object has no attribute 'screen_width'”
我理解错误的含义,但我不明白为什么会出现错误。我仔细检查了我的拼写,甚至尝试将我的设置类和外星人入侵类中的名称更改为没有成功。有人知道可能出了什么问题吗?谢谢
class AlienInvasion:
# Initialize the game and create game resources
def __init__(self):
pygame.init()
self.ai_settings = Settings()
#this code below creates the pygame screen. The set_caption method just changes th title of the window
#remmeber that if we do not pass the attibutes (screen, etc), it is considered the default value and will not change until we change it via another method in the class
self.screen = pygame.display.set_mode((self.ai_settings.screen_width, self.ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#create the background color
self.screen.fill(self.ai_settings.bg_color)
pygame.display.flip()
`if __name__ == '__main__':
ai = AlienInvasion()
ai.run_game()
class Settings:
"""A class to store all settings for the Alien Invasions game."""
def __init__(self):
#Screen Settings
self.screen_width= 1200
self.screen_height = 800
self.bg_color = (230,230,230)
【问题讨论】:
-
两个类都在单独的文件中,我只是不知道如何在问题框中说明这一点,并且除了 settings.py 之外,我的 Alien_Invasion 目录中的任何地方都没有使用设置
-
我没有,我什至尝试将类名从“设置”更改为“设置”,我得到了同样的错误