#主程序
import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(ai_settings.screen_width,ai_settings.screen_height)
pygame.display.set_caption("Alien Invasion")
"""buiding a ship"""
ship = Ship(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
run_game()
#设置类,说我类中没有相应属性
class Settings():
def _init_(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
运行报错原因:
Traceback (most recent call last):
File "E:\Alien_invasion\alien_invasion.py", in <module>
run_game()
File "E:\Alien_invasion\alien_invasion.py",in run_game
screen = pygame.display.set_mode(ai_settings.screen_width,ai_settings.screen_height)
AttributeError: 'Settings' object has no attribute 'screen_width'
我明明定义了了属性'screen_width',为什么还报错呢?