使用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引用(import)文件夹下的py文件的方法
Aug 26 Python
Python实现简单状态框架的方法
Mar 19 Python
Python实现将不规范的英文名字首字母大写
Nov 15 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
Django使用AJAX调用自己写的API接口的方法
Mar 06 Python
django组合搜索实现过程详解(附代码)
Aug 06 Python
python实现证件照换底功能
Aug 20 Python
python实现布隆过滤器及原理解析
Dec 08 Python
Python类的动态绑定实现原理
Mar 21 Python
Python 如何创建一个线程池
Jul 28 Python
python实现画图工具
Aug 27 Python
scrapy-splash简单使用详解
Feb 21 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/01/02 PHP
js获取变量
2006/08/24 Javascript
Js event事件在IE、FF兼容性问题
2011/01/01 Javascript
使图片旋转的3种解决方案
2013/11/21 Javascript
JS中的log对象获取以及debug的写法介绍
2014/03/03 Javascript
解决js图片加载时出现404的问题
2020/11/30 Javascript
AngularJS中$interval的用法详解
2016/02/02 Javascript
纯js实现手风琴效果
2020/04/17 Javascript
JavaScript和jQuery获取input框的绝对位置实现方法
2016/10/13 Javascript
Javascript 实现简单计算器实例代码
2016/10/23 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
jQuery实现table表格信息的展开和缩小功能示例
2018/07/21 jQuery
Vue 引入AMap高德地图的实现代码
2019/04/29 Javascript
vue中利用Promise封装jsonp并调取数据
2019/06/18 Javascript
修改layui的后台模板的左侧导航栏可以伸缩的方法
2019/09/10 Javascript
vue+vant实现商品列表批量倒计时功能
2020/01/13 Javascript
[52:57]2014 DOTA2国际邀请赛中国区预选赛 LGD-CDEC VS HGT
2014/05/21 DOTA
[01:59]游戏“zheng”当时试玩会
2019/08/21 DOTA
通过mod_python配置运行在Apache上的Django框架
2015/07/22 Python
python3 读写文件换行符的方法
2018/04/09 Python
Python实现的拟合二元一次函数功能示例【基于scipy模块】
2018/05/15 Python
NLTK 3.2.4 环境搭建教程
2018/09/19 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
python实现诗歌游戏(类继承)
2019/02/26 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
2019/04/03 Python
Python生成器传参数及返回值原理解析
2020/07/22 Python
西班牙购买行李箱和背包网站:Maletas Greenwich
2019/10/08 全球购物
小米俄罗斯授权商店:Xiaomi俄罗斯
2019/12/08 全球购物
如果NULL定义成#define NULL((char *)0)难道不就可以向函数传入不加转换的NULL了吗
2012/02/15 面试题
中国梦的演讲稿
2014/01/08 职场文书
2014年维修工作总结
2014/11/22 职场文书
研究生毕业登记表的自我鉴定范文
2019/07/15 职场文书
60条职场经典语录,总有一条能触动你的心
2019/08/21 职场文书
Django给表单添加honeypot验证增加安全性
2021/05/06 Python
解决pycharm安装scrapy DLL load failed:找不到指定的程序的问题
2021/06/08 Python
Li list-style-image 图片垂直居中实现方法
2023/05/21 HTML / CSS