在Python中使用 Turtle 图形绘制菱形形状
在本文中,我们将学习如何在Python中使用海龟图形绘制钻石的形状。
海龟图:
- forward(length):将笔向前移动 x 单位。
- right(angle):将笔顺时针方向旋转一个角度x。
- left(angle) : 将笔逆时针旋转角度 x。
方法:
- 导入海龟模块。
- 为海龟定义一个实例。
- 首先,制作更大的三角形
- 然后在更大的三角形内画三条线
- 然后做4个小三角形
- 然后在这四个三角形上方画一条线
以下是上述方法的Python实现:
Python3
# 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)
输出: