import turtle
import time

# ========== 窗口设置 ==========
screen = turtle.Screen()
screen.setup(920, 620)
screen.bgcolor("#0E1526")
screen.title("三棱镜色散动画")
screen.tracer(0)

pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()

# ========== 基础绘图函数 ==========
def draw_line(x1, y1, x2, y2, color, width=3):
    pen.goto(x1, y1)
    pen.pendown()
    pen.color(color)
    pen.width(width)
    pen.goto(x2, y2)
    pen.penup()

def draw_polygon(points, fill_color, outline="white"):
    pen.goto(points[0])
    pen.color(outline, fill_color)
    pen.begin_fill()
    pen.pendown()
    for p in points[1:]:
        pen.goto(p)
    pen.goto(points[0])
    pen.end_fill()
    pen.penup()

def write_text(x, y, text, color="white", size=16):
    pen.goto(x, y)
    pen.color(color)
    pen.write(text, align="center", font=("微软雅黑", size, "normal"))

# ========== 绘制白光 ==========
draw_line(-380, 60, -200, 20, "white", width=5)
write_text(-330, 90, "白光", "white", 16)

# ========== 绘制三棱镜（浅蓝色三角形）==========
prism = [(-180, -140), (-80, 140), (20, -140)]
draw_polygon(prism, "#8FD3FF", "#BCEBFF")
write_text(-80, -180, "三棱镜", "#BCEBFF", 16)

# ========== 绘制光屏（灰色）==========
pen.goto(300, -220)
pen.color("gray")
pen.begin_fill()
pen.pendown()
for _ in range(2):
    pen.goto(420, -220)
    pen.goto(420, 220)
    pen.goto(300, 220)
pen.end_fill()
pen.penup()
write_text(360, 250, "光屏", "gray", 16)

# ========== 色散光线数据 ==========
colors = [
    ("红", "red",      180),
    ("橙", "orange",   150),
    ("黄", "yellow",   120),
    ("绿", "green",    90),
    ("蓝", "#1E90FF",  60),
    ("靛", "#4B0082",  30),
    ("紫", "#8B00FF",  0),
]

# 入射点（三棱镜右表面）
start_x, start_y = 10, 0

# ========== 动画：逐条出现色散光线 ==========
for name, color, end_y in colors:
    draw_line(start_x, start_y, 300, end_y, color, width=3)
    write_text(215, end_y + 12, name, color, 14)
    screen.update()
    time.sleep(0.35)

# ========== 最终文字 ==========
write_text(0, -260,
           "白光不是单色光，它由七种色光混合而成",
           "white", 18)

write_text(0, -290,
           "这种现象叫——光的色散",
           "#FFD700", 20)

screen.update()

# ========== 保持窗口 ==========
turtle.done()