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网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
Apr 25 Python
一个基于flask的web应用诞生 使用模板引擎和表单插件(2)
Apr 11 Python
selenium+python 对输入框的输入处理方法
Oct 11 Python
解决Python一行输出不显示的问题
Dec 03 Python
Python3安装pip工具的详细步骤
Oct 14 Python
Python如何基于smtplib发不同格式的邮件
Dec 30 Python
pytorch 归一化与反归一化实例
Dec 31 Python
Python socket处理client连接过程解析
Mar 18 Python
python中读入二维csv格式的表格方法详解(以元组/列表形式表示)
Apr 24 Python
keras和tensorflow使用fit_generator 批次训练操作
Jul 03 Python
Python爬虫之App爬虫视频下载的实现
Dec 08 Python
Python实现随机生成迷宫并自动寻路
Jun 13 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通用检测函数集合
2006/11/25 PHP
PHP 时间转换Unix时间戳代码
2010/01/22 PHP
PHP中date与gmdate的区别及默认时区设置
2014/05/12 PHP
ThinkPHP模板中判断volist循环的最后一条记录的验证方法
2014/07/01 PHP
使用Composer安装Yii框架的方法
2016/03/15 PHP
php + nginx项目中的权限详解
2017/05/23 PHP
PHP实现的AES双向加密解密功能示例【128位】
2018/09/03 PHP
PDO::beginTransaction讲解
2019/01/27 PHP
JavaScript中constructor()方法的使用简介
2015/06/05 Javascript
javascript实现确定和取消提示框效果
2015/07/10 Javascript
jquery+css3实现会动的小圆圈效果
2016/01/27 Javascript
angularJs关于指令的一些冷门属性详解
2016/10/24 Javascript
javaScript+turn.js实现图书翻页效果实例代码
2017/02/16 Javascript
原生JS实现日历组件的示例代码
2017/09/22 Javascript
JS实现获取汉字首字母拼音、全拼音及混拼音的方法
2017/11/14 Javascript
AngularJS实现的2048小游戏功能【附源码下载】
2018/01/03 Javascript
Vue 通过自定义指令回顾v-内置指令(小结)
2018/09/03 Javascript
vue实现带过渡效果的下拉菜单功能
2020/02/19 Javascript
Python中的列表生成式与生成器学习教程
2016/03/13 Python
Python中基础的socket编程实战攻略
2016/06/01 Python
python利用lxml读写xml格式的文件
2017/08/10 Python
Python 实现购物商城,含有用户入口和商家入口的示例
2017/09/15 Python
解决nohup执行python程序log文件写入不及时的问题
2019/01/14 Python
Python进程,多进程,获取进程id,给子进程传递参数操作示例
2019/10/11 Python
Pycharm Git 设置方法
2020/09/15 Python
python之pygame模块实现飞机大战完整代码
2020/11/29 Python
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
2021/03/03 Python
加拿大最大的体育用品、鞋类和服装零售商:Sport Chek
2018/11/29 全球购物
公司财务总监岗位职责
2013/12/14 职场文书
学习新党章思想汇报
2014/01/09 职场文书
学习雷锋演讲稿
2014/05/10 职场文书
上党课的心得体会
2014/09/02 职场文书
2015年结对帮扶工作总结
2015/05/04 职场文书
公司回复函格式
2015/07/14 职场文书
2016学习医德医风心得体会
2016/01/25 职场文书
HTML中的表格元素介绍
2022/02/28 HTML / CSS