写一段Python生成爱心树代码,用Turtle库
时间: 2023-05-27 14:07:15 浏览: 127
python turtle爱心源码
import turtle
# 设置画布大小及背景色
turtle.setup(800, 600)
turtle.bgcolor("black")
# 定义画爱心的函数
def draw_heart(size):
turtle.color("red", "pink")
turtle.begin_fill()
turtle.left(45)
turtle.forward(size)
turtle.circle(size / 2, 180)
turtle.right(90)
turtle.circle(size / 2, 180)
turtle.forward(size)
turtle.end_fill()
# 定义画树枝的函数
def draw_branch(branch_length):
if branch_length > 5:
turtle.pensize(branch_length / 10)
turtle.forward(branch_length)
turtle.right(20)
draw_branch(branch_length - 15)
turtle.left(40)
draw_branch(branch_length - 15)
turtle.right(20)
turtle.backward(branch_length)
# 设置画笔初始位置
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
# 画树干
turtle.color("brown")
turtle.pensize(30)
turtle.left(90)
turtle.forward(150)
# 画树枝和爱心
turtle.color("green")
draw_branch(100)
draw_heart(30)
# 隐藏画笔
turtle.hideturtle()
# 显示绘图窗口
turtle.done()
阅读全文