python 绘制国旗的示例


Posted in Python onSeptember 27, 2020

国旗是一个国家的象征,它可以反映一个国家的特色和传统,国旗起源于近代的欧洲,是一个国家主权意识不断增强后的必然产物,本文我们使用 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()

实现效果如下:

python 绘制国旗的示例

青天白日旗

青天白日旗是民国时期的国旗,旗面作蓝色以示青天,旗中置一射出叉光的白日图案,实现代码如下:

t.colormode(255)
rcblue=(4,0,174)
rcred=(254,0,0)
def ol(r):
  na = 15 / 180 * math.pi
  ol=2*r*math.cos(na)
  ol=int(round(ol))
  return ol
def loop(r):
  t.fd(ol(r))
  t.right(150)
def main0(a,b):
  t.color(rcred)
  t.penup()
  t.goto(-a/2,b/2)
  t.pendown()
  t.begin_fill()
  t.goto(-a/2,-b/2)
  t.goto(a/2,-b/2)
  t.goto(a/2,b/2)
  t.end_fill()
  t.penup()
  t.goto(-a/4,b/4)
  t.pendown()
def main1(a1,b1):
  t.color('gray',rcblue)
  t.penup()
  t.right(90)
  t.fd(b1/2)
  t.left(90)
  t.pendown()
  t.begin_fill()
  t.fd(a1/2)
  t.left(90)
  t.fd(b1)
  t.left(90)
  t.fd(a1)
  t.left(90)
  t.fd(b1)
  t.left(90)
  t.fd(a1/2)
  t.end_fill()
  t.penup()
  t.goto(-a/4,b/4)
  t.seth(0)
  t.pendown()
def main2(r):
  t.pensize = 20
  t.color('white', 'white')
  t.penup()
  t.fd(r)
  t.right(180 - 30 / 2)
  t.pendown()
  t.begin_fill()
  for i in range(12):
    loop(r)
  t.end_fill()
  t.penup()
  t.goto(-a/4,b/4)
  t.seth(0)
  t.pendown()
def main3(r1,r2):
  t.color(rcblue, rcblue) 
  t.begin_fill()
  t.up()
  t.right(90)
  t.fd(r1)
  t.left(90)
  t.pd()
  t.circle(r1)
  t.end_fill()
  t.penup()
  t.goto(-a/4,b/4)
  t.pendown()
  t.color('white', 'white')
  t.begin_fill()
  t.pu()
  t.right(90)
  t.fd(r2)
  t.left(90)
  t.pd()
  t.circle(r2)
  t.end_fill()
  t.penup()
  t.goto(-a/4,b/4)
  t.seth(0)
  t.pendown()
def main(a,b):
  a1 = a / 2
  b1 = b / 2
  r = a1 / 4
  r2 = a1 / 8
  r1 = b1 * 17 / 80
  main0(a,b)
  main1(a1,b1)
  main2(r)
  main3(r1,r2)
a=1020
b=680
t.setup(1100,700,100,0)

实现效果如下:

python 绘制国旗的示例

红底白十字旗

红底白十字旗是瑞士的国旗,与其他国家有点不同,瑞士的国旗形状是正方形的,代表了该国坚守中立的政策,实现代码如下:

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()

实现效果如下:

python 绘制国旗的示例

星条旗

星条旗是美国的国旗,由两部分组成,旗的左上方蓝底上排列着 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 * 2
turtle.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()

实现效果如下:

python 绘制国旗的示例

总结

本文我们使用 Python 绘制了几面国旗,有兴趣的可以尝试绘制一下其他国家的国旗。

示例代码:https://github.com/JustDoPython/python-examples/tree/master/yeke/py-flag

以上就是python 绘制国旗的示例的详细内容,更多关于python 绘制国旗的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python greenlet实现原理和使用示例
Sep 24 Python
在Python中使用PIL模块对图片进行高斯模糊处理的教程
May 05 Python
Flask框架的学习指南之开发环境搭建
Nov 20 Python
python利用rsa库做公钥解密的方法教程
Dec 10 Python
用Python读取几十万行文本数据
Dec 24 Python
pycharm 实现显示project 选项卡的方法
Jan 17 Python
python中利用numpy.array()实现俩个数值列表的对应相加方法
Aug 26 Python
Python多线程爬取豆瓣影评API接口
Oct 22 Python
python GUI库图形界面开发之PyQt5计数器控件QSpinBox详细使用方法与实例
Feb 28 Python
利用Python优雅的登录校园网
Oct 21 Python
pycharm2021激活码使用教程(永久激活亲测可用)
Mar 30 Python
Python find()、rfind()方法及作用
Dec 24 Python
python把一个字符串切开的实例方法
Sep 27 #Python
python实现图片素描效果
Sep 26 #Python
Python:__eq__和__str__函数的使用示例
Sep 26 #Python
Kmeans均值聚类算法原理以及Python如何实现
Sep 26 #Python
python实现sm2和sm4国密(国家商用密码)算法的示例
Sep 26 #Python
Python爬取股票信息,并可视化数据的示例
Sep 26 #Python
如何利用python发送邮件
Sep 26 #Python
You might like
PHP OPCode缓存 APC详细介绍
2010/10/12 PHP
php防注入,表单提交值转义的实现详解
2013/06/10 PHP
php给每个段落添加空格的方法
2015/03/20 PHP
php微信开发之带参数二维码的使用
2016/08/03 PHP
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
2007/06/29 Javascript
javascript RadioButtonList获取选中值
2009/04/09 Javascript
jQuery 性能优化指南(2)
2009/05/21 Javascript
jQuery 常见操作实现方式和常用函数方法总结
2011/05/06 Javascript
jquery+css3实现会动的小圆圈效果
2016/01/27 Javascript
利用Angularjs和Bootstrap前端开发案例实战
2016/08/27 Javascript
js HTML5手机刮刮乐代码
2020/09/29 Javascript
js倒计时显示实例
2016/12/11 Javascript
原生JS实现层叠轮播图
2017/05/17 Javascript
Python深入学习之上下文管理器
2014/08/31 Python
讲解Python中if语句的嵌套用法
2015/05/14 Python
python中MethodType方法介绍与使用示例
2017/08/03 Python
Python解决八皇后问题示例
2018/04/22 Python
python3.x实现发送邮件功能
2018/05/22 Python
Python基于sklearn库的分类算法简单应用示例
2018/07/09 Python
对python3.4 字符串转16进制的实例详解
2019/06/12 Python
Django框架创建mysql连接与使用示例
2019/07/29 Python
简单了解python中的f.b.u.r函数
2019/11/02 Python
Python类中的装饰器在当前类中的声明与调用详解
2020/04/15 Python
python爬虫实例之获取动漫截图
2020/05/31 Python
Python基于smtplib协议实现发送邮件
2020/06/03 Python
python创建文本文件的简单方法
2020/08/30 Python
美国儿童运动鞋和服装零售商:Kids Foot Locker
2017/08/05 全球购物
美国隐形眼镜网:Major Lens
2018/02/09 全球购物
世界上最大的字体市场:MyFonts
2020/01/10 全球购物
网友共享的几个面试题关于Java和Unix等方面的
2016/09/08 面试题
如何提高MySql的安全性
2014/06/19 面试题
大学生四年生活自我鉴定
2013/11/21 职场文书
新书吧创业计划书
2014/01/31 职场文书
兼职安全员岗位职责
2015/02/15 职场文书
python常见的占位符总结及用法
2021/07/02 Python
vue elementUI批量上传文件
2022/04/26 Vue.js