import pygame
import sys

# 1. 初始化
pygame.init()

# 常量设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 400
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
BLUE = (0, 0, 200)
YELLOW = (255, 255, 0)

# 创建窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("双人方块格斗")
clock = pygame.time.Clock()

# 2. 玩家类


class Player:
    def __init__(self, x, y, color, controls):
        self.rect = pygame.Rect(x, y, 50, 80)
        self.color = color
        self.controls = controls  # [上, 下, 左, 右, 攻击]
        self.vel_y = 0
        self.is_jumping = False
        self.health = 100
        self.attacking = False
        self.attack_cooldown = 0
        self.hit_flash = 0

    def move(self):
        keys = pygame.key.get_pressed()

        # 左右移动
        if keys[self.controls[2]] and self.rect.left > 0:
            self.rect.x -= 7
        if keys[self.controls[3]] and self.rect.right < SCREEN_WIDTH:
            self.rect.x += 7

        # 跳跃
        if not self.is_jumping:
            if keys[self.controls[0]]:
                self.vel_y = -15
                self.is_jumping = True

        # 重力
        self.vel_y += 1
        self.rect.y += self.vel_y

        # 地面检测
        if self.rect.bottom > SCREEN_HEIGHT - 20:
            self.rect.bottom = SCREEN_HEIGHT - 20
            self.is_jumping = False
            self.vel_y = 0

    def attack(self, target):
        keys = pygame.key.get_pressed()
        if self.attack_cooldown > 0:
            self.attack_cooldown -= 1

        if keys[self.controls[4]] and self.attack_cooldown == 0:
            self.attacking = True
            self.attack_cooldown = 20  # 攻击冷却

            # 创建攻击判定区 (前方 50 像素)
            attack_rect = pygame.Rect(
                self.rect.x - 30, self.rect.y, self.rect.width + 60, self.rect.height)

            if attack_rect.colliderect(target.rect):
                target.health -= 10
                target.hit_flash = 5  # 受击闪烁帧数
        else:
            self.attacking = False

    def draw(self, surface):
        # 如果被击中，显示黄色
        display_color = YELLOW if self.hit_flash > 0 else self.color
        if self.hit_flash > 0:
            self.hit_flash -= 1

        pygame.draw.rect(surface, display_color, self.rect)

        # 绘制攻击动作
        if self.attacking:
            attack_visual = pygame.Rect(
                self.rect.x - 10, self.rect.y + 20, self.rect.width + 20, 10)
            pygame.draw.rect(surface, BLACK, attack_visual)

# 3. 绘制血条函数


def draw_health_bar(health, x, y, color):
    pygame.draw.rect(screen, BLACK, (x-2, y-2, 204, 24))  # 外框
    if health > 0:
        pygame.draw.rect(screen, color, (x, y, health * 2, 20))


# 4. 实例化玩家
# 玩家1: WASD 移动, F 攻击
p1 = Player(150, 200, BLUE, [pygame.K_w, pygame.K_s,
                             pygame.K_a, pygame.K_d, pygame.K_f])
# 玩家2: 方向键 移动, K 攻击
p2 = Player(600, 200, RED, [pygame.K_UP, pygame.K_DOWN,
                            pygame.K_LEFT, pygame.K_RIGHT, pygame.K_k])

# 5. 游戏主循环
font = pygame.font.SysFont("SimHei", 40)

while True:
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    if p1.health > 0 and p2.health > 0:
        # 逻辑更新
        p1.move()
        p2.move()
        p1.attack(p2)
        p2.attack(p1)

        # 绘制地面
        pygame.draw.rect(
            screen, BLACK, (0, SCREEN_HEIGHT-20, SCREEN_WIDTH, 20))

        # 绘制角色
        p1.draw(screen)
        p2.draw(screen)

        # 绘制血条
        draw_health_bar(p1.health, 50, 30, BLUE)
        draw_health_bar(p2.health, 550, 30, RED)
    else:
        # 游戏结束显示
        winner = "玩家 1 获胜!" if p1.health > 0 else "玩家 2 获胜!"
        text = font.render(winner, True, BLACK)
        screen.blit(text, (SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2))
        restart_text = font.render("按 R 重新开始", True, BLACK)
        screen.blit(restart_text, (SCREEN_WIDTH //
                                   2 - 100, SCREEN_HEIGHT//2 + 50))

        keys = pygame.key.get_pressed()
        if keys[pygame.K_r]:
            p1.health, p2.health = 100, 100
            p1.rect.x, p2.rect.x = 150, 600

    pygame.display.flip()
    clock.tick(FPS)
