使用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编程中super关键字的用法
Jun 24 Python
python入门前的第一课 python怎样入门
Mar 06 Python
pandas apply 函数 实现多进程的示例讲解
Apr 20 Python
详解Python if-elif-else知识点
Jun 11 Python
python统计多维数组的行数和列数实例
Jun 23 Python
基于Python和PyYAML读取yaml配置文件数据
Jan 13 Python
详解Python的三种拷贝方式
Feb 11 Python
Python基于DB-API操作MySQL数据库过程解析
Apr 23 Python
Python爬取YY评级分数并保存数据实现过程解析
Jun 01 Python
keras训练浅层卷积网络并保存和加载模型实例
Jul 02 Python
python在linux环境下安装skimage的示例代码
Oct 14 Python
python中count函数知识点浅析
Dec 17 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下判断数组中是否存在相同的值array_unique
2008/03/25 PHP
php记录代码执行时间(实现代码)
2013/07/05 PHP
微信红包随机生成算法php版
2016/07/21 PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
2017/04/18 PHP
PHP实现数据四舍五入的方法小结【4种方法】
2019/03/27 PHP
Extjs Ajax 乱码问题解决方案
2009/04/15 Javascript
jquery获取div宽度的实现思路与代码
2013/01/13 Javascript
js中string转int把String类型转化成int类型
2014/08/13 Javascript
基于NodeJS的前后端分离的思考与实践(六)Nginx + Node.js + Java 的软件栈部署实践
2014/09/26 NodeJs
flash+jQuery实现可关闭及重复播放的压顶广告
2015/04/15 Javascript
js实现select下拉框菜单
2015/12/08 Javascript
Javascript自执行匿名函数(function() { })()的原理浅析
2016/05/15 Javascript
JavaScript实现Java中Map容器的方法
2016/10/09 Javascript
JavaScript内存泄漏的处理方式
2017/11/20 Javascript
Vue Socket.io源码解读
2018/02/07 Javascript
VUE table表格动态添加一列数据,新增的这些数据不可以编辑(v-model绑定的数据不能实时更新)
2020/04/03 Javascript
Windows系统下多版本pip的共存问题详解
2017/10/10 Python
python3将视频流保存为本地视频文件
2018/06/20 Python
python opencv实现旋转矩形框裁减功能
2018/07/25 Python
Selenium定时刷新网页的实现代码
2018/10/31 Python
python实现图片识别汽车功能
2018/11/30 Python
Python3 无重复字符的最长子串的实现
2019/10/08 Python
python 调用API接口 获取和解析 Json数据
2020/09/28 Python
HTML5图片预览实例分享
2014/06/04 HTML / CSS
继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?
2015/11/18 面试题
解释一下ruby中的特殊方法与特殊类
2013/02/26 面试题
门卫工作岗位职责
2013/12/17 职场文书
入党积极分子自我鉴定
2014/02/18 职场文书
道路交通安全实施方案
2014/03/12 职场文书
2015年毕业生自我鉴定模板
2014/09/19 职场文书
作风整顿剖析材料
2014/09/30 职场文书
销售会议开幕词
2015/01/28 职场文书
大学毕业生个人总结
2015/02/28 职场文书
物业客服专员岗位职责
2015/04/07 职场文书
python自动计算图像数据集的RGB均值
2021/06/18 Python
Spring Boot 整合 Apache Dubbo的示例代码
2021/07/04 Java/Android