使用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中除法使用的注意事项
Aug 21 Python
Python最基本的输入输出详解
Apr 25 Python
django中send_mail功能实现详解
Feb 06 Python
查看Django和flask版本的方法
May 14 Python
解决pandas read_csv 读取中文列标题文件报错的问题
Jun 15 Python
Python3 读、写Excel文件的操作方法
Oct 20 Python
python实现二维数组的对角线遍历
Mar 02 Python
python中seaborn包常用图形使用详解
Nov 25 Python
使用豆瓣源来安装python中的第三方库方法
Jan 26 Python
Python基础之数据结构详解
Apr 28 Python
Python Pandas知识点之缺失值处理详解
May 11 Python
python计算列表元素与乘积详情
Aug 05 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实现二维数组去重功能示例
2017/01/12 PHP
thinkPHP5.0框架应用请求生命周期分析
2017/03/25 PHP
jQuery动态添加 input type=file的实现代码
2012/06/14 Javascript
js 鼠标移动显示图片的简单实例
2013/12/25 Javascript
jQuery实现下拉框左右选择的简单实例
2014/02/22 Javascript
javascript:void(0)的问题使用探讨
2014/04/10 Javascript
再探JavaScript作用域
2014/09/24 Javascript
JavaScript 常见安全漏洞和自动化检测技术
2015/08/21 Javascript
Bootstrap字体图标无法正常显示的解决方法
2016/10/08 Javascript
js对象简介与基本用法示例
2020/03/13 Javascript
JavaScript进阶(四)原型与原型链用法实例分析
2020/05/09 Javascript
javascript+Canvas实现画板功能
2020/06/23 Javascript
js+css实现扇形导航效果
2020/08/18 Javascript
[34:39]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#1COL VS EG第二局
2016/03/05 DOTA
Python面向对象之类的内置attr属性示例
2018/12/14 Python
解决python2 绘图title,xlabel,ylabel出现中文乱码的问题
2019/01/29 Python
Python实现二叉树前序、中序、后序及层次遍历示例代码
2019/05/18 Python
Python facenet进行人脸识别测试过程解析
2019/08/16 Python
用Python解数独的方法示例
2019/10/24 Python
PyInstaller的安装和使用的详细步骤
2020/06/02 Python
pytorch SENet实现案例
2020/06/24 Python
详解python中的异常捕获
2020/12/15 Python
Python tkinter实现日期选择器
2021/02/22 Python
CSS3中的display:grid,网格布局介绍
2019/10/30 HTML / CSS
美国电子产品主要品牌的授权在线零售商:DataVision
2019/03/23 全球购物
2014学雷锋活动总结
2014/03/09 职场文书
授权委托书样本及填写说明
2014/09/19 职场文书
教师群众路线剖析材料
2014/09/29 职场文书
2014年实习生工作总结
2014/11/27 职场文书
2014年医院后勤工作总结
2014/12/06 职场文书
2014年医院个人工作总结
2014/12/09 职场文书
《小摄影师》教学反思
2016/02/18 职场文书
2016年青少年禁毒宣传教育活动总结(学校)
2016/04/05 职场文书
浅谈spring boot使用thymeleaf版本的问题
2021/08/04 Java/Android
用Python生成会跳舞的美女
2022/01/18 Python
分享3个非常实用的 Python 模块
2022/03/03 Python