一、日常啰嗦
昨天是不是被微信票圈的“请给我一面五星红旗@微信官方”刷屏了,很多小伙伴都上当了吧,哈哈。后来Danny实在看不下去了,就给小伙伴们出了正确的制作五星红旗头像的教程,不知道小伙伴有没有制作呢?今早我又看了一下那个制作五星红旗头像的活动,发现活动已经结束了。应该还有很多人想制作没来得及吧,不过不没关系,虽然没有五星红旗头像了,但是咱有五星红旗啊。今天Danny就带小伙伴们学习如何使用Python画高清五星红旗。
Talk is cheap, show Danny the code!
二、先导入小乌龟包
import turtle as t
三、先做一些初始化的操作(新建画布,设置笔速,填充画笔的颜色等等)
# 长和宽的比值
a = 3.0
b = 2.0
c = 1 / 30
x = 200
# 新建画布并设置画布大小
t.setup(width=a * x, height=b * x)
# 画国旗背景
# 画笔颜色
t.pencolor("red")
# 填充颜色
t.fillcolor("red")
# 画笔速度
t.speed(0)
# 提起画笔
t.penup()
# 画笔开始位置
t.goto(-0.5 * a * x, 0.5 * b * x)
# 开始填充
t.begin_fill()
四、先画五星红旗背景
for n1 in range(2):
t.forward(a * x)
t.right(90)
t.forward(b * x)
t.right(90)
t.end_fill()

这样五星红旗的背景就画好了。
五、编写画五角星的方法
# 画五角星
def draw_star(goto, forward, heading):
t.penup()
t.goto(goto[0], goto[1])
t.begin_fill()
t.down()
t.setheading(heading)
for n2 in range(5):
t.forward(forward)
t.right(144)
t.end_fill()
六、在画好的背景后面,紧接着调用五次画五角星的方法画五个五角星
# 长和宽的比值
a = 3.0
b = 2.0
c = 1 / 30
x = 200
# 新建画布并设置画布大小
t.setup(width=a * x, height=b * x)
# 画五星红旗背景
# 画笔颜色
t.pencolor("red")
# 填充颜色
t.fillcolor("red")
# 画笔速度
t.speed(0)
# 提起画笔
t.penup()
# 画笔开始位置
t.goto(-0.5 * a * x, 0.5 * b * x)
# 开始填充
t.begin_fill()
for n1 in range(2):
t.forward(a * x)
t.right(90)
t.forward(b * x)
t.right(90)
t.end_fill()
time.sleep(100000)
# 开始画五个星星
t.pencolor("yellow")
t.fillcolor("yellow")
t.speed(3)
# 画第一个星星
draw_star((a * x * (-0.5 + 2 * c), b * x * 0.3), a * x * 6 * c, 0)
# 画第二个星星
draw_star((a * x * (-0.5 + 9 * c), b * x * 0.4), a * x * 2 * c, 45)
# 画第三个星星
draw_star((a * x * (-0.5 + 11 * c), b * x * 0.3), a * x * 2 * c, 30)
# 画第四个星星
draw_star((a * x * (-0.5 + 11 * c), b * x * 0.15), a * x * 2 * c, 0)
# 画第五个星星
draw_star((a * x * (-0.5 + 9 * c), 0), a * x * 2 * c, 45)
# 隐藏小海龟
t.hideturtle()
t.mainloop()

到此五星红旗就画好了,学会了就不用去某站找图高清国旗图片发票圈为祖国庆生了,更不会涉及到侵权了,因为咱可以自己使用Python画国旗。另外,使用Python还可以画其它的你想画的东西,后面Danny会继续给大家出教程。
对于没有编程基础的小伙伴来说可能有些难度,但是代码量不大,若感兴趣的话可以在文末扫码关注“DannyWu博客”公众号回复“五星红旗”下载源代码专研专代码或者直接使用我打包好的windows平台软件(下载后可直接打开运行),这样就能自己画高清国旗图啦。赶紧试试吧!


我的微信
有问题微信找我
07/12/2020 18:46 沙发
sfvcs