import pygame
import random

# --- 初始化 ---
pygame.init()
WIDTH, HEIGHT = 600, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("海上枪战")
clock = pygame.time.Clock()

# 颜色定义
SEA_COLOR = (30, 100, 180)       # 海水蓝
WAVE_COLOR = (50, 130, 210)      # 海浪浅蓝
PLAYER_COLOR = (200, 200, 200)   # 玩家船体
ENEMY_COLOR = (180, 40, 40)      # 敌船红
BULLET_COLOR = (255, 255, 0)     # 子弹黄
UI_COLOR = (255, 255, 255)

FONT = pygame.font.SysFont("arial", 30)
BIG_FONT = pygame.font.SysFont("arial", 60)

# --- 类定义 ---

class Player:
    def __init__(self):
        self.width = 40
        self.height = 60
        self.x = WIDTH // 2
        self.y = HEIGHT - 100
        self.speed = 6
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.x += self.speed
        if keys[pygame.K_UP] or keys[pygame.K_w]:
            self.y -= self.speed
        if keys[pygame.K_DOWN] or keys[pygame.K_s]:
            self.y += self.speed

        # 边界限制
        self.x = max(0, min(WIDTH - self.width, self.x))
        self.y = max(0, min(HEIGHT - self.height, self.y))
        self.rect.topleft = (self.x, self.y)

    def draw(self, surface):
        # 简单的船体形状
        points = [
            (self.x + self.width // 2, self.y),
            (self.x + self.width, self.y + self.height),
            (self.x, self.y + self.height)
        ]
        pygame.draw.polygon(surface, PLAYER_COLOR, points)
        pygame.draw.polygon(surface, (100, 100, 100), points, 2)

class Bullet:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 6, 15)
        self.speed = 10

    def update(self):
        self.rect.y -= self.speed

    def draw(self, surface):
        pygame.draw.rect(surface, BULLET_COLOR, self.rect)

class Enemy:
    def __init__(self):
        self.width = 40
        self.height = 50
        self.x = random.randint(0, WIDTH - self.width)
        self.y = -50
        self.speed = random.uniform(2, 5)
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        self.y += self.speed
        self.rect.topleft = (self.x, self.y)

    def draw(self, surface):
        # 倒三角表示敌船
        points = [
            (self.x, self.y),
            (self.x + self.width, self.y),
            (self.x + self.width // 2, self.y + self.height)
        ]
        pygame.draw.polygon(surface, ENEMY_COLOR, points)
        pygame.draw.polygon(surface, (100, 0, 0), points, 2)

# --- 主程序 ---

def main():
    player = Player()
    bullets = []
    enemies = []
    score = 0
    game_over = False
    spawn_timer = 0
    wave_offset = 0 # 用于海浪动画

    running = True
    while running:
        clock.tick(60)
        screen.fill(SEA_COLOR)

        # 简单的海浪背景动画
        wave_offset = (wave_offset + 1) % 40
        for i in range(0, HEIGHT, 40):
            y = i + wave_offset
            pygame.draw.line(screen, WAVE_COLOR, (0, y), (WIDTH, y), 2)

        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and not game_over:
                if event.key == pygame.K_SPACE:
                    # 从船头发射子弹
                    bx = player.x + player.width // 2 - 3
                    by = player.y
                    bullets.append(Bullet(bx, by))

        if not game_over:
            player.update()

            # 生成敌船
            spawn_timer += 1
            if spawn_timer > 40: # 每40帧生成一个
                spawn_timer = 0
                enemies.append(Enemy())

            # 更新子弹
            for bullet in bullets[:]:
                bullet.update()
                if bullet.rect.bottom < 0:
                    bullets.remove(bullet)

            # 更新敌船
            for enemy in enemies[:]:
                enemy.update()
                
                # 敌船超出屏幕
                if enemy.rect.top > HEIGHT:
                    enemies.remove(enemy)
                    continue

                # 子弹击中敌船
                hit = False
                for bullet in bullets[:]:
                    if bullet.rect.colliderect(enemy.rect):
                        bullets.remove(bullet)
                        hit = True
                        score += 10
                        break
                if hit:
                    enemies.remove(enemy)
                    continue

                # 敌船撞到玩家
                if enemy.rect.colliderect(player.rect):
                    game_over = True

        # --- 绘图 ---
        player.draw(screen)
        for bullet in bullets: bullet.draw(screen)
        for enemy in enemies: enemy.draw(screen)

        # UI
        score_text = FONT.render(f"Score: {score}", True, UI_COLOR)
        screen.blit(score_text, (10, 10))

        if game_over:
            over_text = BIG_FONT.render("GAME OVER", True, (255, 50, 50))
            rect = over_text.get_rect(center=(WIDTH//2, HEIGHT//2))
            screen.blit(over_text, rect)
            
            restart_text = FONT.render("Press R to Restart", True, UI_COLOR)
            r_rect = restart_text.get_rect(center=(WIDTH//2, HEIGHT//2 + 60))
            screen.blit(restart_text, r_rect)

            # 按 R 重新开始
            keys = pygame.key.get_pressed()
            if keys[pygame.K_r]:
                main() # 简单粗暴的重启方式
                return

        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()