用Python画一个圣诞树
时间: 2023-11-16 18:02:01 浏览: 90
用Python画一颗漂亮的圣诞树项目源码,圣诞节快到了送给你女朋友,有飘雪花的功能,含打包的EXE可执行文件,下载即用
好的,以下是Python画圣诞树的代码:
```python
# 导入必要的库
import turtle
# 设置画板
screen = turtle.Screen()
screen.bgcolor('white')
# 画树干
trunk = turtle.Turtle()
trunk.color('brown')
trunk.pensize(20)
trunk.penup()
trunk.setposition(0, -200)
trunk.pendown()
trunk.forward(50)
trunk.right(90)
trunk.forward(100)
trunk.right(90)
trunk.forward(50)
# 画树枝
branch = turtle.Turtle()
branch.color('green')
branch.pensize(5)
branch.penup()
branch.setposition(0, -100)
branch.pendown()
branch.right(90)
branch.forward(100)
branch.right(180)
# 画树叶
for i in range(5):
branch.right(40)
branch.forward(20)
branch.right(140)
branch.forward(20)
branch.right(40)
# 完成画图
turtle.done()
```
希望这个代码能够帮助到你!
阅读全文