使用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 list语法学习(带例子)
Nov 01 Python
Python实现基本线性数据结构
Aug 22 Python
Python的SimpleHTTPServer模块用处及使用方法简介
Jan 22 Python
Python使用ConfigParser模块操作配置文件的方法
Jun 29 Python
Python多进程池 multiprocessing Pool用法示例
Sep 07 Python
python使用PyQt5的简单方法
Feb 27 Python
python 按钮点击关闭窗口的实现
Mar 04 Python
python except异常处理之后不退出,解决异常继续执行的实现
Apr 25 Python
Django分组聚合查询实例分享
Apr 29 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
K近邻法(KNN)相关知识总结以及如何用python实现
Jan 28 Python
pytorch Dataset,DataLoader产生自定义的训练数据案例
Mar 03 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基础知识:类与对象(3) 构造函数和析构函数
2006/12/13 PHP
PHP Array交叉表实现代码
2010/08/05 PHP
php命令行使用方法和命令行参数说明
2014/04/08 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
2018/02/07 PHP
js操作textarea方法集合封装(兼容IE,firefox)
2011/02/22 Javascript
javascript定时保存表单数据的代码
2011/03/17 Javascript
js onkeypress与onkeydown 事件区别详细说明
2012/12/13 Javascript
jQuery - css() 方法示例详解
2014/01/16 Javascript
用js判断是否为360浏览器的实现代码
2015/01/15 Javascript
浅谈javascript中自定义模版
2015/01/29 Javascript
JavaScript位移运算符(无符号) >>> 三个大于号 的使用方法详解
2016/03/31 Javascript
JavaScript实现Base64编码转换
2016/04/23 Javascript
jQuery Mobile中的button按钮组件基础使用教程
2016/05/23 Javascript
Vuejs第八篇之Vuejs组件的定义实例解析
2016/09/05 Javascript
js仿网易表单及时验证功能
2017/03/07 Javascript
微信小程序 wx.login解密出现乱码的问题解决办法
2017/03/10 Javascript
微信小程序 开发MAP(地图)实例详解
2017/06/27 Javascript
js实现手机web图片左右滑动效果
2017/12/29 Javascript
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
跟混乱的页面弹窗说再见
2019/04/11 Javascript
微信小程序组件传值图示过程详解
2019/07/31 Javascript
使用python Fabric动态修改远程机器hosts的方法
2018/10/26 Python
Python数据结构之栈、队列及二叉树定义与用法浅析
2018/12/27 Python
python如何实现数据的线性拟合
2019/07/19 Python
python3连接mysql获取ansible动态inventory脚本
2020/01/19 Python
python实现坦克大战
2020/04/24 Python
python suds访问webservice服务实现
2020/06/26 Python
Python实现FTP文件定时自动下载的步骤
2020/12/19 Python
Puccini乌克兰:购买行李箱、女士手袋网上商店
2020/08/06 全球购物
我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
2014/03/30 面试题
社区学习十八大感想
2014/01/22 职场文书
小学感恩节活动策划方案
2014/10/06 职场文书
涉外离婚协议书怎么写
2014/11/20 职场文书
矛盾论读书笔记
2015/06/29 职场文书
2016年秋季运动会加油稿
2015/12/21 职场文书
《自然之道》读后感3篇
2019/12/17 职场文书