python四瓣花图形_使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
时间: 2024-03-12 18:44:27 浏览: 25
好的,我可以为您提供绘制樱花和玫瑰的代码实例,圣诞树的代码稍微有些复杂,需要更多的画图技巧,但我也可以尝试为您提供。以下是樱花和玫瑰的代码实例:
绘制樱花:
``` python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(width=800, height=600)
turtle.bgcolor("black")
# 定义绘制花朵的函数
def draw_flower(petal_num, petal_len, petal_width):
turtle.color("white")
for i in range(petal_num):
turtle.begin_fill()
turtle.circle(petal_len, 60)
turtle.left(120)
turtle.circle(petal_len, 60)
turtle.end_fill()
turtle.left(360/petal_num)
turtle.color("pink")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 随机生成樱花的位置和大小
for i in range(20):
x = random.randint(-400, 400)
y = random.randint(-300, 300)
petal_num = random.randint(5, 10)
petal_len = random.randint(50, 100)
petal_width = random.randint(5, 15)
# 移动画笔到指定位置
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
# 绘制樱花
draw_flower(petal_num, petal_len, petal_width)
# 隐藏画笔
turtle.hideturtle()
# 点击窗口关闭画布
turtle.exitonclick()
```
绘制玫瑰:
``` python
import turtle
# 设置画布大小和背景颜色
turtle.setup(width=800, height=600)
turtle.bgcolor("black")
# 定义绘制花朵的函数
def draw_flower(petal_num, petal_len, petal_width, petal_color):
turtle.color(petal_color)
turtle.begin_fill()
for i in range(petal_num):
turtle.circle(petal_len, 60)
turtle.left(120)
turtle.circle(petal_len, 60)
turtle.left(360/petal_num)
turtle.end_fill()
turtle.color("green")
turtle.begin_fill()
turtle.circle(petal_width)
turtle.end_fill()
# 设置画笔初始位置和颜色
turtle.penup()
turtle.goto(0, -200)
turtle.color("red")
# 绘制玫瑰的花瓣
turtle.pendown()
draw_flower(15, 100, 10, "red")
# 绘制玫瑰的花心
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.color("yellow")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 点击窗口关闭画布
turtle.exitonclick()
```
希望这些代码能够帮助您进行绘图。如果您有任何问题或需要更多帮助,请随时问我。