import pygame
import random
import time

# ====================
# 初始化
# ====================
pygame.init()
WIDTH, HEIGHT = 500, 350
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("随机点名器")

font_big = pygame.font.SysFont("simhei", 44)
font_btn = pygame.font.SysFont("simhei", 26)
clock = pygame.time.Clock()

# ====================
# 名单（可自行修改）
# ====================
names = [
    "张三", "李四", "王五", "赵六",
    "陈七", "孙八", "周九", "吴十"
]

current_name = "点击开始"
rolling = False
last_time = 0

start_btn = pygame.Rect(100, 250, 120, 50)
stop_btn = pygame.Rect(280, 250, 120, 50)

# ====================
# 主循环
# ====================
running = True
while running:
    screen.fill((25, 25, 35))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            if start_btn.collidepoint(event.pos):
                rolling = True
            if stop_btn.collidepoint(event.pos):
                rolling = False

    # ====================
    # 滚动名字
    # ====================
    if rolling:
        now = time.time()
        if now - last_time > 0.05:
            current_name = random.choice(names)
            last_time = now

    # ====================
    # 绘制
    # ====================
    title = font_big.render("随机点名器", True, (255, 255, 255))
    screen.blit(title, (150, 30))

    name_text = font_big.render(current_name, True, (0, 255, 150))
    screen.blit(name_text, (WIDTH // 2 - name_text.get_width() // 2, 120))

    # 开始按钮
    pygame.draw.rect(screen, (0, 180, 100), start_btn)
    start_text = font_btn.render("开始", True, (255, 255, 255))
    screen.blit(start_text, (start_btn.x + 35, start_btn.y + 12))

    # 停止按钮
    pygame.draw.rect(screen, (220, 80, 80), stop_btn)
    stop_text = font_btn.render("停止", True, (255, 255, 255))
    screen.blit(stop_text, (stop_btn.x + 35, stop_btn.y + 12))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
