多棵圣诞树代码编程python下雪
时间: 2023-12-21 07:32:10 浏览: 108
python圣诞树代码编程
以下是一个使用Turtle库在Python中绘制多棵圣诞树并下雪的代码示例:
```python
from turtle import *
from random import *
import math
# 设置画布大小和背景颜色
setup(800, 600)
bgcolor("black")
# 定义绘制圣诞树的函数
def draw_tree(x, y, color):
# 移动到绘制起始点
penup()
goto(x, y)
pendown()
# 设置画笔颜色和填充颜色
pencolor(color)
fillcolor(color)
# 绘制树干
begin_fill()
setheading(90)
forward(100)
right(90)
forward(20)
right(90)
forward(40)
right(90)
forward(20)
right(90)
forward(100)
end_fill()
# 绘制树冠
setheading(0)
forward(60)
setheading(120)
fillcolor("green")
begin_fill()
circle(40, 120)
setheading(-120)
circle(40, 120)
end_fill()
# 绘制多棵圣诞树
colors = ["#006400", "#228B22", "#008000", "#00FF00", "#32CD32"]
for i in range(5):
x = randint(-300, 300)
y = randint(-200, 200)
color = choice(colors)
draw_tree(x, y, color)
# 下雪效果
hideturtle()
speed(0)
penup()
goto(-400, 250)
pendown()
color("white")
for i in range(120):
forward(10)
backward(10)
right(30)
# 结束绘制
done()
```
阅读全文