使用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 29 Python
Python多线程和队列操作实例
Jun 21 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
Jun 14 Python
简述:我为什么选择Python而不是Matlab和R语言
Nov 14 Python
Python实现的简单计算器功能详解
Aug 25 Python
实例讲解python中的序列化知识点
Oct 08 Python
Python 使用指定的网卡发送HTTP请求的实例
Aug 21 Python
Python进度条的制作代码实例
Aug 31 Python
Django模板获取field的verbose_name实例
May 19 Python
Python如何使用ConfigParser读取配置文件
Nov 12 Python
pandas针对excel处理的实现
Jan 15 Python
Python3+Appium安装及Appium模拟微信登录方法详解
Feb 16 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+DBM的同学录程序(5)
2006/10/09 PHP
php 随机记录mysql rand()造成CPU 100%的解决办法
2010/05/18 PHP
PHP中怎样保持SESSION不过期 原理及方案介绍
2013/08/08 PHP
php 多进程编程父进程的阻塞与非阻塞实例分析
2020/02/22 PHP
php与阿里云短信接口接入操作案例分析
2020/05/27 PHP
phpmyadmin在宝塔面板里进不去的解决方案
2020/07/06 PHP
取选中的radio的值
2010/01/11 Javascript
js getBoundingClientRect() 来获取页面元素的位置
2010/11/25 Javascript
js面向对象设计用{}好还是function(){}好(构造函数)
2011/10/23 Javascript
在Firefox下js select标签点击无法弹出
2014/03/06 Javascript
Vue.js系列之vue-router(上)(3)
2017/01/03 Javascript
对称加密与非对称加密优缺点详解
2017/02/06 Javascript
JavaScript创建对象的七种方式(推荐)
2017/06/26 Javascript
vue forEach循环数组拿到自己想要的数据方法
2018/09/21 Javascript
详解nodejs 配置文件处理方案
2019/01/02 NodeJs
基于three.js实现的3D粒子动效实例代码
2019/04/09 Javascript
使用 JavaScript 创建并下载文件(模拟点击)
2019/10/25 Javascript
js判断浏览器的环境(pc端,移动端,还是微信浏览器)
2020/12/24 Javascript
创建与框架无关的JavaScript插件
2020/12/01 Javascript
分享一下Python 开发者节省时间的10个方法
2015/10/02 Python
将python代码和注释分离的方法
2018/04/21 Python
Python实现注册、登录小程序功能
2018/09/21 Python
python中的json总结
2018/10/11 Python
浅谈python编译pyc工程--导包问题解决
2019/03/20 Python
Pycharm连接远程服务器并实现远程调试的实现
2019/08/02 Python
python爬虫模拟浏览器的两种方法实例分析
2019/12/09 Python
CSS实现限制字数功能当对象内文本溢出时显示省略标记
2014/08/20 HTML / CSS
优秀护士演讲稿
2014/04/30 职场文书
中秋晚会策划方案
2014/06/12 职场文书
死者家属慰问信
2015/03/24 职场文书
2016学校先进集体事迹材料
2016/02/29 职场文书
python正则表达式re.search()的基本使用教程
2021/05/21 Python
Python连续赋值需要注意的一些问题
2021/06/03 Python
MySQL 1130异常,无法远程登录解决方案详解
2021/08/23 MySQL
深入解读Java三大集合之map list set的用法
2021/11/11 Java/Android
mysql幻读详解实例以及解决办法
2022/06/16 MySQL