import pygame
import sys
import time

pygame.init()

WIDTH, HEIGHT = 900, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("白光三棱镜色散实验")
clock = pygame.time.Clock()

# 七色 + 名称
color_info = [
    ((255, 0, 0), "Red"),
    ((255, 140, 0), "Orange"),
    ((255, 255, 0), "Yellow"),
    ((0, 220, 0), "Green"),
    ((0, 100, 255), "Blue"),
    ((75, 0, 150), "Indigo"),
    ((148, 0, 211), "Violet"),
]
COLOR_WHITE = (255, 255, 255)
COLOR_GRAY = (60, 60, 60)
COLOR_BLACK = (0, 0, 0)

# 位置
prism_x, prism_y = 320, 250
screen_x = 720
start_y = 160
line_gap = 28
line_width = 4

# 字体：改用默认字体，规避 simhei 报错
font = pygame.font.Font(None, 22)

# 时序控制
show_index = -1
last_show_time = time.time()
interval = 0.35

running = True
while running:
    now = time.time()
    # 每0.35秒解锁下一条光线
    if now - last_show_time >= interval and show_index < 6:
        show_index += 1
        last_show_time = now

    screen.fill(COLOR_BLACK)

    # 画三棱镜
    prism_points = [
        (prism_x, prism_y - 90),
        (prism_x - 70, prism_y + 60),
        (prism_x + 70, prism_y + 60)
    ]
    pygame.draw.polygon(screen, (200, 220, 255), prism_points, 2)

    # 入射白光
    pygame.draw.line(screen, COLOR_WHITE, (80, prism_y), (prism_x, prism_y), 4)

    # 光屏
    pygame.draw.line(screen, COLOR_GRAY, (screen_x, 140), (screen_x, 360), 3)
    txt = font.render("Screen", True, (200,200,200))
    screen.blit(txt, (screen_x+10, 370))

    # 绘制已经出现的彩色光线
    for i in range(show_index + 1):
        c, name = color_info[i]
        y_pos = start_y + i * line_gap
        pygame.draw.line(screen, c, (prism_x, prism_y), (screen_x, y_pos), line_width)
        name_text = font.render(name, True, c)
        screen.blit(name_text, (screen_x + 12, y_pos - 12))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()
