国旗是一个国家的象征,它可以反映一个国家的特色和传统,国旗起源于近代的欧洲,是一个国家主权意识不断增强后的必然产物,本文我们使用 Python 来画几面国旗,使用的 Python 库是大家比较熟悉的 turtle。
五星红旗五星红旗是中华人民共和国的国旗,它是由四颗小的黄五角星环绕一颗大的黄五角星组成的,底色为红色,实现代码如下:
turtle.setup(600,400,0,0)turtle.bgcolor("red")turtle.fillcolor("yellow")turtle.color('yellow')turtle.speed(10)# 主星turtle.begin_fill()turtle.up()turtle.goto(-280,100)turtle.down()for i in range (5): turtle.forward(150) turtle.right(144)turtle.end_fill()# 副星turtle.begin_fill()turtle.up()turtle.goto(-100,180)turtle.setheading(305)turtle.down()for i in range (5): turtle.forward(50) turtle.left(144)turtle.end_fill()turtle.begin_fill()turtle.up()turtle.goto(-50,110)turtle.setheading(30)turtle.down()for i in range (5): turtle.forward(50) turtle.right(144)turtle.end_fill()turtle.begin_fill()turtle.up()turtle.goto(-40,50)turtle.setheading(5)turtle.down()for i in range (5): turtle.forward(50) turtle.right(144)turtle.end_fill()turtle.begin_fill()turtle.up()turtle.goto(-100,10)turtle.setheading(300)turtle.down()for i in range (5): turtle.forward(50) turtle.left(144)turtle.end_fill()turtle.hIDeturtle()turtle.done()
实现效果如下:
红底白十字旗红底白十字旗是瑞士的国旗,与其他国家有点不同,瑞士的国旗形状是正方形的,代表了该国坚守中立的政策,实现代码如下:
def draw_crossshaped(aTurtle, wIDth=0, height=0, color=None): aTurtle = turtle.Turtle() aTurtle.hIDeturtle() aTurtle.penup() aTurtle.goto(30, 50) aTurtle.begin_fill() aTurtle.fillcolor(color) for i in range(4): aTurtle.pendown() aTurtle.fd(wIDth) aTurtle.rt(90) aTurtle.fd(height) aTurtle.rt(90) aTurtle.fd(wIDth) aTurtle.lt(90) aTurtle.end_fill()def draw_RQ(times=20.0): wIDth, height = 26 * times, 26 * times window = turtle.Screen() aTurtle = turtle.Turtle() aTurtle.hIDeturtle() aTurtle.speed(10) aTurtle.penup() aTurtle.goto(-wIDth / 2, height / 2) aTurtle.pendown() aTurtle.begin_fill() aTurtle.fillcolor('red') aTurtle.fd(wIDth) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.fd(wIDth) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.end_fill() draw_crossshaped(aTurtle, wIDth=80, height=80, color='white') window.exitonclick()
实现效果如下:
星条旗星条旗是美国的国旗,由两部分组成,旗的左上方蓝底上排列着 50 颗白色的星,其余部分是 13 道红白相间的条子,实现代码如下:
# 画条纹def drawSquar(): turtle.color('black', 'red') turtle.begin_fill() for i in range(7): turtle.forward(600) turtle.left(90) turtle.forward(350 / 13) turtle.left(90) turtle.forward(600) turtle.right(90) turtle.forward(350 / 13) turtle.right(90) turtle.end_fill()# 画左上角的小矩形def drawSmallsqure(): turtle.color('blue') turtle.begin_fill() turtle.left(90) turtle.forward(350 / 2) turtle.left(90) turtle.forward(300) turtle.left(90) turtle.forward(350 * 7 / 13) turtle.left(90) turtle.forward(300) turtle.end_fill()# 画左上角的星星def drawSrarts(): x = -10 y = 0 for k in range(4): x = -15 for i in range(6): turtle.goto(x, y) turtle.color('white') turtle.begin_fill() for j in range(5): turtle.left(144) turtle.forward(20) x -= 50 turtle.end_fill() y += 350 / 13 * 2 x = -10 y = 350 / 13 for i in range(3): x = -35 for j in range(5): turtle.goto(x, y) turtle.color('white') turtle.begin_fill() for k in range(5): turtle.left(144) turtle.forward(20) x -= 50 turtle.end_fill() y += 350 / 13 * 2turtle.setup(0.8, 0.8, -100, -100)turtle.speed(10)turtle.pu()turtle.forward(300)turtle.left(90)turtle.forward(350 / 2)turtle.left(90)drawSquar()turtle.home()drawSmallsqure()turtle.home()drawSrarts()turtle.hIDeturtle()turtle.done()
实现效果如下:
一点红旗一点红旗是日本的国旗,构成比较简单,就是在长方形白底中点一个大红点,实现代码如下:
turtle.setup(wIDth=600, height=400)# 设置画笔起点turtle.penup()turtle.goto(0, -50)turtle.pendown()# 设置画笔属性turtle.pensize(5)turtle.pencolor("red")turtle.fillcolor("red")# 绘制速度turtle.speed(10)turtle.begin_fill()turtle.circle(50)turtle.end_fill()turtle.hIDeturtle()turtle.mainloop()
实现效果如下:
源码在公众号 Python小二 后台回复 210127 获取。
总结本文非首发于个人号
以上是内存溢出为你收集整理的用 Python 画了几个国家的国旗全部内容,希望文章能够帮你解决用 Python 画了几个国家的国旗所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)