照着书上打的,错误在哪里?
import pygameimport sys
from settings import Settings
def run_game():
#初始化游戏并创建一个屏幕对象
pygame.init()
ai_settings= Settings()
print (ai_settings.screen_width)
ai_settings.screen_height=80
#ai_settings.screen_width, ai_settings.screen_height
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
#设置背景色
bg_color = (30,100,230)
#开始游戏的主循环
while True:
#监视键盘和鼠标
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#每次循环是都绘制屏幕
#screen.fill(ai_settings.bg_color)
#让最近绘制的屏幕可见
pygame.display.flip()
run_game()
#####################################################
class Settings():
"""存储《外星人入侵》的所有设置的类"""
def _init_(self):
"""初始化游戏的设置"""
#屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
print (screen_width)
print (1221)
print(Settings.screen_width)
#############################################################################
pygame 1.9.6
Hello from the pygame community. https://www.
Traceback (most recent call last):
File "E:/python/game_fly/alien_invasion.py", line 23, in <module>
run_game()
File "E:/python/game_fly/alien_invasion.py", line 9, in run_game
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
AttributeError: 'Settings' object has no attribute 'screen_width'
>>>
======================================================================= RESTART: E:/python/game_fly/alien_invasion.py ======================================================================
pygame 1.9.6
Hello from the pygame community. https://www.
======================================================================= RESTART: E:/python/game_fly/alien_invasion.py ======================================================================
pygame 1.9.6
Hello from the pygame community. https://www.
Traceback (most recent call last):
File "E:/python/game_fly/alien_invasion.py", line 25, in <module>
run_game()
File "E:/python/game_fly/alien_invasion.py", line 8, in run_game
print (ai_settings.screen_width)
AttributeError: 'Settings' object has no attribute 'screen_width'
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
122121
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
Traceback (most recent call last):
File "E:/python/game_fly/settings.py", line 11, in <module>
print(screen_width)
NameError: name 'screen_width' is not defined
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
Traceback (most recent call last):
File "E:/python/game_fly/settings.py", line 11, in <module>
print(Settings.screen_width)
AttributeError: type object 'Settings' has no attribute 'screen_width'
>>>