Python绘制樱花关键词
时间: 2023-07-02 22:16:25 浏览: 106
以下是Python绘制樱花关键词的示例代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
canvas = turtle.Screen()
canvas.setup(800, 600)
canvas.bgcolor("#f8f8f8")
# 定义樱花关键词
keywords = ["爱", "美丽", "幸福", "温馨", "浪漫", "快乐", "感动", "美好", "微笑"]
# 定义樱花花瓣颜色
colors = ["pink", "lightpink", "hotpink", "deeppink"]
# 定义绘制樱花的函数
def draw_cherry_blossom(x, y):
# 随机选择花瓣颜色和大小
color = random.choice(colors)
size = random.randint(10, 20)
# 绘制花瓣
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(size)
turtle.end_fill()
# 绘制樱花关键词
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.color("#e60073")
turtle.write("樱花关键词:", font=("Arial", 20, "bold"))
turtle.penup()
turtle.goto(-200, 200)
turtle.pendown()
for keyword in keywords:
turtle.write(keyword, font=("Arial", 16))
turtle.penup()
turtle.fd(100)
turtle.pendown()
# 绘制樱花
turtle.speed(0)
for i in range(100):
x = random.randint(-400, 400)
y = random.randint(-300, 200)
draw_cherry_blossom(x, y)
# 隐藏画笔
turtle.hideturtle()
# 显示画布
turtle.done()
```
运行代码后,你就可以看到一个绘制了樱花关键词和樱花花瓣的窗口。每次运行代码,樱花花瓣的位置、颜色和大小都会随机生成,所以每次的效果都不一样。
阅读全文