ps,用turtle导入小海龟来画图。画图代码都要导入海龟库哦,见下面第一行。
import turtle as t
- 直线
# 画直线
def draw_line():pen = t.Pen()pen.fd(200) # 直线默认水平方向
draw_line()
- 画正方形
def draw_square():pen = t.Pen()for i in range(4): # 画四条等长直线pen.fd(200)pen.left(90)draw_square()
- 画长方形
def draw_rectangle():pen = t.Pen()for i in range(2): # 一长一宽画两遍pen.fd(100)pen.left(90)pen.fd(200)pen.left(90)draw_rectangle()
- 三角形
def triangle():pen = t.Pen()for i in range(3):pen.fd(200)pen.left(120)
triangle()
- 画圆
# 画圆
def draw_circle():pen = t.Pen()pen.circle(100) # 半径两百draw_circle()
- 画四个相交的圆
def draw_intersect_circle():pen = t.Pen()for i in range(2): # 先画上下,再画左右pen.circle(100)pen.circle(-100)pen.left(90)draw_intersect_circle()
- 五边形
def pentagon():pen = t.Pen()for i in range(5):pen.fd(100)pen.right(72)
pentagon()
- 五角星
def pentacle():pen = t.Pen()for i in range(5):pen.fd(100)pen.left(216)pentacle()
- 四边形
def draw_quadrangle():pen = t.Pen()for i in range(2): # 画两次pen.fd(200) pen.left(45) pen.fd(150) pen.left(135)draw_quadrangle()
- 正六边形
# 六边形的偏转度数60
def draw_hexagon():pen = t.Pen()for i in range(6):pen.fd(150)pen.left(60)draw_hexagon()
- 画各种正n边形系列
算出n变形要偏转的角度
把n变形切成三角形(180)+四边形(360),得到总角度
拿总的角度/边数 就是偏转的角度
- 画大奔
#turtle 的背景颜色
t.bgcolor("red")
def draw_gallop():pen = t.Pen()# 笔的宽度pen.width(5)# 笔的颜色pen.pencolor('pink')pen.left(95)pen.fd(200)for i in range(3):pen.left(170)pen.fd(200)pen.left(310)pen.fd(200)pen.left(85)pen.circle(210)draw_gallop()
```![](https://img-blog.csdnimg.cn/20200526220837354.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzgzMjgz,size_16,color_FFFFFF,t_70)