利用python的tur的turtle库可以轻松的画出各种图案,我觉的美队盾牌是比较有规则的,也比较简单,更适合新手,所以发出来和大家分享。
话不多说,直接上代码,注释内有详细解析:
import turtle as t
t.tracer(1.5)
'''
写一个方法,用来调整画笔位置
'''
def setpen():
t.left(90)
t.penup()
t.fd(25)
t.pendown()
t.right(90)
t.penup()
t.right(90)
t.fd(200)
t.pendown()
t.left(90)
# 先画一个大红圆
t.fillcolor("red") # 设置填充颜色
t.pencolor("red") # 设置画笔颜色
t.begin_fill()
t.circle(200) # 画园
t.end_fill()
setpen() # 利用写好的方法,调整画笔位置
# 再画一个白圆
t.fillcolor("white")
t.pencolor("white")
t.begin_fill()
t.circle(175)
t.end_fill()
setpen()
# 再画一个红圆
t.fillcolor("red")
t.pencolor("red")
t.begin_fill()
t.circle(150)
t.end_fill()
setpen()
# 再画一个蓝色的圆
t.fillcolor("blue")
t.pencolor("blue")
t.begin_fill()
t.circle(125)
t.end_fill()
# 画一个白色的五角星
t.fillcolor("white")
t.pencolor("white")
t.penup()
t.goto(-118,38)
t.pendown()
t.begin_fill()
for i in range(5): # 画五角星的五条边
t.forward(238)
t.right(144)
t.end_fill()
t.hideturtle()
t.done()
为了让大家更好的理解代码,我专门找了turtle库的中文文档,贴在下面,大家看到不熟悉的函数一定要多看文档。
https://www.rddoc.com/doc/Python/3.6.0/zh/library/turtle/?highlight=turtle