用python画一个圣诞树
时间: 2023-04-05 16:02:08 浏览: 102
圣诞节快到了,用python、turtle画棵圣诞树吧~(csdn)————程序.pdf
下面是一个在 Python 中画圣诞树的简单示例代码:
```
# 导入所需的库
import turtle
# 初始化画布和画笔
turtle.setup(400, 500)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
# 画出圣诞树的主体
turtle.color("dark green")
turtle.begin_fill()
turtle.left(90)
turtle.forward(300)
turtle.circle(40, 180)
turtle.forward(300)
turtle.left(90)
turtle.end_fill()
# 画出圣诞树的顶部
turtle.color("dark green")
turtle.begin_fill()
turtle.left(180)
turtle.forward(80)
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(80)
turtle.end_fill()
# 画出圣诞树上的星星
turtle.color("yellow")
turtle.begin_fill()
turtle.left(90)
turtle.forward(20)
turtle.left(45)
turtle.forward(14)
turtle.left(90)
turtle.forward(14)
turtle.left(45)
turtle.forward(20)
turtle.end_fill()
# 画出圣诞树上的装饰品
turtle.color("red")
turtle.begin_fill()
turtle.left(180)
turtle.forward(20)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(10)
turtle.end_fill()
turtle.color("red")
turtle.begin_fill()
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(10)
turtle.end_fill()
# 画完后,等待用户点击关闭窗口
turtle.exitonclick()
```
希望这个示例能够帮助到你。
阅读全文