使用Python Turtle绘制逼真的雪花效果

import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("black")
# 定义雪花的形状
turtle.register_shape("snowflake", ((0, 0), (0, 10), (-5, 5), (0, 10), (5, 5), (0, 10)))
# 定义雪花的颜色列表
colors = ["white", "lightblue", "lightcyan", "lavender", "snow"]
# 循环绘制雪花
for i in range(100):
    # 随机生成雪花的位置和大小
    x = random.randint(-400, 400)
    y = random.randint(-300, 300)
    size = random.randint(1, 4)
    # 随机选择雪花的颜色
    color = random.choice(colors)
    # 创建雪花
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.color(color)
    turtle.shape("snowflake")
    turtle.shapesize(size)
    turtle.setheading(random.randint(0, 360))
    turtle.stamp()
# 隐藏海龟
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
物联沃分享整理
物联沃-IOTWORD物联网 » 使用Python Turtle绘制逼真的雪花效果

发表评论