import pygame
import random

pygame.init()

# 窗口设置
WIDTH = 850
HEIGHT = 550
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)
PLAYER_CARD = (40, 120, 210)
PC_CARD = (210, 40, 40)
YELLOW = (255, 210, 0)
GREEN = (20, 180, 20)
RED = (220, 20, 20)

# 字体（修复注册表报错）
font = pygame.font.Font(None, 36)
small_font = pygame.font.Font(None, 28)

# 游戏数据
player_hand = []
pc_hand = []
player_score = 0
pc_score = 0
round_count = 0
max_round = 5
game_over = False
wait_click = False
selected_card = None
pc_show_card = 0

# 生成手牌 攻击力1‑10
for _ in range(5):
    player_hand.append(random.randint(1, 10))
    pc_hand.append(random.randint(1, 10))

running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            mx, my = pygame.mouse.get_pos()
            #选择手牌
            if not wait_click:
                for index in range(len(player_hand)):
                    card_x = 80 + index*110
                    card_y = 380
                    if card_x < mx < card_x+80 and card_y<my<card_y+100:
                        selected_card = player_hand.pop(index)
                        pc_show_card = pc_hand.pop(random.randint(0,len(pc_hand)-1))
                        #回合计分
                        if selected_card > pc_show_card:
                            player_score += 1
                        elif selected_card < pc_show_card:
                            pc_score += 1
                        round_count += 1
                        wait_click = True
                        if round_count >= max_round:
                            game_over = True
            else:
                wait_click = False

    #绘制画面
    screen.fill(WHITE)

    #电脑卡牌区
    if wait_click:
        pygame.draw.rect(screen, PC_CARD, (330, 120, 80, 100))
        text_pc = font.render(str(pc_show_card), True, WHITE)
        screen.blit(text_pc, (355, 155))
    else:
        pygame.draw.rect(screen, PC_CARD, (330, 120, 80, 100))

    #玩家打出的卡牌
    if wait_click:
        pygame.draw.rect(screen, PLAYER_CARD, (440, 280, 80, 100))
        text_player = font.render(str(selected_card), True, WHITE)
        screen.blit(text_player, (465, 315))

    #玩家手牌
    for i,atk in enumerate(player_hand):
        cx = 80 + i*110
        cy = 380
        pygame.draw.rect(screen, PLAYER_CARD, (cx, cy, 80, 100))
        num_text = font.render(str(atk), True, WHITE)
        screen.blit(num_text, (cx+25, cy+35))

    #UI文字
    round_text = font.render(f"回合：{round_count}/{max_round}",True,BLACK)
    score_text = font.render(f"玩家:{player_score}  电脑:{pc_score}",True,BLACK)
    tip_text = small_font.render("点击手牌出牌，点击屏幕继续下一回合",True,BLACK)
    screen.blit(round_text,(30,20))
    screen.blit(score_text,(30,60))
    if not wait_click and not game_over:
        screen.blit(tip_text,(30,100))

    #结算最终胜负
    if game_over:
        if player_score > pc_score:
            result = font.render("恭喜！你获得了胜利！",True,GREEN)
        elif player_score < pc_score:
            result = font.render("很遗憾，电脑获胜",True,RED)
        else:
            result = font.render("本局对战平局！",True,BLACK)
        screen.blit(result,(WIDTH//2-170,HEIGHT//2))

    pygame.display.flip()

pygame.quit()
