import pygame
import random

pygame.init()

# 窗口配置
WIDTH = 480
HEIGHT = 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
clock = pygame.time.Clock()
FPS = 60

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PLANE_COLOR = (0, 150, 255)
ENEMY_COLOR = (220, 30, 30)
BULLET_COLOR = (255, 220, 0)
RED = (255, 0, 0)

# 字体（避开SysFont报错）
font = pygame.font.Font(None, 36)

#玩家飞机
player_x = WIDTH // 2
player_y = HEIGHT - 80
player_speed = 6
player_width = 40
player_height = 50
hp = 3

#子弹列表
bullets = []
bullet_speed = 8
shoot_cd = 0
shoot_delay = 12

#敌机列表
enemies = []
enemy_speed = 3
spawn_timer = 0

score = 0
game_over = False

running = True
while running:
    clock.tick(FPS)
    shoot_cd += 1

    #事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if not game_over:
        #移动玩家
        if keys[pygame.K_LEFT] and player_x>0:
            player_x -= player_speed
        if keys[pygame.K_RIGHT] and player_x<WIDTH-player_width:
            player_x += player_speed
        if keys[pygame.K_UP] and player_y>0:
            player_y -= player_speed
        if keys[pygame.K_DOWN] and player_y<HEIGHT-player_height:
            player_y += player_speed
        #发射子弹
        if keys[pygame.K_SPACE] and shoot_cd >= shoot_delay:
            bullets.append([player_x+player_width//2, player_y])
            shoot_cd = 0

        #生成敌机
        spawn_timer += 1
        if spawn_timer >= 45:
            ex = random.randint(0, WIDTH - 40)
            enemies.append([ex,-50])
            spawn_timer = 0

        #更新子弹
        new_bullets = []
        for b in bullets:
            b[1] -= bullet_speed
            if b[1]>0:
                new_bullets.append(b)
        bullets = new_bullets

        #更新敌机
        new_enemy = []
        for e in enemies:
            e[1] += enemy_speed
            #子弹击中敌机
            hit = False
            for b in bullets[:]:
                if e[0] < b[0] < e[0]+40 and e[1] < b[1] < e[1]+50:
                    bullets.remove(b)
                    score += 10
                    hit = True
                    break
            if hit:
                continue
            #敌机撞到玩家
            if (player_x < e[0]+40 and player_x+player_width>e[0]
                and player_y<e[1]+50 and player_y+player_height>e[1]):
                hp -= 1
                e[1] = HEIGHT + 100
                if hp <= 0:
                    game_over = True
            if e[1] < HEIGHT:
                new_enemy.append(e)
        enemies = new_enemy

    #绘制画面
    screen.fill(BLACK)

    #绘制子弹
    for b in bullets:
        pygame.draw.rect(screen,BULLET_COLOR,(b[0]-2,b[1],4,12))

    #绘制敌机
    for e in enemies:
        pygame.draw.rect(screen,ENEMY_COLOR,(e[0],e[1],40,50))

    #绘制玩家飞机
    pygame.draw.rect(screen,PLANE_COLOR,(player_x,player_y,player_width,player_height))

    #UI文字
    score_text = font.render(f"分数: {score}",True,WHITE)
    hp_text = font.render(f"生命: {hp}",True,RED)
    screen.blit(score_text,(10,10))
    screen.blit(hp_text,(10,45))

    if game_over:
        end_text = font.render("游戏结束!",True,RED)
        screen.blit(end_text,(WIDTH//2-70,HEIGHT//2))

    pygame.display.flip()

pygame.quit()
