import pygame
import sys

# --- 1. 初始化 ---
pygame.init()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 51, 153)
GRAY = (220, 220, 220)
RED = (255, 0, 0)

# 窗口设置
CARD_W, CARD_H = 500, 300  # 稍微加大一点窗口，方便观察
screen = pygame.display.set_mode((CARD_W, CARD_H))
pygame.display.set_caption("学生证预览 (按 S 保存图片)")

# --- 2. 准备数据 ---
student_info = {
    "school": "极速开发大学",
    "name": "张三",
    "id": "202408001",
    "major": "人工智能工程",
    "date": "2024年09月"
}

# 加载字体 (确保系统有中文字体)
try:
    # Windows: SimHei, Mac: Arial Unicode MS / STHeiti
    font_title = pygame.font.SysFont("SimHei", 32, bold=True)
    font_info = pygame.font.SysFont("SimHei", 20)
except:
    font_title = pygame.font.SysFont("arial", 32)
    font_info = pygame.font.SysFont("arial", 20)

def draw_card():
    """在屏幕上绘制证件内容"""
    screen.fill((100, 100, 100)) # 绘制一个深灰色背景，突出证件本身
    
    # 绘制证件白底
    card_rect = pygame.Rect(50, 25, 400, 250)
    pygame.draw.rect(screen, WHITE, card_rect)
    pygame.draw.rect(screen, BLACK, card_rect, 2) # 外边框
    
    # 1. 顶部蓝色条
    pygame.draw.rect(screen, BLUE, (50, 25, 400, 60))
    
    # 2. 学校名称
    title_surf = font_title.render(student_info["school"], True, WHITE)
    screen.blit(title_surf, (70, 38))
    
    # 3. 照片占位符
    photo_rect = pygame.Rect(80, 100, 100, 130)
    pygame.draw.rect(screen, GRAY, photo_rect)
    pygame.draw.rect(screen, BLACK, photo_rect, 1)
    
    # 照片文字提示
    label_photo = font_info.render("照片", True, (150, 150, 150))
    screen.blit(label_photo, (110, 155))
    
    # 4. 学生具体信息
    start_x = 200
    start_y = 110
    gap = 35
    
    info_list = [
        f"姓  名:  {student_info['name']}",
        f"学  号:  {student_info['id']}",
        f"专  业:  {student_info['major']}",
        f"入  学:  {student_info['date']}"
    ]
    
    for i, text in enumerate(info_list):
        txt_surf = font_info.render(text, True, BLACK)
        screen.blit(txt_surf, (start_x, start_y + i * gap))
        
    # 5. 绘制一个红色的装饰印章
    pygame.draw.circle(screen, RED, (380, 210), 35, 2)
    stamp_txt = font_info.render("核准", True, RED)
    screen.blit(stamp_txt, (360, 200))

# --- 3. 主循环 ---
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                # 截取证件部分并保存
                # sub_surface 的范围对应 card_rect 的位置
                card_image = screen.subsurface((50, 25, 400, 250))
                pygame.image.save(card_image, "student_card_export.png")
                print("证件已保存为 student_card_export.png")

    # 绘制
    draw_card()
    
    # 提示文字（在证件下方）
    hint = font_info.render("按 'S' 键保存证件图片", True, WHITE)
    screen.blit(hint, (130, 280))
    
    pygame.display.flip()

pygame.quit()
sys.exit()