使用Python的turtle模块画国旗


Posted in Python onSeptember 24, 2019

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的turtle模块画国旗

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

Python 相关文章推荐
Python实现抓取百度搜索结果页的网站标题信息
Jan 22 Python
Python 稀疏矩阵-sparse 存储和转换
May 27 Python
Pycharm更换python解释器的方法
Oct 29 Python
python批量下载网站马拉松照片的完整步骤
Dec 05 Python
详解Python3 基本数据类型
Apr 19 Python
python中metaclass原理与用法详解
Jun 25 Python
使用python切片实现二维数组复制示例
Nov 26 Python
python ubplot使用方法解析
Jan 10 Python
Python常用数字处理基本操作汇总
Sep 10 Python
Python ConfigParser模块的使用示例
Oct 12 Python
Python通过getattr函数获取对象的属性值
Oct 16 Python
Python自动化工具之实现Excel转Markdown表格
Apr 08 Python
给你一面国旗 教你用python画中国国旗
Sep 24 #Python
Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法
Sep 24 #Python
Python获取时间戳代码实例
Sep 24 #Python
Python django框架输入汉字,数字,字符生成二维码实现详解
Sep 24 #Python
分享一个pycharm专业版安装的永久使用方法
Sep 24 #Python
python实现的config文件读写功能示例
Sep 24 #Python
python使用socket实现的传输demo示例【基于TCP协议】
Sep 24 #Python
You might like
php实现的百度搜索某地天气的小偷代码
2014/04/23 PHP
CI框架常用方法小结
2016/05/17 PHP
利用laravel搭建一个迷你博客实战教程
2017/08/13 PHP
PHP常用日期加减计算方法实例小结
2018/07/31 PHP
jquery的ajax()函数传值中文乱码解决方法介绍
2012/11/08 Javascript
jquery在IE、FF浏览器的差别详细探讨
2013/04/28 Javascript
js实现兼容IE和FF的上下层的移动
2015/05/04 Javascript
深入理解Angularjs中$http.post与$.post
2017/05/19 Javascript
vue-cli项目如何使用vue-resource获取本地的json数据(模拟服务端返回数据)
2017/08/04 Javascript
element-ui 中的table的列隐藏问题解决
2018/08/24 Javascript
vue data有值,但是页面{{}} 取不到值的解决
2020/11/09 Javascript
js实现滚动条自动滚动
2020/12/13 Javascript
python常用web框架简单性能测试结果分享(包含django、flask、bottle、tornado)
2014/08/25 Python
Python 中Pickle库的使用详解
2018/02/24 Python
Python使用pip安装pySerial串口通讯模块
2018/04/20 Python
django-rest-framework解析请求参数过程详解
2019/07/18 Python
python读写csv文件的方法
2019/08/13 Python
解决pyinstaller打包运行程序时出现缺少plotly库问题
2020/06/02 Python
Python  word实现读取及导出代码解析
2020/07/09 Python
Python爬虫爬取糗事百科段子实例分享
2020/07/31 Python
用 python 进行微信好友信息分析
2020/11/28 Python
python中函数返回多个结果的实例方法
2020/12/16 Python
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
华硕新加坡官方网上商店:ASUS Singapore
2020/07/09 全球购物
广州御银科技股份有限公司试卷(C++)
2016/11/04 面试题
副总经理工作职责
2013/11/28 职场文书
入党自我评价范文
2014/02/02 职场文书
秋季校运动会广播稿
2014/02/23 职场文书
货物运输服务质量承诺书
2014/05/29 职场文书
大学专科自荐信
2014/06/17 职场文书
幸福家庭标语
2014/06/27 职场文书
车贷收入证明范本
2014/09/14 职场文书
公司行政助理岗位职责
2015/04/11 职场文书
react使用antd的上传组件实现文件表单一起提交功能(完整代码)
2021/06/29 Javascript
sql通过日期判断年龄函数的示例代码
2021/07/16 SQL Server
基于Redission的分布式锁实战
2022/08/14 Redis