在Python中使用Turtle图形绘制钻石形状
在这篇文章中,我们将学习如何在Python中使用Turtle图形绘制钻石的形状。
Turtle 图形:
- forward(length): 向前移动笔的方向,以x为单位。
- right(angle): 将笔沿顺时针方向旋转一个角度x。
- left(angle) : 将笔沿逆时针方向旋转一个角度x。
步骤:
- 导入Turtle模块。
- 为Turtle定义一个实例。
- 首先,制作更大的三角形
- 然后在较大的三角形内做三条线
- 然后做4个小三角形
- 然后在这四个三角形的上方做一条线
以下是上述方法的Python实现:
# python program
# import for turtle module
import turtle
# defining instance of turtle
pen = turtle.Turtle()
wn = turtle.Screen()
# this is for bigger triangle
pen.left(60)
pen.forward(200)
pen.left(120)
pen.forward(200)
pen.left(120)
pen.forward(200)
pen.left(150)
# this for making three lines
# inside the bigger triangle
pen.forward(174)
pen.backward(174)
pen.left(16.5)
pen.forward(180)
pen.backward(180)
pen.right(31.5)
pen.forward(180)
pen.right(75)
# this is for making upper triangle1
pen.forward(53)
pen.left(120)
pen.forward(50)
pen.left(120)
pen.forward(50)
# this is for making upper triangle2
pen.right(120)
pen.forward(50)
pen.left(120)
pen.forward(50)
# this is for making upper triangle3
pen.right(120)
pen.forward(50)
pen.left(120)
pen.forward(50)
# this is for making upper triangle1
pen.right(120)
pen.forward(50)
pen.left(120)
pen.forward(50)
pen.left(180)
pen.forward(50)
# this is for making line above all 4 small triangle
pen.left(300)
pen.forward(160)
输出: