python画一个圣诞树
时间: 2023-10-23 15:40:13 浏览: 98
圣诞节快到了,用python、turtle画棵圣诞树吧~(csdn)————程序.pdf
以下是Python代码,用于绘制一个圣诞树:
```
import turtle
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("#003366")
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
# 绘制树干
pen.penup()
pen.goto(-50, -200)
pen.pendown()
pen.setheading(90)
pen.forward(200)
# 绘制树枝
def draw_tree(branch_len, angle):
if branch_len > 5:
pen.forward(branch_len)
pen.right(angle)
draw_tree(branch_len - 15, angle)
pen.left(angle*2)
draw_tree(branch_len - 15, angle)
pen.right(angle)
pen.backward(branch_len)
pen.penup()
pen.goto(0, -150)
pen.pendown()
pen.color("green")
draw_tree(100, 30)
# 绘制圣诞球
pen.penup()
pen.goto(-70, -50)
pen.pendown()
pen.color("red")
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(70, -50)
pen.pendown()
pen.color("gold")
pen.begin_fill()
pen.circle(20)
pen.end_fill()
# 绘制星星
pen.penup()
pen.goto(0, 100)
pen.pendown()
pen.color("yellow")
pen.begin_fill()
for i in range(5):
pen.forward(50)
pen.right(144)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
运行代码后,将会看到一个绚丽多彩的圣诞树,如下所示:
![Christmas Tree](https://i.imgur.com/byfqwAz.png)
阅读全文