How to write code in Python to draw snowflakes?
Here is a simple Python code example for drawing a snowflake shape:
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 800)
turtle.bgcolor("black")
# 设置画笔属性
turtle.color("white")
turtle.penup()
# 雪花形状
snowflake = [
(90, 60),
(-135, 120),
(60, 120),
(-135, 60),
(90, 0),
(-135, 0),
(90, 60)
]
# 绘制雪花
for _ in range(30):
x = random.randint(-400, 400)
y = random.randint(-400, 400)
turtle.goto(x, y)
turtle.pendown()
for angle, length in snowflake:
turtle.setheading(angle)
turtle.forward(length)
turtle.penup()
# 关闭画布
turtle.done()
After running the code, you will see a white snowflake graphic on a black background. This example uses the Turtle library for drawing, creating multiple snowflake graphics by randomly generating coordinates. You can adjust parameters such as canvas size, color, and snowflake shape as needed.