使用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 OpenCV之图片缩放的实现(cv2.resize)

    Python OpenCV之图片缩放的实现(cv2.resize)

    这篇文章主要介绍了Python OpenCV之图片缩放的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Python实现前向和反向自动微分的示例代码

    Python实现前向和反向自动微分的示例代码

    自动微分技术(称为“automatic differentiation, autodiff”)是介于符号微分和数值微分的一种技术,它是在计算效率和计算精度之间的一种折衷。本文主要介绍了Python如何实现前向和反向自动微分,需要的可以参考一下
    2022-12-12
  • 使用Windows批处理和WMI设置Python的环境变量方法

    使用Windows批处理和WMI设置Python的环境变量方法

    今天小编就为大家分享一篇使用Windows批处理和WMI设置Python的环境变量方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • python 边缘扩充方式的实现示例

    python 边缘扩充方式的实现示例

    本文主要介绍了python 边缘扩充方式的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Python中内置函数filter函数用法详解

    Python中内置函数filter函数用法详解

    filter()函数是Python内置的另一个有用的高阶函数,filter()函数接收一个函数f和一个序列,函数f的作用是对每个元素进行判断,返回True或False,下面这篇文章主要给大家介绍了关于Python中内置函数filter函数用法的相关资料,需要的朋友可以参考下
    2024-05-05
  • Python多进程Process和管道Pipe的使用方式

    Python多进程Process和管道Pipe的使用方式

    这篇文章主要介绍了Python多进程Process和管道Pipe的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python内置库之webbrowser模块用法详解

    Python内置库之webbrowser模块用法详解

    webbrowser模块是Python自带的标准库,无需安装,可以直接在Python中使用该模块来打开网页、PDF文件等,本文给大家详细介绍了Python webbrowser模块用法,需要的朋友可以参考下
    2023-08-08
  • Django框架视图层URL映射与反向解析实例分析

    Django框架视图层URL映射与反向解析实例分析

    这篇文章主要介绍了Django框架视图层URL映射与反向解析,结合实例形式分析了Django框架普通url映射、命名URL参数映射、分布式URL映射、反向解析等相关操作技巧,需要的朋友可以参考下
    2019-07-07
  • 2021年的Python 时间轴和即将推出的功能详解

    2021年的Python 时间轴和即将推出的功能详解

    这篇文章主要介绍了2021年的Python 时间轴和即将推出的功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • NVIDIA安装CUDA的实现(图文教程)

    NVIDIA安装CUDA的实现(图文教程)

    本文主要介绍了NVIDIA安装CUDA的实现,包括系统要求、软件下载、安装步骤以及常见问题解决,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01

最新评论