在Python中使用 Turtle 绘制一朵花
先决条件: Python中的海龟编程
Turtle 是Python的一个功能,就像画板一样,它可以让我们命令一只乌龟在上面画画!我们可以使用像turtle.forward(…)和turtle.right(…)这样的函数来移动海龟。 Turtle 是一种初学者友好的学习Python的方式,通过运行一些基本命令并以图形方式查看海龟。它就像一个画板,可以让你在上面画画。海龟模块可以以面向对象和面向过程的方式使用。
为了绘制, Python海龟提供了许多函数和方法,即前进、后退等。一些常用的方法是:
- forward(x):将笔向前移动 x 单位。
- 向后(x):将笔向后移动 x 单位。
- right(x):将笔顺时针方向旋转一个角度 x。
- left(x):将笔逆时针旋转一个角度x。
- penup():停止绘制乌龟笔。
- pendown():开始绘制乌龟笔。
在本文中,我们将借助 Turtle 编程编写绘制花朵的代码。如下图所示。
方法:
- 导入海龟模块
- 设置乌龟的速度
- 使用循环来避免不必要的代码重复。
- 用特定坐标绘制每个步骤
下面是实现:
示例 1:- 花
Python3
import turtle
tur = turtle.Turtle()
tur.speed(20)
tur.color("black", "orange")
tur.begin_fill()
for i in range(50):
tur.forward(300)
tur.left(170)
tur.end_fill()
turtle.done()
Python3
import turtle
# Set initial position
turtle.penup ()
turtle.left (90)
turtle.fd (200)
turtle.pendown ()
turtle.right (90)
# flower base
turtle.fillcolor ("red")
turtle.begin_fill ()
turtle.circle (10,180)
turtle.circle (25,110)
turtle.left (50)
turtle.circle (60,45)
turtle.circle (20,170)
turtle.right (24)
turtle.fd (30)
turtle.left (10)
turtle.circle (30,110)
turtle.fd (20)
turtle.left (40)
turtle.circle (90,70)
turtle.circle (30,150)
turtle.right (30)
turtle.fd (15)
turtle.circle (80,90)
turtle.left (15)
turtle.fd (45)
turtle.right (165)
turtle.fd (20)
turtle.left (155)
turtle.circle (150,80)
turtle.left (50)
turtle.circle (150,90)
turtle.end_fill ()
# Petal 1
turtle.left (150)
turtle.circle (-90,70)
turtle.left (20)
turtle.circle (75,105)
turtle.setheading (60)
turtle.circle (80,98)
turtle.circle (-90,40)
# Petal 2
turtle.left (180)
turtle.circle (90,40)
turtle.circle (-80,98)
turtle.setheading (-83)
# Leaves 1
turtle.fd (30)
turtle.left (90)
turtle.fd (25)
turtle.left (45)
turtle.fillcolor ("green")
turtle.begin_fill ()
turtle.circle (-80,90)
turtle.right (90)
turtle.circle (-80,90)
turtle.end_fill ()
turtle.right (135)
turtle.fd (60)
turtle.left (180)
turtle.fd (85)
turtle.left (90)
turtle.fd (80)
# Leaves 2
turtle.right (90)
turtle.right (45)
turtle.fillcolor ("green")
turtle.begin_fill ()
turtle.circle (80,90)
turtle.left (90)
turtle.circle (80,90)
turtle.end_fill ()
turtle.left (135)
turtle.fd (60)
turtle.left (180)
turtle.fd (60)
turtle.right (90)
turtle.circle (200,60)
turtle.done()
输出:
示例 2:
蟒蛇3
import turtle
# Set initial position
turtle.penup ()
turtle.left (90)
turtle.fd (200)
turtle.pendown ()
turtle.right (90)
# flower base
turtle.fillcolor ("red")
turtle.begin_fill ()
turtle.circle (10,180)
turtle.circle (25,110)
turtle.left (50)
turtle.circle (60,45)
turtle.circle (20,170)
turtle.right (24)
turtle.fd (30)
turtle.left (10)
turtle.circle (30,110)
turtle.fd (20)
turtle.left (40)
turtle.circle (90,70)
turtle.circle (30,150)
turtle.right (30)
turtle.fd (15)
turtle.circle (80,90)
turtle.left (15)
turtle.fd (45)
turtle.right (165)
turtle.fd (20)
turtle.left (155)
turtle.circle (150,80)
turtle.left (50)
turtle.circle (150,90)
turtle.end_fill ()
# Petal 1
turtle.left (150)
turtle.circle (-90,70)
turtle.left (20)
turtle.circle (75,105)
turtle.setheading (60)
turtle.circle (80,98)
turtle.circle (-90,40)
# Petal 2
turtle.left (180)
turtle.circle (90,40)
turtle.circle (-80,98)
turtle.setheading (-83)
# Leaves 1
turtle.fd (30)
turtle.left (90)
turtle.fd (25)
turtle.left (45)
turtle.fillcolor ("green")
turtle.begin_fill ()
turtle.circle (-80,90)
turtle.right (90)
turtle.circle (-80,90)
turtle.end_fill ()
turtle.right (135)
turtle.fd (60)
turtle.left (180)
turtle.fd (85)
turtle.left (90)
turtle.fd (80)
# Leaves 2
turtle.right (90)
turtle.right (45)
turtle.fillcolor ("green")
turtle.begin_fill ()
turtle.circle (80,90)
turtle.left (90)
turtle.circle (80,90)
turtle.end_fill ()
turtle.left (135)
turtle.fd (60)
turtle.left (180)
turtle.fd (60)
turtle.right (90)
turtle.circle (200,60)
turtle.done()
输出: