使用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验证企业工商注册码
Oct 25 Python
关于Python 3中print函数的换行详解
Aug 08 Python
python爬虫之xpath的基本使用详解
Apr 18 Python
Linux 修改Python命令的方法示例
Dec 03 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
Jun 03 Python
python itchat给指定联系人发消息的方法
Jun 11 Python
Python Web框架之Django框架cookie和session用法分析
Aug 16 Python
Django框架 信号调度原理解析
Sep 04 Python
Python pygame绘制文字制作滚动文字过程解析
Dec 12 Python
python实现随机加减法生成器
Feb 24 Python
后端开发使用pycharm的技巧(推荐)
Mar 27 Python
如何Tkinter模块编写Python图形界面
Oct 14 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
文件系统基本操作类
2006/11/23 PHP
php基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
PHP 错误处理机制
2015/07/06 PHP
thinkphp3.x中cookie方法的用法分析
2016/05/19 PHP
Yii2实现ActiveForm ajax提交
2017/05/26 PHP
JS宝典学习笔记(下)
2007/01/10 Javascript
JQuery的html(data)方法与<script>脚本块的解决方法
2010/03/09 Javascript
jquery应该如何来设置改变按钮input的onclick事件
2012/12/10 Javascript
Javascript获取当前日期的农历日期代码
2014/10/08 Javascript
Javascript实现商品秒杀倒计时(时间与服务器时间同步)
2015/09/16 Javascript
Bootstrap表单Form全面解析
2016/06/13 Javascript
jQuery子元素过滤选择器用法示例
2016/09/09 Javascript
微信小程序 LOL 英雄介绍开发实例
2016/09/30 Javascript
JS简单实现点击按钮或文字显示遮罩层的方法
2017/04/27 Javascript
微信小程序城市定位的实现实例(获取当前所在国家城市信息)
2017/05/17 Javascript
js中url对象化管理分析
2017/12/29 Javascript
Angular Excel 导入与导出的实现代码
2019/04/17 Javascript
layer.open回调获取弹出层参数的实现方法
2019/09/10 Javascript
jQuery实现消息弹出框效果
2019/12/10 jQuery
JS基础之逻辑结构与循环操作示例
2020/01/19 Javascript
原生js实现瀑布流效果
2020/03/09 Javascript
[43:58]DOTA2上海特级锦标赛C组败者赛 Newbee VS Archon第二局
2016/02/27 DOTA
python+selenium识别验证码并登录的示例代码
2017/12/21 Python
flask框架中勾子函数的使用详解
2018/08/01 Python
Python定时任务APScheduler的实例实例详解
2019/07/22 Python
Python warning警告出现的原因及忽略方法
2020/01/31 Python
python虚拟环境模块venv使用及示例
2020/03/04 Python
div或img图片高度随宽度自适应的方法
2020/02/06 HTML / CSS
香港通票:Hong Kong Pass
2019/02/26 全球购物
Watchshop德国:欧洲在线手表No.1
2019/06/20 全球购物
台湾全方位线上课程与职能学习平台:TibaMe
2019/12/04 全球购物
创业计划书——互联网商机
2014/01/12 职场文书
党员学习群众路线教育实践活动对照检查材料
2014/09/23 职场文书
英文诗歌翻译方法(赏析)
2019/08/16 职场文书
CSS3实现的侧滑菜单
2021/04/27 HTML / CSS
Python docx库删除复制paragraph及行高设置图片插入示例
2022/07/23 Python