Python使用Turtle模块绘制五星红旗代码示例


Posted in Python onDecember 11, 2017

在Udacity上课时学到了python的turtle方法,这是一个很经典的用来教小孩儿编程的图形模块,最早起源于logo语言。python本身内置了这个模块,其可视化的方法可以帮助小孩儿对编程的一些基本理念有所理解。

在作业提交的论坛里看到很多turtle画出来的精美图形,想不出什么要画的东西,于是决定拿五星红旗来练练手。

前期准备

五星红旗绘制参数

Turtle官方文档

turtle的基本操作

# 初始化屏幕
window = turtle.Screen()
# 新建turtle对象实例
import turtle
aTurtle = turtle.Turtle()
# 海龟设置
aTurtle.hideturtle() # 隐藏箭头
aTurtle.speed(10)  # 设置速度
# 前进后退,左转右转
aTurtle.fd(100)  # 前进100像素
aTurtle.right(90) # 右转90°
aTurtle.back(100)
aTurtle.left(90)
# 填充颜色
aTurtle.begin_fill()
aTurtle.fillcolor('yellow')
DoSomethinghere()
aTurtle.end_fill()
# 抬起笔和放下笔,这样进行的操作不会留下痕迹
aTurtle.penup()
aTurtle.goto(start_pos)
aTurtle.fd(radius)
aTurtle.pendown()

绘制五星红旗代码

github地址:https://gist.github.com/dc11287081ee67075da8

#!/usr/bin/env python   
# -*- coding: utf-8 ?*-
''' 对于turtle类的一些封装方法,包括画正多边形,正多角形和五星红旗。'''
__author__ = 'Hu Wenchao'
 
import turtle
import math
 
def draw_polygon(aTurtle, size=50, n=3):
  ''' 绘制正多边形
  args:
    aTurtle: turtle对象实例
    size: int类型,正多边形的边长
    n: int类型,是几边形    
  '''
  for i in xrange(n):
    aTurtle.forward(size)
    aTurtle.left(360.0/n)
 
def draw_n_angle(aTurtle, size=50, num=5, color=None):
  ''' 绘制正n角形,默认为黄色
  args:
    aTurtle: turtle对象实例
    size: int类型,正多角形的边长
    n: int类型,是几角形  
    color: str, 图形颜色,默认不填色
  '''
  if color:
    aTurtle.begin_fill()
    aTurtle.fillcolor(color)
  for i in xrange(num):
    aTurtle.forward(size)
    aTurtle.left(360.0/num)
    aTurtle.forward(size)
    aTurtle.right(2*360.0/num)
  if color:
    aTurtle.end_fill()
 
def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
  ''' 根据起始位置、结束位置和外接圆半径画五角星
  args:
    aTurtle: turtle对象实例
    start_pos: int的二元tuple,要画的五角星的外接圆圆心
    end_pos: int的二元tuple,圆心指向的位置坐标点
    radius: 五角星外接圆半径
    color: str, 图形颜色,默认不填色  
  '''
  aTurtle = aTurtle or turtle.Turtle()
  size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
  aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
  aTurtle.penup()
  aTurtle.goto(start_pos)
  aTurtle.fd(radius)
  aTurtle.pendown()
  aTurtle.right(math.degrees(math.pi*9/10))
  draw_n_angle(aTurtle, size, 5, color)
 
def draw_5_star_flag(times=20.0):
  ''' 绘制五星红旗
  args:
    times: 五星红旗的规格为30*20, times为倍数,默认大小为10倍, 即300*200
  '''
  width, height = 30*times, 20*times
  # 初始化屏幕和海龟
  window = turtle.Screen()
  aTurtle = turtle.Turtle()
  aTurtle.hideturtle()
  aTurtle.speed(10)
  # 画红旗
  aTurtle.penup()
  aTurtle.goto(-width/2, height/2)
  aTurtle.pendown()
  aTurtle.begin_fill()
  aTurtle.fillcolor('red')
  aTurtle.fd(width)
  aTurtle.right(90)
  aTurtle.fd(height)
  aTurtle.right(90)
  aTurtle.fd(width)
  aTurtle.right(90)
  aTurtle.fd(height)
  aTurtle.right(90)  
  aTurtle.end_fill()
  # 画大星星
  draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow') 
  # 画四个小星星
  stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
  for pos in stars_start_pos:
    draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow') 
  # 点击关闭窗口
  window.exitonclick()
if __name__ == '__main__':
    draw_5_star_flag()

结果:

Python使用Turtle模块绘制五星红旗代码示例

总结

以上就是本文关于Python使用Turtle模块绘制五星红旗代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python tornado微信开发入门代码
Aug 24 Python
Django如何自定义分页
Sep 25 Python
python实现对指定字符串补足固定长度倍数截断输出的方法
Nov 15 Python
python实现生成字符串大小写字母和数字的各种组合
Jan 01 Python
pygame实现俄罗斯方块游戏(对战篇1)
Oct 29 Python
在PyCharm中实现添加快捷模块
Feb 12 Python
Python任务调度利器之APScheduler详解
Apr 02 Python
基于python 凸包问题的解决
Apr 16 Python
keras实现多种分类网络的方式
Jun 11 Python
浅析Python中字符串的intern机制
Oct 03 Python
OpenCV中resize函数插值算法的实现过程(五种)
Jun 05 Python
Python面向对象之成员相关知识总结
Jun 24 Python
浅析Git版本控制器使用
Dec 10 #Python
python中Apriori算法实现讲解
Dec 10 #Python
Python自动化运维之IP地址处理模块详解
Dec 10 #Python
python利用rsa库做公钥解密的方法教程
Dec 10 #Python
Python跨文件全局变量的实现方法示例
Dec 10 #Python
git进行版本控制心得详谈
Dec 10 #Python
Python内置模块turtle绘图详解
Dec 09 #Python
You might like
php foreach循环中使用引用的问题
2013/11/06 PHP
全面了解PHP中的全局变量
2016/06/17 PHP
用javascript获取当页面上鼠标光标位置和触发事件的对象的代码
2009/12/09 Javascript
js中的数组Array定义与sort方法使用示例
2013/08/29 Javascript
JavaScript数组常用方法
2015/03/02 Javascript
Javascript实现div层渐隐效果的方法
2015/05/30 Javascript
self.attachevent is not a function的解决方法
2017/04/04 Javascript
利用js的闭包原理做对象封装及调用方法
2017/04/07 Javascript
Angular实现下拉框模糊查询功能示例
2018/01/03 Javascript
深入探索VueJS Scoped CSS 实现原理
2019/09/23 Javascript
js实现数字滚动特效
2019/12/16 Javascript
python实现的简单FTP上传下载文件实例
2015/06/30 Python
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
2016/04/13 Python
python解析html提取数据,并生成word文档实例解析
2018/01/22 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
2018/02/08 Python
Python实现多属性排序的方法
2018/12/05 Python
python 在屏幕上逐字显示一行字的实例
2018/12/24 Python
Python Django基础二之URL路由系统
2019/07/18 Python
tensorboard 可以显示graph,却不能显示scalar的解决方式
2020/02/15 Python
python多进程下的生产者和消费者模型
2020/05/07 Python
python如何调用字典的key
2020/05/25 Python
Python使用sys.exc_info()方法获取异常信息
2020/07/23 Python
Python中的特殊方法以及应用详解
2020/09/20 Python
python中使用np.delete()的实例方法
2021/02/01 Python
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
John Hardy官方网站:手工设计首饰的奢侈品牌
2017/07/05 全球购物
名人珠宝设计师:Melinda Maria Jewelry
2019/03/06 全球购物
ECOSUSI官网:女式皮革背包
2019/09/27 全球购物
英国在线药房和在线医生:LloydsPharmacy
2019/10/21 全球购物
质量月活动总结
2014/08/26 职场文书
高中生毕业评语
2014/12/30 职场文书
董事长岗位职责
2015/02/13 职场文书
2016教师政治学习心得体会
2016/01/23 职场文书
工作转正自我鉴定范文
2019/06/21 职场文书
html中显示特殊符号(附带特殊字符对应表)
2021/06/21 HTML / CSS
win11高清晰音频管理器在哪里?win11找不到高清晰音频管理器解决办法
2022/04/08 数码科技