您的位置:首页 > 编程语言 > Python开发

Python中用turtle来画中国国旗

2019-07-27 10:02 225 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_37232647/article/details/97486766

之前有看到过有大哥级人物用turtle来画出小猪佩奇,作为刚入门的小白来说,画小猪佩奇还是比较难的,于是就画了个比较简单的(中国国旗)上上手。

在这里呢,我是首先画出了中国国旗的外部矩形边框,采用了2:1的设置来进行,长:宽=400:200,长与宽的设置也可以自己设置,一般都是2:1以及3:2。

import turtle as t
t.up()#提笔
t.goto(-325,0)#提笔至(-325,0)象限内
t.down()#落笔
t.begin_fill()#  开始填充
t.fillcolor("red") #填充颜色
t.forward(400)#向前移动400个像素点
t.left(90)#左转90度
t.forward(200)
t.left(90)
t.forward(400)
t.left(90)
t.forward(200)
t.end_fill()

在外部矩形画好之后,接下里就是开始画里面的大五角星,在画入里面的大五角星可以先进行提笔与落笔等语句,虽然每次这样写很麻烦,要是不写则会从上个点到下个点中间会有一条直线。

#大五角星
t.lt(90)
t.up()
t.goto(-300,150)
t.down()
t.right(36)
t.forward(25)
t.begin_fill()#开始填充颜色
t.fillcolor("yellow")
for _ in range(5): #用循环来画出五角星的路径
t.right(72)
t.forward(25)
t.left(144)
t.forward(25)
t.end_fill()

继而在画完大五角星之后,转而对最上方小五角星进行画出

#小五角星1
t.up()
t.goto(-220,185)
t.down()
t.right(36)
t.forward(10)
t.begin_fill()
t.fillcolor("yellow")
for _ in range(5):
t.right(72)
t.forward(10)
t.left(144)
t.forward(10)
t.end_fill()

在这里的话,因为每个五角星的角度都是不一样的,所以我就用简单粗暴的方式相对写出了剩余的3个。

#小五角星2
t.up()
t.goto(-200, 155)
t.down()
t.lt(36)
t.right(36)
t.forward(10)
t.begin_fill()
t.fillcolor("yellow")
for _ in range(5):
t.right(72)
t.forward(10)
t.left(144)
t.forward(10)
t.end_fill()
#五角星3
t.up()
t.goto(-190, 125)
t.down()
t.right(36)
t.forward(10)
t.begin_fill()#  开始填充
t.fillcolor("yellow") #填充颜色
for _ in range(5):
t.right(72)
t.forward(10)
t.left(144)
t.forward(10)
t.end_fill()
#小五角星4
t.up()
t.goto(-220, 100)
t.down()
t.lt(72)
t.right(36)
t.forward(10)
t.begin_fill()
t.fillcolor("yellow")
for _ in range(5):
t.right(72)
t.forward(10)
t.left(144)
t.forward(10)
t.end_fill()
t.done()

对于初学者的我还是先画画简单的好,以下是我整理部分的turtle语法:
import turtle#导入模块
turtle.forward(x) #向前移动x个像素
turtle.left(x) #左转x度
turtle.pensize(5) # 画笔的粗细
turtle.pencolor(“green”)# 画笔颜色
turtle.begin_fill()# 开始填充
turtle.fillcolor(“red”) #填充颜色
turtle.end_fill() #结束填充
turtle.circle(x) # 画一个半径为x的圆
turtle.speed(x) # 设置画笔速度为x
turtle.setup(x, y) # 设置主窗口的大小为x*y
turtle.colormode(255) # 设置GBK颜色范围为0-255
turtle.seth(90) # 笔的角度为90度

小白第一次写博客,不太有经验,若有错误的地方,请指出一下給。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: