使用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 调用DLL操作抄表机
Jan 12 Python
Python中import导入上一级目录模块及循环import问题的解决
Jun 04 Python
解决Python中字符串和数字拼接报错的方法
Oct 23 Python
Pandas 数据处理,数据清洗详解
Jul 10 Python
Python JSON格式数据的提取和保存的实现
Mar 22 Python
Python数据类型之Tuple元组实例详解
May 08 Python
在python中实现同行输入/接收多个数据的示例
Jul 20 Python
在python中用url_for构造URL的方法
Jul 25 Python
python 视频逐帧保存为图片的完整实例
Dec 10 Python
Python3 操作 MySQL 插入一条数据并返回主键 id的实例
Mar 02 Python
python实现模拟器爬取抖音评论数据的示例代码
Jan 06 Python
python中的插入排序的简单用法
Jan 19 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数字游戏 计算24算法
2012/06/10 PHP
PHP类的封装与继承详解
2015/09/29 PHP
PHP调用存储过程返回值不一致问题的解决方法分析
2016/04/26 PHP
如何在PHP环境中使用ProtoBuf数据格式
2020/06/19 PHP
ExtJS 学习专题(一) 如何应用ExtJS(附实例)
2010/03/11 Javascript
jQuery中创建实例与原型继承揭秘
2011/12/21 Javascript
jQuery 追加元素的方法如append、prepend、before
2014/01/16 Javascript
js的参数有长度限制吗?发现不能超过2083个字符
2014/04/20 Javascript
js打开windows上的可执行文件示例
2014/05/27 Javascript
node.js中的fs.existsSync方法使用说明
2014/12/17 Javascript
javascript实现多级联动下拉菜单的方法
2015/02/06 Javascript
node-http-proxy修改响应结果实例代码
2016/06/06 Javascript
卸载安装Node.js与npm过程详解
2016/08/15 Javascript
深入理解bootstrap框架之第二章整体架构
2016/10/09 Javascript
微信小程序中显示html格式内容的方法
2017/04/25 Javascript
详解Vue-cli 创建的项目如何跨域请求
2017/05/18 Javascript
用node和express连接mysql实现登录注册的实现代码
2017/07/05 Javascript
Ionic3实现图片瀑布流布局
2017/08/09 Javascript
微信小程序wx.request实现后台数据交互功能分析
2017/11/25 Javascript
vue实现输入框自动跳转功能
2020/05/20 Javascript
[01:19:46]EG vs Secret 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21.mp4
2020/07/19 DOTA
[57:22]完美世界DOTA2联赛PWL S2 FTD vs PXG 第二场 11.27
2020/12/01 DOTA
使用wxpython实现的一个简单图片浏览器实例
2014/07/10 Python
详解如何管理多个Python版本和虚拟环境
2019/05/10 Python
python使用递归的方式建立二叉树
2019/07/03 Python
使用OpenCV circle函数图像上画圆的示例代码
2019/12/27 Python
Docker部署Python爬虫项目的方法步骤
2020/01/19 Python
Pycharm如何运行.py文件的方法步骤
2020/03/03 Python
python中reload重载实例用法
2020/12/15 Python
CSS3实现红包抖动效果
2020/12/23 HTML / CSS
校园安全检查制度
2014/02/03 职场文书
三八活动策划方案
2014/08/17 职场文书
学校总务处领导干部个人对照检查材料思想汇报
2014/10/06 职场文书
2015银行年终工作总结范文
2015/05/26 职场文书
MySQL获取所有分类的前N条记录
2021/05/07 MySQL
Centos系统通过Docker安装并搭建MongoDB数据库
2022/04/12 MongoDB