python樱花树爱心代码
时间: 2023-10-05 18:04:34 浏览: 141
以下是Python樱花树爱心代码:
```python
import turtle
import random
# 设置画布和画笔
t = turtle.Turtle()
t.hideturtle()
t.getscreen().bgcolor("#e6f3ff")
t.color("white")
t.speed(0)
t.penup()
t.goto(0, 200)
t.pendown()
# 画爱心
def draw_heart(size):
t.begin_fill()
t.left(45)
t.forward(size)
t.circle(size * 0.7, 180)
t.right(90)
t.circle(size * 0.7, 180)
t.forward(size)
t.end_fill()
# 画樱花
def draw_cherry_blossom(x, y):
t.penup()
t.goto(x, y)
t.pendown()
t.color("pink")
t.begin_fill()
t.circle(random.randint(5, 15))
t.end_fill()
t.penup()
t.goto(x + random.randint(-10, 10), y + random.randint(5, 15))
t.pendown()
t.begin_fill()
t.circle(random.randint(5, 15))
t.end_fill()
t.penup()
t.goto(x + random.randint(-10, 10), y + random.randint(5, 15))
t.pendown()
t.begin_fill()
t.circle(random.randint(5, 15))
t.end_fill()
# 主程序
for i in range(20):
draw_heart(random.randint(100, 150))
t.penup()
t.goto(random.randint(-200, 200), random.randint(-200, 200))
t.pendown()
for j in range(random.randint(3, 8)):
draw_cherry_blossom(t.xcor(), t.ycor())
turtle.done()
```
运行代码后,你将看到一个樱花树和许多爱心。
阅读全文