使用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 之编写简单乘法运算题
Feb 27 Python
浅谈Python中函数的参数传递
Jun 21 Python
Python中的FTP通信模块ftplib的用法整理
Jul 08 Python
python实现验证码识别功能
Jun 07 Python
django中ORM模型常用的字段的使用方法
Mar 05 Python
python 机器学习之支持向量机非线性回归SVR模型
Jun 26 Python
python 梯度法求解函数极值的实例
Jul 10 Python
python根据多个文件名批量查找文件
Aug 13 Python
opencv3/C++实现视频读取、视频写入
Dec 11 Python
Python中的sys.stdout.write实现打印刷新功能
Feb 21 Python
Python实现播放和录制声音的功能
Aug 12 Python
python中spy++的使用超详细教程
Jan 29 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 第一节 php简介
2012/04/28 PHP
php使用ffmpeg获取视频信息并截图的实现方法
2016/05/03 PHP
JavaScript 中的replace方法说明
2007/04/13 Javascript
javascript 鼠标滚轮事件
2009/04/09 Javascript
Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
2009/08/09 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
2015/01/28 Javascript
JavaScript判断浏览器类型的方法
2015/02/10 Javascript
javascript事件处理模型实例说明
2016/05/31 Javascript
JavaScript中ES6字符串扩展方法
2016/08/26 Javascript
Node.js连接MongoDB数据库产生的问题
2017/02/08 Javascript
jQuery插件HighCharts实现的2D堆条状图效果示例【附demo源码下载】
2017/03/14 Javascript
详解Angular5路由传值方式及其相关问题
2018/04/28 Javascript
Angular中使用ng-zorro图标库部分图标不能正常显示问题
2019/04/22 Javascript
Ant-design-vue Table组件customRow属性的使用说明
2020/10/28 Javascript
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
python中readline判断文件读取结束的方法
2014/11/08 Python
python+matplotlib实现动态绘制图片实例代码(交互式绘图)
2018/01/20 Python
Flask模拟实现CSRF攻击的方法
2018/07/24 Python
python处理excel绘制雷达图
2019/10/18 Python
python程序文件扩展名知识点详解
2020/02/27 Python
使用Matplotlib绘制不同颜色的带箭头的线实例
2020/04/17 Python
Python Django搭建网站流程图解
2020/06/13 Python
Python性能测试工具Locust安装及使用
2020/12/01 Python
python爬取代理ip的示例
2020/12/18 Python
通过canvas转换颜色为RGBA格式及性能问题的解决
2019/11/22 HTML / CSS
意大利男装网店:Vrients
2019/05/02 全球购物
英国运动服、设备及配件网站:DW Sports
2019/12/04 全球购物
介绍一下Cookie和Session及他们之间的区别
2012/11/20 面试题
extern是什么意思
2016/03/10 面试题
《记承天寺夜游》教学反思
2014/02/16 职场文书
如何写好建议书
2014/03/13 职场文书
机关干部四风问题自查报告及整改措施
2014/10/26 职场文书
因公司原因离职的辞职信范文
2015/05/12 职场文书
高质量“欢迎词”
2019/04/03 职场文书
HTML5+CSS+JavaScript实现捉虫小游戏设计和实现
2021/10/16 HTML / CSS
Python内置包对JSON文件数据进行编码和解码
2022/04/12 Python