使用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模块restful使用方法实例
Dec 10 Python
python创建文件备份的脚本
Sep 11 Python
python开发游戏的前期准备
May 05 Python
利用Python检测URL状态
Jul 31 Python
numpy.random.shuffle打乱顺序函数的实现
Sep 10 Python
django中使用事务及接入支付宝支付功能
Sep 15 Python
python银行系统实现源码
Oct 25 Python
Python日期格式和字符串格式相互转换的方法
Feb 18 Python
django 读取图片到页面实例
Mar 27 Python
如何使用python切换hosts文件
Apr 29 Python
基于python实现可视化生成二维码工具
Jul 08 Python
发工资啦!教你用Python实现邮箱自动群发工资条
May 10 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
海河写的 Discuz论坛帖子调用js的php代码
2007/08/23 PHP
用php将任何格式视频转为flv的代码
2009/09/03 PHP
关于UEditor编辑器远程图片上传失败的解决办法
2012/08/31 PHP
php array_keys 返回数组的键名
2016/10/25 PHP
php实现文件管理与基础功能操作
2017/03/21 PHP
php实现微信小程序授权登录功能(实现流程)
2019/11/13 PHP
javascript 自动转到命名锚记
2009/01/10 Javascript
论坛里点击别人帖子下面的回复,回复标题变成“回复 24# 的帖子”
2009/06/14 Javascript
JS 实现导航栏悬停效果(续2)
2013/09/24 Javascript
jquery ajax跨域解决方法(json方式)
2014/02/04 Javascript
JavaSript中变量的作用域闭包的深入理解
2014/05/12 Javascript
2014年最火的Node.JS后端框架推荐
2014/10/27 Javascript
js判断某个方法是否存在实例代码
2015/01/10 Javascript
jQuery简单实现彩色云标签效果示例
2016/08/01 Javascript
老生常谈JavaScript 正则表达式语法
2016/08/20 Javascript
完美解决IE9浏览器出现的对象未定义问题
2016/09/29 Javascript
vue发送websocket请求和http post请求的实例代码
2019/07/11 Javascript
layui之table checkbox初始化时选中对应选项的方法
2019/09/02 Javascript
js实现直播点击飘心效果
2020/08/19 Javascript
Python中的默认参数详解
2015/06/24 Python
使用Python多线程爬虫爬取电影天堂资源
2016/09/23 Python
对python的文件内注释 help注释方法
2018/05/23 Python
python3基于OpenCV实现证件照背景替换
2018/07/18 Python
Python实现统计英文文章词频的方法分析
2019/01/28 Python
python lambda表达式(匿名函数)写法解析
2019/09/16 Python
python Pillow图像处理方法汇总
2019/10/16 Python
在tensorflow中实现屏蔽输出的log信息
2020/02/04 Python
django ObjectDoesNotExist 和 DoesNotExist的用法
2020/07/09 Python
Europcar西班牙:全球汽车租赁领域的领导者
2018/09/17 全球购物
求职信怎么写
2014/05/23 职场文书
啤酒节策划方案
2014/05/28 职场文书
学位证书委托书
2014/09/30 职场文书
公司离职证明标准格式
2014/11/18 职场文书
经典哲理警句:志不真则心不热,心不热则功不贤
2019/11/14 职场文书
JavaScript 对象创建的3种方法
2021/11/17 Javascript
如何使用python包中的sched事件调度器
2022/04/30 Python