这是主程序,和书上一摸一样。
各位帮帮忙,看不出来哪里出错了!
from xxxxx import settings
如果你使用了setting文件,你要确定导入了这个基本的setting,找你导入配置的地方
以下答案引用自GPT-3大模型,请合理使用:
调整后的正确的版本。
Traceback (most recent call last):
File "alien_invasion.py", line 15, in <module>
ai = AlienInvasion()
File "alien_invasion.py", line 9, in __init__
self.settings = Settings()
File "settings.py", line 7, in __init__
self.screen_width = 1200
AttributeError: 'Settings' object has no attribute 'screen_width'
代码调整后的正确版本:
alien_invasion.py文件:
import pygame
from settings import Settings
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
"""Start the main loop for the game."""
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
settings.py文件:
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
如果我的回答解决了您的问题,请采纳我的回答
不知道你这个问题是否已经解决, 如果还没有解决的话: