import pygame
import math

pygame.init()
WIDTH, HEIGHT = 1200, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("光的色散")

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PRISM_COLOR = (175, 225, 255)
SCREEN_COLOR = (80, 80, 80)
# 红、橙、黄、绿、蓝、靛、紫
rainbow = [
    (255, 0, 0),
    (255, 140, 0),
    (255, 255, 0),
    (0, 255, 0),
    (0, 110, 255),
    (80, 0, 150),
    (160, 0, 230)
]

# ===== 几何坐标 =====
# 倒三角棱镜（底边在上，顶点在下）
cx, cy = 480, 320
base_w = 230
prism_h = 210
p_left_top = (cx - base_w//2, cy - prism_h//2)
p_right_top = (cx + base_w//2, cy - prism_h//2)
p_bottom = (cx, cy + prism_h//2)
prism = [p_left_top, p_right_top, p_bottom]

# 入射光与出射点
light_in_start = (60, cy)
hit_in = (cx - base_w//2 + 20, cy)
hit_out = (cx + base_w//2 - 20, cy)

# 右侧光屏
screen_x = 1060
screen_w = 40
screen_y1 = 100
screen_y2 = 540

# 【关键】用pygame内置默认字体，不调用系统字体，避开海龟编辑器的SysFont报错
font = pygame.font.Font(None, 42)

# ===== 绘制完整画面 =====
screen.fill(BLACK)

# 1. 右侧灰色光屏
pygame.draw.rect(screen, SCREEN_COLOR, (screen_x, screen_y1, screen_w, screen_y2 - screen_y1))

# 2. 浅蓝填充三棱镜
pygame.draw.polygon(screen, PRISM_COLOR, prism)

# 3. 入射白光 + 棱镜内部过渡光线
pygame.draw.line(screen, WHITE, light_in_start, hit_in, 5)
pygame.draw.line(screen, (220, 220, 220), hit_in, hit_out, 4)

# 4. 七色出射光（红上紫下，扇形散开）
beam_len = 700
angle_step = 0.035
for i, color in enumerate(rainbow):
    angle = (i - 3) * angle_step
    ex = hit_out[0] + beam_len * math.cos(angle)
    ey = hit_out[1] + beam_len * math.sin(angle)
    pygame.draw.line(screen, color, hit_out, (ex, ey), 3)

# 5. 底部知识点文字
text1 = font.render("白光不是单色光，它由七种光色混合而成", True, WHITE)
text2 = font.render("这种现象叫——光的色散", True, WHITE)
screen.blit(text1, (WIDTH//2 - text1.get_width()//2, 580))
screen.blit(text2, (WIDTH//2 - text2.get_width()//2, 630))

pygame.display.flip()

# 仅保留关闭窗口功能，无任何交互
running = True
while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False

pygame.quit()
