python表白代码画爱心树
时间: 2023-06-16 21:08:09 浏览: 190
python画爱心表白
好的,以下是一个简单的 Python 代码,可以画出一个爱心树:
```python
import math
def draw_heart(x, y):
# 画心形的函数
points = []
for i in range(0, 360):
radians = math.radians(i)
x = 16 * math.pow(math.sin(radians), 3)
y = 13 * math.cos(radians) - 5 * math.cos(2 * radians) - 2 * math.cos(3 * radians) - math.cos(4 * radians)
points.append((x, -y))
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('red')
turtle.begin_fill()
for point in points:
turtle.goto(x + point[0], y + point[1])
turtle.end_fill()
def draw_tree(x, y, length, angle):
# 画树的函数
if length < 10:
return
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(angle)
turtle.color('green')
turtle.pensize(length / 20)
turtle.forward(length)
draw_tree(turtle.xcor(), turtle.ycor(), length * 0.7, angle + 30)
draw_tree(turtle.xcor(), turtle.ycor(), length * 0.7, angle - 50)
draw_heart(turtle.xcor(), turtle.ycor())
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(angle)
turtle.color('brown')
turtle.pensize(length / 10)
turtle.backward(length * 0.1)
# 主函数
import turtle
turtle.speed(0)
turtle.hideturtle()
turtle.bgcolor('black')
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.color('white')
turtle.write("I love you", align="center", font=("Courier", 48, "bold"))
draw_tree(0, -100, 200, 90)
turtle.done()
```
将代码保存为 `love_tree.py`,然后在命令行中运行 `python love_tree.py`,就可以看到爱心树的画面了。
阅读全文