# coding:utf-8
import sys
import pygame
from plane_sprites import *


class PlaneGame(object):
    """飞机大战"""

    def __init__(self):
        print "游戏初始化"

        # 1.创建游戏的窗口

        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        # 2. 创建游戏时钟
        self.clock = pygame.time.Clock()
        # 3.调用私有方法， 精灵和精灵组的创建
        self.__create_sprites()
        # 4.设置定时器事件，创建敌机，1s
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def __create_sprites(self):

        # 创建背景精灵和精灵组
        bg1 = Background()
        bg2 = Background(is_alt=True)
        self.back_group = pygame.sprite.Group(bg1, bg2)

        # 创建敌机精灵组
        self.enemy_group = pygame.sprite.Group()

        # 创建英雄精灵组
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

    def start_game(self):
        print "游戏开始..."

        while True:
            # 1.设置刷新率
            self.clock.tick(FRAME_PER_SECOND)
            # 2.事件监听
            self.__event_handler()
            # 3.碰撞检测
            self.__check_collide()
            # 4.更新精灵组
            self.__update_sprites()
            # 5.更新显示
            pygame.display.update()

    def __event_handler(self):
        for event in pygame.event.get():
            # 判断是否退出游戏
            if event.type == pygame.QUIT:
                PlaneGame.__game_over()
            elif event.type == CREATE_ENEMY_EVENT:
                print "enemy attack"
                # 创建敌机精灵，将其添加到精灵组
                enemy = Enemy()
                self.enemy_group.add(enemy)
            elif event.type == HERO_FIRE_EVENT:
                    if not self.hero.bomb:
                        self.hero.fire()
            # elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
            #     print "move left"
        # 使用键盘方法获取按键元组
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            self.hero.speed = 5
        elif keys_pressed[pygame.K_LEFT]:
            self.hero.speed = -5
        else:
            self.hero.speed = 0

    def __check_collide(self):
        enemies_2 = pygame.sprite.groupcollide(self.hero.bullets,
                                   self.enemy_group,
                                   dokilla=1, dokillb=0)

        enemies = pygame.sprite.groupcollide(self.hero_group,
                                             self.enemy_group, 0, 0)
        if len(enemies) > 0:
            # 英雄消除
            self.hero.bomb = 1
            for i in enemies[self.hero]:
                i.bomb = 1
        if len(enemies_2) > 0:
            for i in enemies_2.values():
                for j in i:
                    j.bomb = 1

    def __update_sprites(self):

        self.back_group.update()
        self.back_group.draw(self.screen)

        self.enemy_group.update()
        self.enemy_group.draw(self.screen)

        self.hero_group.update()
        self.hero_group.draw(self.screen)

        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    @staticmethod
    def __game_over():
        print "Game Over!"
        pygame.quit()
        sys.exit()


if __name__ == "__main__":
    # 创建游戏对象
    game = PlaneGame()

    # 开始游戏
    game.start_game()
