import pygame
import random

# --- 初始化 ---
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("像素RPG - 回合制战斗")
clock = pygame.time.Clock()

# 颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (52, 152, 219)   # 玩家颜色
RED = (231, 76, 60)     # 敌人颜色
GREEN = (46, 204, 113)  # 按钮颜色
GRAY = (50, 50, 50)     # 背景色
YELLOW = (241, 196, 15) # 文字高亮

# 字体
font = pygame.font.SysFont("arial", 24)
big_font = pygame.font.SysFont("arial", 36)

class Character:
    def __init__(self, name, x, y, color, max_hp, attack):
        self.name = name
        self.x = x
        self.y = y
        self.color = color
        self.max_hp = max_hp
        self.hp = max_hp
        self.attack = attack
        self.width = 60
        self.height = 60
        self.is_attacking = False
        self.attack_anim_timer = 0

    def draw(self, surface):
        # 简单的攻击动画位移
        draw_x = self.x
        if self.is_attacking:
            direction = 1 if self.name == "Hero" else -1
            draw_x += 20 * direction * (self.attack_anim_timer / 10)

        # 画身体
        pygame.draw.rect(surface, self.color, (draw_x, self.y, self.width, self.height))
        
        # 画名字
        name_text = font.render(self.name, True, WHITE)
        surface.blit(name_text, (draw_x + 5, self.y - 30))

        # 画血条背景
        pygame.draw.rect(surface, BLACK, (draw_x, self.y - 10, self.width, 8))
        # 画当前血量
        hp_width = int((self.hp / self.max_hp) * self.width)
        pygame.draw.rect(surface, GREEN, (draw_x, self.y - 10, hp_width, 8))

    def update_animation(self):
        if self.is_attacking:
            self.attack_anim_timer += 1
            if self.attack_anim_timer > 10:
                self.is_attacking = False
                self.attack_anim_timer = 0

    def take_damage(self, amount):
        self.hp -= amount
        if self.hp < 0: self.hp = 0

class Button:
    def __init__(self, text, x, y, w, h):
        self.text = text
        self.rect = pygame.Rect(x, y, w, h)
        self.color = GRAY

    def draw(self, surface):
        mouse_pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(mouse_pos):
            self.color = (80, 80, 80)
        else:
            self.color = GRAY
            
        pygame.draw.rect(surface, self.color, self.rect)
        pygame.draw.rect(surface, WHITE, self.rect, 2)
        
        text_surf = font.render(self.text, True, WHITE)
        text_rect = text_surf.get_rect(center=self.rect.center)
        surface.blit(text_surf, text_rect)

# --- 主程序 ---

def main():
    # 创建角色
    player = Character("Hero", 150, 300, BLUE, 100, 15)
    enemy = Character("Slime", 550, 300, RED, 80, 10)
    
    buttons = [
        Button("Attack (攻击)", 50, 500, 150, 50),
        Button("Heal (治疗)", 220, 500, 150, 50),
        Button("Run (逃跑)", 390, 500, 150, 50)
    ]

    log_messages = ["A wild Slime appeared!"]
    turn = "player" # player or enemy
    game_over = False

    running = True
    while running:
        clock.tick(60)
        screen.fill((30, 30, 40)) # 深色背景

        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            
            if event.type == pygame.MOUSEBUTTONDOWN and turn == "player" and not game_over:
                mx, my = event.pos
                # 检查按钮点击
                if buttons[0].rect.collidepoint(mx, my): # Attack
                    player.is_attacking = True
                    dmg = random.randint(player.attack - 5, player.attack + 5)
                    enemy.take_damage(dmg)
                    log_messages.append(f"You hit Slime for {dmg} dmg!")
                    turn = "enemy"
                
                elif buttons[1].rect.collidepoint(mx, my): # Heal
                    heal = 20
                    player.hp = min(player.max_hp, player.hp + heal)
                    log_messages.append(f"You healed for {heal} HP.")
                    turn = "enemy"

                elif buttons[2].rect.collidepoint(mx, my): # Run
                    log_messages.append("You ran away safely!")
                    game_over = True

        # 敌人回合逻辑 (简单延时模拟思考)
        if turn == "enemy" and not game_over:
            # 这里为了演示简单，直接让敌人行动，实际可以加个计时器延迟
            if not enemy.is_attacking and player.hp > 0: 
                 # 稍微延迟一下让动画看起来自然
                 pygame.time.wait(500) 
                 enemy.is_attacking = True
                 dmg = random.randint(enemy.attack - 3, enemy.attack + 3)
                 player.take_damage(dmg)
                 log_messages.append(f"Slime hits you for {dmg} dmg!")
                 turn = "player"

        # 更新动画
        player.update_animation()
        enemy.update_animation()

        # 检查死亡
        if enemy.hp <= 0 and not game_over:
            log_messages.append("Victory! Slime defeated.")
            game_over = True
        if player.hp <= 0 and not game_over:
            log_messages.append("Defeat... You fainted.")
            game_over = True

        # --- 绘图 ---
        
        # 画角色
        if enemy.hp > 0: enemy.draw(screen)
        if player.hp > 0: player.draw(screen)

        # 画UI面板
        pygame.draw.rect(screen, BLACK, (0, 450, WIDTH, 150))
        pygame.draw.line(screen, WHITE, (0, 450), (WIDTH, 450), 2)

        # 画按钮
        if not game_over:
            for btn in buttons:
                btn.draw(screen)

        # 画战斗日志
        for i, msg in enumerate(log_messages[-4:]): # 只显示最后4条
            text = font.render(msg, True, YELLOW if i == len(log_messages[-4:])-1 else WHITE)
            screen.blit(text, (20, 460 + i * 30))

        if game_over:
            over_text = big_font.render("GAME OVER", True, RED)
            screen.blit(over_text, (WIDTH//2 - 100, 200))

        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()