import sys
from time import sleep

import pygame

from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from ship import Ship
from bullet import Bullet
from alien import Alien

class AlienInvasion: # 管理游戏资源和行为的类11

    def __init__(self):  # 初始化游戏设置并创建资源
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("消灭外星人")

        self.ship = Ship(self)

    def run_game(self): # 开始游戏主循环
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()
            # 监视键盘和鼠标事件

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
    elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
    self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
        self.ship.moving_left = False

    def _update_screen(self):
        self.screen.fill(self.settings.bg_color) # 每次循环时都重绘屏幕
        self.ship.blitme()

        # 让最近绘制的屏幕可见
        pygame.display.flip()

if __name__ == '__main__':     # 创建游戏实例并运行游戏
    ai = AlienInvasion()
    ai.run_game()