使用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实现递归版汉诺塔示例(汉诺塔递归算法)
Apr 08 Python
Python实现屏幕截图的两种方式
Feb 05 Python
python3 requests中使用ip代理池随机生成ip的实例
May 07 Python
pandas 将list切分后存入DataFrame中的实例
Jul 03 Python
python 使用正则表达式按照多个空格分割字符的实例
Dec 20 Python
解析python的局部变量和全局变量
Aug 15 Python
python读取当前目录下的CSV文件数据
Mar 11 Python
python对指定字符串逆序的6种方法(小结)
Apr 02 Python
python如何代码集体右移
Jul 20 Python
python UDF 实现对csv批量md5加密操作
Jan 01 Python
利用Selenium添加cookie实现自动登录的示例代码(fofa)
May 08 Python
OpenCV-Python实现轮廓的特征值
Jun 09 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
4月1日重磅发布!《星际争霸II》6.0.0版本更新
2020/04/09 星际争霸
《DOTA3》开发工作已经开始 《DOTA3》将代替《DOTA2》
2021/03/06 DOTA
PHP合并两个数组的两种方式的异同
2012/09/14 PHP
PHP获取一年中每个星期的开始和结束日期的方法
2015/02/12 PHP
ThinkPHP3.2框架使用addAll()批量插入数据的方法
2017/03/16 PHP
ThinkPHP实现分页功能
2017/04/28 PHP
PHP实现正则表达式分组捕获操作示例
2018/02/03 PHP
php使用pecl方式安装扩展操作示例
2019/08/12 PHP
Fixie.js 自动填充内容的插件
2012/06/28 Javascript
不能不知道的10个angularjs英文学习网站
2016/03/23 Javascript
微信JS-SDK选取手机照片上传功能
2017/04/21 Javascript
es6基础学习之解构赋值
2018/12/10 Javascript
layui lay-verify form表单自定义验证规则详解
2019/09/18 Javascript
解决layui数据表格Date日期格式的回显Object的问题
2019/09/19 Javascript
详解利用nodejs对本地json文件进行增删改查
2019/09/20 NodeJs
Vuex实现数据增加和删除功能
2019/11/11 Javascript
JavaScript实现简单的计算器
2020/01/16 Javascript
请求时token过期自动刷新token操作
2020/09/11 Javascript
Map与WeakMap类型在JavaScript中的使用详解
2020/11/18 Javascript
Python实现类的创建与使用方法示例
2017/07/25 Python
python中将字典形式的数据循环插入Excel
2018/01/16 Python
python 同时读取多个文件的例子
2019/07/16 Python
Django 实现xadmin后台菜单改为中文
2019/11/15 Python
OpenCV Python实现拼图小游戏
2020/03/23 Python
python实现逻辑回归的示例
2020/10/09 Python
python 实现全球IP归属地查询工具
2020/12/18 Python
Django如何重置migration的几种情景
2021/02/24 Python
大专生工程监理求职信
2013/10/04 职场文书
医学院学生求职简历的自我评价
2013/10/24 职场文书
在职党员进社区活动总结
2014/07/05 职场文书
个人授权委托书样本
2014/09/13 职场文书
县政府班子个人对照检查材料
2014/10/05 职场文书
太行山上观后感
2015/06/05 职场文书
水知道答案观后感
2015/06/08 职场文书
八年级物理教学反思
2016/02/19 职场文书
把77A收信机改造成收音机
2022/04/05 无线电