今天主要讲一下怎么通过python和turtle绘制国旗。先看效果图。

python turtle 绘制对联(python和turtle绘制国旗)(1)

运行效果图

第一步:通过cad或caxa绘制出国旗形状(必须按照国家标准规定的尺寸进行)。

python turtle 绘制对联(python和turtle绘制国旗)(2)

用cad或caxa绘制出国旗

第二步:通过cad或caxa将需要绘制的国旗尺寸进行标注,通过python绘制时需要用到。

python turtle 绘制对联(python和turtle绘制国旗)(3)

标注尺寸

第三步:打开pycharm,创建python文件,结合第二步的尺寸定义画布坐标(大小)并实现绘制国旗代码(调用turtle库)的编写,实现绘制国旗的效果。

import turtle t=turtle.Pen() t.hideturtle() #隐藏画笔的形状 #画国旗背景图 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) t.pendown() t.begin_fill() t.forward(960) t.right(90) t.forward(640) t.right(90) t.forward(960) t.right(90) t.forward(640) t.end_fill() #绘制中心五角星 t.penup() t.goto(-320,256) t.right(162) t.pendown() t.color('yellow') t.fillcolor('yellow') t.begin_fill() t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #绘制右上第一个小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第二个小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第三个小五角星 t.penup() t.goto(-96,128) t.right(170.13) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第四个小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) t.pendown() t.fillcolor('yellow') t.begin_fill() t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我爱你中国",font=("Times",30,"bold"))

第四步:通过观察代码,发现绘制五角星时,其中有几步是重复的,可以通过for语句进行循环,减少代码数量。

import turtle as t #隐藏画笔的形状 t.hideturtle() #设置画布大小 t.screensize(1,1) #画国旗背景图 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) t.pendown() t.begin_fill() t.forward(960) t.right(90) t.forward(640) t.right(90) t.forward(960) t.right(90) t.forward(640) t.end_fill() #绘制中心五角星 t.penup() t.goto(-320,256) t.right(162) t.pendown() t.color('yellow') t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循环4次 t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #绘制右上第一个小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循环4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第二个小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循环4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第三个小五角星 t.penup() t.goto(-96,128) t.right(170.13) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循环4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #绘制右上第四个小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1,5):#循环4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我爱你中国",font=("Times",30,"bold"))

第五步:再次观察代码,可以通过将for语句创建为函数,并通过调用函数的形式绘制五角星,以此达到再次优化代码的效果。

import turtle as t #隐藏画笔的形状 t.hideturtle() #设置画布大小 t.screensize(1,1) #设置背景循环函数 def bj(): t.pendown() t.begin_fill() for i in range(1,3): t.forward(960) t.right(90) t.forward(640) t.right(90) t.end_fill() #设置中心五角星循环函数 def wj1(): t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1, 5): # 循环4次 t.forward(69.7) t.left(72) t.forward(69.7) t.right(144) t.forward(69.7) t.left(72) t.forward(69.7) t.end_fill() #设置小五角星循环函数 def wj2(): t.pendown() t.fillcolor('yellow') t.begin_fill() for i in range(1, 5): # 循环4次 t.forward(23.2) t.left(72) t.forward(23.2) t.right(144) t.forward(23.2) t.left(72) t.forward(23.2) t.end_fill() #画国旗背景图 t.color('red') t.fillcolor('red') t.penup() t.goto(-480,320) bj() #绘制中心五角星 t.penup() t.goto(-320,256) t.right(72) t.color('yellow') wj1() #绘制右上第一个小五角星 t.penup() t.goto(-147.5,285.4) t.right(167.04) wj2() #绘制右上第二个小五角星 t.penup() t.goto(-110.1,220.7) t.right(94.83) wj2() #绘制右上第三个小五角星 t.penup() t.goto(-96,128) t.right(170.13) wj2() #绘制右上第四个小五角星 t.penup() t.goto(-148.7,61.9) t.right(164.66) wj2() #添加文字 t.penup() t.color('red') t.goto(-100,-380) t.pendown() t.write("我爱你中国",font=("Times",30,"bold"))

,