import pygame
import random
import sys

# 初始化
pygame.init()
W, H = 700, 450
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("随机点名器")
clock = pygame.time.Clock()
FPS = 60

# 颜色
BG_COLOR = (25, 25, 35)
TEXT_COLOR = (255, 255, 255)
BTN_COLOR = (45, 120, 220)
BTN_HOVER = (70, 150, 255)
GRAY = (100, 100, 100)

# 字体兼容中文
try:
    font_big = pygame.font.Font("simhei.ttf", 80)
    font_mid = pygame.font.Font("simhei.ttf", 32)
    font_small = pygame.font.Font("simhei.ttf", 22)
except:
    font_big = pygame.font.SysFont("Microsoft YaHei", 80)
    font_mid = pygame.font.SysFont("Microsoft YaHei", 32)
    font_small = pygame.font.SysFont("Microsoft YaHei", 22)

# 名单 可自行修改这里的名字列表
name_list = [
    "张三", "李四", "王五", "赵六", "钱七",
    "孙八", "周九", "吴十", "郑十一", "陈十二",
    "小明", "小红", "小刚", "小丽", "78"
]
origin_list = name_list.copy()
used = []
running_name = ""
is_rolling = False

# 按钮区域
btn_start = pygame.Rect(80, 320, 160, 60)
btn_reset = pygame.Rect(280, 320, 160, 60)
btn_clear = pygame.Rect(480, 320, 160, 60)

def draw_btn(rect, text, mouse_pos):
    color = BTN_HOVER if rect.collidepoint(mouse_pos) else BTN_COLOR
    pygame.draw.rect(screen, color, rect, border_radius=8)
    txt = font_mid.render(text, True, TEXT_COLOR)
    tx = rect.centerx - txt.get_width()//2
    ty = rect.centery - txt.get_height()//2
    screen.blit(txt, (tx, ty))

def pick_name():
    global running_name
    if name_list:
        running_name = random.choice(name_list)

while True:
    mouse_pos = pygame.mouse.get_pos()
    screen.fill(BG_COLOR)

    # 事件
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if e.type == pygame.MOUSEBUTTONDOWN and e.button == 1:
            # 开始滚动
            if btn_start.collidepoint(mouse_pos):
                if not is_rolling:
                    is_rolling = True
                else:
                    # 停止点名
                    is_rolling = False
                    if running_name in name_list:
                        name_list.remove(running_name)
                        used.append(running_name)
            # 重置全部名单
            if btn_reset.collidepoint(mouse_pos):
                name_list = origin_list.copy()
                used.clear()
                running_name = ""
                is_rolling = False
            # 清空已点名记录
            if btn_clear.collidepoint(mouse_pos):
                used.clear()

    # 滚动时持续刷新名字
    if is_rolling:
        pick_name()

    # 显示当前抽取姓名
    if running_name:
        name_surf = font_big.render(running_name, True, TEXT_COLOR)
        screen.blit(name_surf, (W//2 - name_surf.get_width()//2, 100))
    else:
        tip = font_mid.render("点击开始随机点名", True, GRAY)
        screen.blit(tip, (W//2 - tip.get_width()//2, 130))

    # 按钮绘制
    draw_btn(btn_start, "开始/停止", mouse_pos)
    draw_btn(btn_reset, "重置名单", mouse_pos)
    draw_btn(btn_clear, "清空记录", mouse_pos)

    # 底部显示剩余人数与已点名
    info1 = font_small.render(f"剩余人数：{len(name_list)}", True, GRAY)
    info2 = font_small.render(f"已点名：{','.join(used)}", True, GRAY)
    screen.blit(info1, (20, 400))
    screen.blit(info2, (20, 425))

    pygame.display.update()
    clock.tick(12)