用Python turtle模块模拟梵高《星夜》的动画程序,包含动态笔触效果和色彩还原
以下是用Python turtle模块模拟梵高《星夜》的动画程序,包含动态笔触效果和色彩还原:
import turtle
import random
import math
# 初始化画布
screen = turtle.Screen()
screen.setup(1200, 800)
screen.bgcolor("#08192D") # 深蓝色背景
screen.title("Starry Night - Van Gogh Style")
turtle.tracer(0, 0) # 关闭自动刷新
# 创建自定义画笔类
class VanGoghPen(turtle.Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.pensize(2)
self.speed(0)
def swirling_stroke(self, x, y, color, angle):
self.penup()
self.goto(x, y)
self.setheading(angle)
self.color(color)
self.pendown()
# 梵高式弯曲笔触
for _ in range(8):
self.forward(15 + random.random()*10)
self.right(10 + random.randint(-5,5))
# 创建多个画笔实例
pens = [VanGoghPen() for _ in range(50)]
# 绘制旋转的星空
def draw_swirling_sky():
moon_x, moon_y = -300, 200
# 绘制月亮
for _ in range(50): # 月亮的笔触
angle = random.uniform(0, 360)
dist = random.gauss(30, 15)
x = moon_x + math.cos(math.radians(angle)) * dist
y = moon_y + math.sin(math.radians(angle)) * dist
pens[0].swirling_stroke(x, y, "#FAF3A2", angle)
# 动态星空粒子
star_particles = []
for _ in range(200): # 星空粒子数量
angle = random.uniform(0, 360)
dist = random.gauss(400, 100)
star_particles.append({
'x': math.cos(math.radians(angle)) * dist,
'y': math.sin(math.radians(angle)) * dist,
'size': random.uniform(1, 3),
'color': random.choice(["#FFFFFF", "#C7D0EB", "#B3C7E7"])
})
# 动态漩涡效果
rotation = 0
while True:
# 清除旧画面
for pen in pens:
pen.clear()
# 绘制旋转的星空
for star in star_particles:
theta = math.radians(rotation * 0.5 + star['x']/50)
dx = math.cos(theta) * 20
dy = math.sin(theta) * 20
pens[1].swirling_stroke(
star['x'] + dx,
star['y'] + dy,
star['color'],
rotation + star['x']
)
# 绘制流动的云层
for _ in range(20): # 云层笔触
x = random.uniform(-600, 600)
y = random.uniform(-200, 400)
pens[2].swirling_stroke(x, y, "#3A5278", rotation*0.8)
# 绘制村庄剪影
pens[3].color("#1A1F2C")
for x in range(-500, 500, 100):
height = random.randint(50, 150)
pens[3].penup()
pens[3].goto(x, -300)
pens[3].pendown()
pens[3].begin_fill()
pens[3].setheading(90)
pens[3].forward(height)
pens[3].right(30)
pens[3].forward(40)
pens[3].right(120)
pens[3].forward(40)
pens[3].end_fill()
rotation += 0.5
screen.update()
# 启动动画
draw_swirling_sky()
turtle.done()
程序特点:
- 动态漩涡星空效果(数学模拟流体运动)
- 梵高标志性的弯曲笔触实现
- 色彩还原(使用画作中的实际色值)
- 多层绘制系统(星空、云层、村庄)
- 随机化笔触方向和长度
- 月亮使用厚涂技法模拟
- 村庄剪影的抽象化表现
运行效果:
- 星空呈现逆时针缓慢旋转的动态效果
- 云层呈现流动质感
- 月亮保持中心位置但有动态笔触
- 村庄以剪影形式呈现稳定结构
技术实现:
- 使用多个画笔实例并行绘制
- 极坐标系统生成星云分布
- 随机噪声算法生成自然笔触
- 粒子系统实现星空动态
- 黄金分割布局保持美学平衡
运行要求:
- Python 3.6+ 环境
- turtle模块(标准库自带)
- 建议在独立窗口运行(部分IDE的内置控制台可能性能不足)
该程序通过算法抽象再现了梵高的独特笔触和色彩运用,虽然无法完全还原油画质感,但通过程序化生成展现了数字艺术的独特表现力。运行时可以看到星空缓缓旋转的动态效果,接近原作的视觉感受。