import pygame
import sys
import random
import math

pygame.init()
WIDTH, HEIGHT = 960, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("双人火柴人羽毛球【优化版】")
clock = pygame.time.Clock()

# ===== 颜色 =====
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRASS = (35, 145, 35)
GROUND = (28, 120, 28)
NET = (40, 40, 40)
BLUE_P = (20, 90, 220)
RED_P = (220, 25, 25)

# 字体兼容
try:
    font_info = pygame.font.SysFont("simhei", 24)
    font_score = pygame.font.SysFont("simhei", 36)
    font_win = pygame.font.SysFont("simhei", 64)
except:
    font_info = pygame.font.Font(None, 24)
    font_score = pygame.font.Font(None, 36)
    font_win = pygame.font.Font(None, 64)

NET_X = WIDTH // 2
GROUND_Y = HEIGHT - 40

# ===== 玩家基类 =====
class Stickman:
    def __init__(self, start_x, color):
        self.x = start_x
        self.y = GROUND_Y - 80
        self.color = color
        self.speed = 6
        self.vy = 0
        self.gravity = 0.65
        self.jump_force = -14
        self.on_ground = True

        self.swing_angle = 20
        self.swing_timer = 0
        self.swing_duration = 14

    def jump(self):
        if self.on_ground:
            self.vy = self.jump_force
            self.on_ground = False

    def swing(self):
        if self.swing_timer <= 0:
            self.swing_timer = self.swing_duration

    def update(self):
        #重力
        self.y += self.vy
        self.vy += self.gravity
        if self.y >= GROUND_Y - 80:
            self.y = GROUND_Y - 80
            self.vy = 0
            self.on_ground = True

        #挥拍动画
        if self.swing_timer > 0:
            self.swing_angle = -50
            self.swing_timer -= 1
        else:
            self.swing_angle = 18

    def draw(self):
        x, y = self.x, self.y
        #头
        pygame.draw.circle(screen, self.color, (x, y - 36), 15, 3)
        #躯干
        pygame.draw.line(screen, self.color, (x, y - 21), (x, y + 16), 3)
        #腿
        pygame.draw.line(screen, self.color, (x, y + 16), (x - 18, y + 48), 3)
        pygame.draw.line(screen, self.color, (x, y + 16), (x + 18, y + 48), 3)
        #手臂+球拍
        rad = math.radians(self.swing_angle)
        arm_x = x + math.cos(rad) * 30
        arm_y = y - 6 + math.sin(rad) * 30
        pygame.draw.line(screen, self.color, (x, y - 6), (arm_x, arm_y), 3)
        racket_x = arm_x + math.cos(rad) * 18
        racket_y = arm_y + math.sin(rad) * 18
        pygame.draw.circle(screen, self.color, (racket_x, racket_y), 10, 3)
        return racket_x, racket_y

# ===== 羽毛球 =====
class Shuttlecock:
    def reset(self, direction):
        self.x = WIDTH // 2
        self.y = 120
        self.vx = random.uniform(3.2, 5) * direction
        self.vy = random.uniform(-2, 1)
        self.r = 11

    def __init__(self):
        self.reset(random.choice([1, -1]))

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.22
        #空气阻力
        self.vx *= 0.9985

    def draw(self):
        pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.r)
        pygame.draw.circle(screen, BLACK, (int(self.x), int(self.y)), self.r, 2)

    def check_hit(self, racket_x, racket_y):
        dist = math.hypot(self.x - racket_x, self.y - racket_y)
        return dist < self.r + 12

# ===== 初始化 =====
p1 = Stickman(130, BLUE_P)
p2 = Stickman(WIDTH - 130, RED_P)
ball = Shuttlecock()
score1 = 0
score2 = 0
WIN_SCORE = 5
game_over = False

def serve(direction):
    ball.reset(direction)

running = True
while running:
    clock.tick(60)
    screen.fill(GRASS)
    pygame.draw.rect(screen, GROUND, [0, GROUND_Y, WIDTH, 40])
    #球网
    pygame.draw.rect(screen, NET, [NET_X - 5, GROUND_Y - 160, 10, 120])

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if game_over:
                if event.key == pygame.K_SPACE:
                    score1 = 0
                    score2 = 0
                    game_over = False
                    serve(random.choice([1, -1]))
            else:
                #挥拍
                if event.key == pygame.K_j:
                    p1.swing()
                if event.key == pygame.K_l:
                    p2.swing()
                #跳跃
                if event.key == pygame.K_w:
                    p1.jump()
                if event.key == pygame.K_UP:
                    p2.jump()

    keys = pygame.key.get_pressed()
    if not game_over:
        #玩家1移动
        if keys[pygame.K_a] and p1.x > 60:
            p1.x -= p1.speed
        if keys[pygame.K_d] and p1.x < NET_X - 70:
            p1.x += p1.speed
        #玩家2移动
        if keys[pygame.K_LEFT] and p2.x > NET_X + 70:
            p2.x -= p2.speed
        if keys[pygame.K_RIGHT] and p2.x < WIDTH - 60:
            p2.x += p2.speed

        p1.update()
        p2.update()
        ball.update()

        #获取球拍位置
        r1x, r1y = p1.draw()
        r2x, r2y = p2.draw()

        #击球判定
        if p1.swing_timer > 0 and ball.check_hit(r1x, r1y) and ball.vx < 0:
            power = random.uniform(-7.5, -2.5)
            ball.vx = abs(ball.vx) * 1.15
            ball.vy = power

        if p2.swing_timer > 0 and ball.check_hit(r2x, r2y) and ball.vx > 0:
            power = random.uniform(-7.5, -2.5)
            ball.vx = -abs(ball.vx) * 1.15
            ball.vy = power

        #球网阻挡简易逻辑
        if NET_X - 10 < ball.x < NET_X + 10 and ball.y > GROUND_Y - 160:
            if ball.vx < 0:
                ball.x = NET_X - 12
            else:
                ball.x = NET_X + 12
            ball.vx *= -0.7

        #落地判定
        if ball.y >= GROUND_Y - ball.r:
            if ball.x < NET_X:
                score2 += 1
                serve(1)
            else:
                score1 += 1
                serve(-1)

        #左右出界
        if ball.x < 0 or ball.x > WIDTH:
            if ball.x < 0:
                score2 += 1
            else:
                score1 += 1
            serve(random.choice([-1, 1]))

        #胜负判定
        if score1 >= WIN_SCORE or score2 >= WIN_SCORE:
            game_over = True
    else:
        p1.draw()
        p2.draw()

    ball.draw()

    #UI文字
    s_text = font_score.render(f"蓝 {score1} : {score2} 红", True, WHITE)
    screen.blit(s_text, (20, 12))
    hint1 = font_info.render("蓝：A/D移动 W跳 J挥拍", True, WHITE)
    hint2 = font_info.render("红：←→移动 ↑跳 L挥拍", True, WHITE)
    screen.blit(hint1, (20, 50))
    screen.blit(hint2, (20, 76))

    if game_over:
        if score1 >= WIN_SCORE:
            win_text = font_win.render("蓝方获胜！", True, BLUE_P)
        else:
            win_text = font_win.render("红方获胜！", True, RED_P)
        restart = font_info.render("按空格键重新开局", True, WHITE)
        screen.blit(win_text, (WIDTH//2 - 160, HEIGHT//2 - 70))
        screen.blit(restart, (WIDTH//2 - 140, HEIGHT//2 + 10))

    pygame.display.flip()

pygame.quit()
sys.exit()