python 绘制国旗的示例

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

实现效果如下:

青天白日旗

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

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)

实现效果如下:

红底白十字旗

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

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 * 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 绘制了几面国旗,有兴趣的可以尝试绘制一下其他国家的国旗。

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

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

(0)

相关推荐

  • 解决Python Matplotlib绘图数据点位置错乱问题

    在绘制正负样本在各个特征维度上的CDF(累积分布)图时出现了以下问题: 问题具体表现为: 1.几个负样本的数据点位置倒错 2.X轴刻度变成了乱七八糟一团鬼东西 最终解决办法 造成上述情况的原因其实是由于输入matplotlib.plot()函数的数据x_data和y_data从CSV文件中直接导入后格式为string,因此才会导致所有数据点的x坐标都被直接刻在了x轴上,且由于坐标数据格式错误,部分点也就表现为"乱点".解决办法就是导入x,y数据后先将其转化为float型数据,然后输入p

  • 给你一面国旗 教你用python画中国国旗

    本文实例为大家分享了python画中国国旗的具体代码,供大家参考,具体内容如下 # author : momo import turtle #中国国旗 turtle.up() turtle.goto(-200,200) turtle.down() turtle.begin_fill() turtle.fillcolor("red") turtle.pencolor("red") for i in range(2): turtle.forward(280) turtl

  • Python实现手绘图效果实例分享

    首先我们来看看原图: 接着我们来看看效果图: 通过分析我们不难发现以下特征: 主要颜色为黑白灰 边界线条较重 相同或相近色趋于白色 略有光源效果 需要用到的库有: numpy PIL 代码实现: import numpy as np from PIL import Image baseImg = Image.open("./img/myimg2.jpg").convert("L")  # 这里放置你要手绘的图片原图 a = np.array(baseImg).ast

  • Python Matplotlib绘图基础知识代码解析

    1.Figure和Subplot import numpy as np import matplotlib.pyplot as plt #创建一个Figure fig = plt.figure() #不能通过空figure绘图,必须使用add_subplot创建一个或多个subplot #图像为2x2,第三个参数为当前选中的第几个 ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot

  • python matplotlib 绘图 和 dpi对应关系详解

    我就废话不多说啦! dpi=1 600×400 dpi=2 1200×800 dpi=3 1800×1200 ........ dpi=21 (21×600)×(21×400) ---> 12600×8400 示例代码: ............... ............... plt_temp=y_axis plt_temp.resize(len(y_axis) , 1) plt_arr=np.concatenate((plt_arr,plt_temp ), axis=1) #print

  • python Plotly绘图工具的简单使用

    1.plotly库的相关介绍 1)相关说明 plotly是一个基于javascript的绘图库,plotly绘图种类丰富,效果美观: 易于保存与分享plotly的绘图结果,并且可以与Web无缝集成: ploty默认的绘图结果,是一个HTML网页文件,通过浏览器可以直接查看: 2)plotly与matplotlib.seaborn的关系   需要注意的是,ployly绘图库与matplotlib绘图库.seaborn绘图库并没有什么关系.也就是说说plotly是一个单独的绘图库,有自己独特的绘图语

  • Python三维绘图之Matplotlib库的使用方法

    前言 在遇到三维数据时,三维图像能给我们对数据带来更加深入地理解.python的matplotlib库就包含了丰富的三维绘图工具. 1.创建三维坐标轴对象Axes3D 创建Axes3D主要有两种方式,一种是利用关键字projection='3d'l来实现,另一种则是通过从mpl_toolkits.mplot3d导入对象Axes3D来实现,目的都是生成具有三维格式的对象Axes3D. #方法一,利用关键字 from matplotlib import pyplot as plt from mpl_

  • 使用Python的turtle模块画国旗

    Python的turtle模块画国旗主要用到两个函数:draw_rentangle和draw_star. 至于函数的调用就和我们学的C,C++是一样的.对于turtle画国旗的程序中,首先是查找国旗的画法,才能用程序实现.自己在实现的过程中主要是对turtle.circle()没有准确掌握,所以花了一些不必要的时间.turtle.circle画弧时,海龟(turtle)的方向就是弧的切线方向,也就是说turtle的垂直方向就是圆心在的直线上,给定参数radius就可以画了,程序中第二注意的地方就

  • Python绘图之二维图与三维图详解

    各位工程师累了吗? 推荐一篇可以让你技术能力达到出神入化的网站"持久男" 1.二维绘图 a. 一维数据集 用 Numpy ndarray 作为数据传入 ply 1. import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(10) print "y = %s"% y x =

  • 给我一面国旗 python帮你实现

    本文实例为大家分享了Python之给我一面国旗的具体代码,供大家参考,具体内容如下 1."给我一面国旗@微信官方" 今天"给我一面国旗@微信官方"刷爆了朋友圈,我也蹭波热度,出个Pythoon教程,原创作品感谢支持. 2.安装PIL模块 python2安装PIL模块 Python安装Pillow模块`pip3 install Pillow 3.准备一张背景图 注意中间要镂空,保存成GIF格式 下图是我自己PS的 4.解决思路 朋友圈下载有国旗的好友头像 查看属性分辨

  • python GUI库图形界面开发之PyQt5简单绘图板实例与代码分析

    在PyQt中常用的图像类有四种,QPixmap,QImage,QPicture,QBitmap 类型 描述 QPixmap 专门为绘图设计的,在绘制图片时需要使用QPixmap QImage 提供了一个与硬件无关的图像表示函数,可以用于图片像素级访问 QPicture 是一个绘图设备类,它继承自QPainter类,可以使用QPainter的begin()函数在QPicture上绘图,使用end()函数结束绘图,使用QPicture的save()函数将QPainter所使用的绘图指令保存在文件中

随机推荐