使用Python的turtle模块画国旗

 更新时间:2019年09月24日 14:22:24   作者:嗨学编程  
这篇文章主要为大家详细介绍了用Python的turtle模块画国旗,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Python的turtle模块画国旗主要用到两个函数:draw_rentangle和draw_star。

至于函数的调用就和我们学的C,C++是一样的。对于turtle画国旗的程序中,首先是查找国旗的画法,才能用程序实现。自己在实现的过程中主要是对turtle.circle()没有准确掌握,所以花了一些不必要的时间。turtle.circle画弧时,海龟(turtle)的方向就是弧的切线方向,也就是说turtle的垂直方向就是圆心在的直线上,给定参数radius就可以画了,程序中第二注意的地方就是小五角星和大五角星的位置关系,主要是程序中的turtle.left(turtle.towards(center_x,center_y)-turtle.heading()),当然,我看有的人用了round()函数来获取近似值,但是,默认的已经足够了。下面是本人写的程序和结果演示。

import time
import turtle
import os
'''
想要学习Python?Python学习交流群:984632579满足你的需求,资料都已经上传群文件,可以自行下载!
'''
def draw_rectangle(start_x,start_y,rec_x,rec_y):
 turtle.goto(start_x,start_y)
 turtle.color('red')
 turtle.fillcolor('red')
 turtle.begin_fill()
 for i in range(2):
  turtle.forward(rec_x)
  turtle.left(90)
  turtle.forward(rec_y)
  turtle.left(90)
 turtle.end_fill()
 
 
 
 
def draw_star(center_x,center_y,radius):
 turtle.setpos(center_x,center_y)
 #find the peak of the five-pointed star
 pt1=turtle.pos()
 turtle.circle(-radius,72)
 pt2=turtle.pos()
 turtle.circle(-radius,72)
 pt3=turtle.pos()
 turtle.circle(-radius,72)
 pt4=turtle.pos()
 turtle.circle(-radius,72)
 pt5=turtle.pos()
 #draw the five-pointed star
 turtle.color('yellow','yellow')
 turtle.fill(True)
 turtle.goto(pt3)
 turtle.goto(pt1)
 turtle.goto(pt4)
 turtle.goto(pt2)
 turtle.goto(pt5)
 turtle.fill(False)
 
 
#start the project
turtle.speed(5)
turtle.penup()
#draw the rectangle
star_x=-320
star_y=-260
len_x=660
len_y=440
draw_rectangle(star_x,star_y,len_x,len_y)
#draw the big star
pice=660/30
big_center_x=star_x+5*pice
big_center_y=star_y+len_y-pice*5
turtle.goto(big_center_x,big_center_y)
turtle.left(90)
turtle.forward(pice*3)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice*3)
#draw the small star
turtle.goto(star_x+10*pice,star_y+len_y-pice*2)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the second star
turtle.goto(star_x+pice*12,star_y+len_y-pice*4)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the third
turtle.goto(star_x+pice*12,star_y+len_y-7*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the final
turtle.goto(star_x+pice*10,star_y+len_y-9*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
 
 
turtle.ht()
time.sleep(3)
os._exit(1)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python流程控制 if else实现解析

    Python流程控制 if else实现解析

    这篇文章主要介绍了Python 流程控制 if else实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python自动化之如何利用allure生成测试报告

    python自动化之如何利用allure生成测试报告

    这篇文章主要给大家介绍了关于python自动化之如何利用allure生成测试报告的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Python IDLE设置清屏快捷键的方法详解

    Python IDLE设置清屏快捷键的方法详解

    这篇文章主要为大家详细介绍了Python IDLE设置清屏快捷键的方法,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的可以了解一下
    2022-09-09
  • 利用Seaborn绘制20个精美的pairplot图

    利用Seaborn绘制20个精美的pairplot图

    本文记录的使用seaborn绘制pairplot图,主要是用来显示两两变量之间的关系(线性或非线性,有无较为明显的相关关系等),感兴趣的可以了解一下
    2022-07-07
  • Python实现读取目录所有文件的文件名并保存到txt文件代码

    Python实现读取目录所有文件的文件名并保存到txt文件代码

    这篇文章主要介绍了Python实现读取目录所有文件的文件名并保存到txt文件代码,本文分别使用os.listdir和os.walk实现给出两段实现代码,需要的朋友可以参考下
    2014-11-11
  • python迭代器实例简析

    python迭代器实例简析

    这篇文章主要介绍了python迭代器,以一个简单实例的形式分析了Python中迭代器的用法及注意事项,需要的朋友可以参考下
    2014-09-09
  • python 回溯法模板详解

    python 回溯法模板详解

    今天小编就为大家分享一篇python 回溯法模板详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Pytorch中accuracy和loss的计算知识点总结

    Pytorch中accuracy和loss的计算知识点总结

    在本片文章里小编给大家整理的是关于Pytorch中accuracy和loss的计算相关知识点内容,有需要的朋友们可以学习下。
    2019-09-09
  • 基于python实现操作git过程代码解析

    基于python实现操作git过程代码解析

    这篇文章主要介绍了基于python实现操作git过程代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • django序列化serializers过程解析

    django序列化serializers过程解析

    这篇文章主要介绍了django序列化serializers过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12

最新评论