Python画圣诞树
时间: 2023-12-25 10:29:14 浏览: 127
Python画圣诞树
5星 · 资源好评率100%
以下是使用Python绘制圣诞树的示例代码:
```python
import turtle
# 设置画布和画笔
screen = turtle.Screen()
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
pen.color("green")
# 绘制圣诞树
def draw_tree():
pen.penup()
pen.goto(0, -200)
pen.pendown()
pen.setheading(90)
pen.forward(200)
draw_branch(100, 60)
def draw_branch(branch_length, angle):
if branch_length < 10:
pen.color("green")
else:
pen.color("brown")
if branch_length > 5:
pen.forward(branch_length)
pen.right(angle)
draw_branch(branch_length * 0.7, angle)
pen.left(angle * 2)
draw_branch(branch_length * 0.7, angle)
pen.right(angle)
pen.backward(branch_length)
draw_tree()
# 绘制雪花
def draw_snow():
pen.penup()
pen.goto(-248, -100)
pen.pendown()
pen.color("white")
pen.pensize(2)
for i in range(80):
pen.goto(randint(-248, 248), randint(-100, 248))
petal_number = 6
snow_size = int(randint(2, 10))
for j in range(petal_number):
pen.forward(snow_size)
pen.backward(snow_size)
pen.right(360 / petal_number)
draw_snow()
turtle.done()
```
阅读全文