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()

你可能感兴趣的:(python,开发语言)