使用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 异常处理实例详解
Mar 12 Python
Python getopt模块处理命令行选项实例
May 13 Python
python使用PyGame绘制图像并保存为图片文件的方法
Apr 24 Python
python 远程统计文件代码分享
May 14 Python
Python subprocess模块功能与常见用法实例详解
Jun 28 Python
Python 经典面试题 21 道【不可错过】
Sep 21 Python
python pandas库的安装和创建
Jan 10 Python
Python实现对特定列表进行从小到大排序操作示例
Feb 11 Python
对python3中的RE(正则表达式)-详细总结
Jul 23 Python
jupyter notebook的安装与使用详解
May 18 Python
Django-silk性能测试工具安装及使用解析
Nov 28 Python
Python Selenium XPath根据文本内容查找元素的方法
Dec 07 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
如何修改和添加Apache的默认站点目录
2013/07/05 PHP
PHP中copy on write写时复制机制介绍
2014/05/13 PHP
laravel 实现关闭CSRF(全部关闭、部分关闭)
2019/10/21 PHP
发两个小东西,ASP/PHP 学习工具。 用JavaScript写的
2007/04/12 Javascript
漂亮的widgets,支持换肤和后期开发新皮肤(2007-4-27已更新1.7alpha)
2007/04/27 Javascript
JavaScript 调试器简介
2009/02/21 Javascript
解决js正则匹配换行问题实现代码
2012/12/10 Javascript
JS解决ie6下png透明的方法实例
2013/08/02 Javascript
JQuery实现绚丽的横向下拉菜单
2013/12/19 Javascript
使用Nodejs开发微信公众号后台服务实例
2014/09/03 NodeJs
jQuery+CSS3实现树叶飘落特效
2015/02/01 Javascript
Vue 组件间的样式冲突污染
2017/08/31 Javascript
微信JSSDK实现打开摄像头拍照再将相片保存到服务器
2019/11/15 Javascript
JS几个常用的函数和对象定义与用法示例
2020/01/15 Javascript
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
python编码最佳实践之总结
2016/02/14 Python
Odoo中如何生成唯一不重复的序列号详解
2018/02/10 Python
python数字图像处理实现直方图与均衡化
2018/05/04 Python
python中强大的format函数实例详解
2018/12/05 Python
详解python中的index函数用法
2019/08/06 Python
keras .h5转移动端的.tflite文件实现方式
2020/05/25 Python
一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系
2020/07/03 Python
HTML5在线预览PDF的示例代码
2017/09/14 HTML / CSS
SQL里面IN比较快还是EXISTS比较快
2012/07/19 面试题
MySQL面试题目集锦
2016/04/14 面试题
护理专科毕业生自荐书范文
2014/02/19 职场文书
大学生学期自我鉴定
2014/03/19 职场文书
产品销售计划书
2014/05/04 职场文书
求职信的正确写法
2014/07/10 职场文书
2014年医务科工作总结
2014/12/18 职场文书
保证书格式
2015/01/16 职场文书
给客户的感谢信
2015/01/21 职场文书
律师函格式范本
2015/05/27 职场文书
社区宣传标语口号
2015/12/26 职场文书
22句经典语录:送给优柔寡断和胡思乱想的朋友们
2019/12/13 职场文书
vue修饰符.capture和.self的区别
2022/04/22 Vue.js