使用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 18 Python
Python操作使用MySQL数据库的实例代码
May 25 Python
Python快速排序算法实例分析
Nov 29 Python
小白如何入门Python? 制作一个网站为例
Mar 06 Python
python批量设置多个Excel文件页眉页脚的脚本
Mar 14 Python
Python pymongo模块用法示例
Mar 31 Python
python按行读取文件,去掉每行的换行符\n的实例
Apr 19 Python
Python 实现两个列表里元素对应相乘的方法
Nov 14 Python
对Python中DataFrame选择某列值为XX的行实例详解
Jan 29 Python
PyTorch搭建一维线性回归模型(二)
May 22 Python
基于python-opencv3的图像显示和保存操作
Jun 27 Python
详解python调用cmd命令三种方法
Jul 08 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
set_include_path在win和linux下的区别
2008/01/10 PHP
PHP读取txt文件的内容并赋值给数组的代码
2011/11/03 PHP
php 操作符与控制结构
2012/03/07 PHP
PHP字符串长度计算 - strlen()函数使用介绍
2013/10/15 PHP
一个简单且很好用的php分页类
2013/10/26 PHP
2014年10个最佳的PHP图像操作库
2014/07/14 PHP
php更新mysql后获取改变行数的方法
2014/12/25 PHP
PHP通过加锁实现并发情况下抢码功能
2016/08/10 PHP
laravel 解决Eloquent ORM的save方法无法插入数据的问题
2019/10/21 PHP
JavaScript入门教程(8) Location地址对象
2009/01/31 Javascript
浅谈javascript回调函数
2014/12/07 Javascript
关于Javascript加载执行优化的研究报告
2014/12/16 Javascript
js实现选中复选框文字变色的方法
2015/08/14 Javascript
JavaScript与HTML的结合方法详解
2015/11/23 Javascript
简单实现JavaScript图片切换效果
2016/11/28 Javascript
Kindeditor单独调用多图上传实例
2017/07/31 Javascript
使用mock.js随机数据和使用express输出json接口的实现方法
2018/01/07 Javascript
基于mpvue的小程序项目搭建的步骤
2018/05/22 Javascript
Vue-cli assets SubDirectory及PublicPath区别详解
2020/08/18 Javascript
在vscode 中设置 vue模板内容的方法
2020/09/02 Javascript
Vue实现开关按钮拖拽效果
2020/09/22 Javascript
Vue实现手机号、验证码登录(60s禁用倒计时)
2020/12/19 Vue.js
vue+flask实现视频合成功能(拖拽上传)
2021/03/04 Vue.js
python处理cookie详解
2014/02/07 Python
python实现批量修改服务器密码的方法
2019/08/13 Python
python根据文本生成词云图代码实例
2019/11/15 Python
Python3的socket使用方法详解
2020/02/18 Python
英国最大的宠物食品和宠物用品网上零售商: Zooplus
2016/08/01 全球购物
猎人靴英国官网:Hunter Boots
2017/02/02 全球购物
Agoda香港:全球特价酒店预订
2017/05/07 全球购物
世界上最受欢迎的钓鱼诱饵:Rapala
2019/05/02 全球购物
公司离职证明标准样本
2014/10/05 职场文书
2015年图书馆个人工作总结
2015/05/26 职场文书
大学体育课感想
2015/08/10 职场文书
2019请假条的基本格式及范文!
2019/07/05 职场文书
python中的random模块和相关函数详解
2022/04/22 Python