通过Turtle库在Python中绘制一个鼠年福鼠


Posted in Python onFebruary 03, 2020

turtle库是一个很经典的绘图库,其最初来自于1967年创造的logo编程语言,之后被Python编写放到了Python的内置模块中。网络上有很多借助于turtle绘制精美图像的案例。比如小猪佩奇、皮卡丘、柯基犬等等。趁着新年假期还未结束,今天州的先生(https://zmister.com)为大家带来一个福鼠的绘制。

一、绘制鼠头

首先,咱们(https://zmister.com)把鼠的头给绘制了。鼠头主要是由圆来构成,脸庞是一个大圆,耳朵、眉毛、眼睛、嘴角和鼻子也都是由不同弧度的圆构成。鼠头的绘制代码如下所示:

def head():
 turtle.color('black')

 # 脸轮廓
 turtle.pd() # 落笔
 turtle.circle(50) # 画一个半径为50的圆
 turtle.pu() # 提笔

 # 右耳轮廓
 turtle.goto(50,60) # 移动到x=50,y=60的位置
 turtle.pd() # 落笔
 turtle.circle(30,260) # 画一个半径为30,角度为245的圆弧
 turtle.pu() # 提笔
 # 右耳耳纹
 turtle.goto(30,90)
 turtle.pd()
 turtle.seth(65)
 turtle.circle(-30,70)
 turtle.pu()

 # 左耳轮廓
 turtle.goto(-50,60)
 turtle.pd()
 turtle.seth(180) # 设置方向为西,
 turtle.circle(-30,260)
 turtle.pu()
 # 左耳耳纹
 turtle.goto(-30,90)
 turtle.pd()
 turtle.seth(120)
 turtle.circle(30,70)
 turtle.pu()

 # 面部五官
 # 右侧眉毛
 turtle.goto(5,80)
 turtle.seth(20)
 turtle.pd()
 turtle.circle(-25,40)
 turtle.pu()
 # 左侧眉毛
 turtle.goto(-5,80)
 turtle.seth(160)
 turtle.pd()
 turtle.circle(25,40)
 turtle.pu()

 # 右侧眼睛
 turtle.begin_poly()
 turtle.goto(8,60)
 turtle.seth(45)
 turtle.pd()
 turtle.circle(-15,120)
 turtle.pu()
 turtle.goto(8,60)
 turtle.seth(40)
 turtle.pd()
 turtle.circle(-15,100)
 turtle.pu()
 turtle.end_poly()

 # 左侧眼睛
 turtle.goto(-8,60)
 turtle.seth(135)
 turtle.pd()
 turtle.circle(15,120)
 turtle.pu()
 turtle.goto(-8,60)
 turtle.seth(140)
 turtle.pd()
 turtle.circle(15,100)
 turtle.pu()

 # 鼻子
 # 鼻子上瓣
 turtle.goto(-6,45)
 turtle.seth(70)
 turtle.pd()
 turtle.circle(-6,150)
 turtle.pu()
 # 鼻子下瓣
 turtle.goto(-6,45)
 turtle.seth(-70)
 turtle.pd()
 turtle.circle(6,150)
 turtle.pu()

 # 鼻线
 turtle.goto(0,40)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(7)
 turtle.pu()

 # 上嘴线
 turtle.seth(200)
 turtle.pd()
 turtle.circle(-15,60)
 turtle.pu()

 turtle.goto(0,33)
 turtle.seth(-20)
 turtle.pd()
 turtle.circle(15,60)
 turtle.pu()

 # 下嘴线
 turtle.goto(10,33)
 turtle.seth(260)
 turtle.pd()
 turtle.circle(-15,65)
 turtle.pu()

 turtle.goto(-10,33)
 turtle.seth(280)
 turtle.pd()
 turtle.circle(15,65)
 turtle.pu()

 # 牙齿
 turtle.goto(4,33)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(4)
 turtle.seth(180)
 turtle.forward(8)
 turtle.seth(90)
 turtle.forward(4)
 turtle.pu()

 # 胡须
 turtle.pensize(2)
 turtle.goto(30,30)
 turtle.seth(8)
 turtle.pd()
 turtle.circle(-60,40)
 turtle.pu()

 turtle.goto(30,25)
 turtle.seth(-5)
 turtle.pd()
 turtle.circle(-60,40)
 turtle.pu()


 turtle.goto(-30,30)
 turtle.seth(172)
 turtle.pd()
 turtle.circle(60,40)
 turtle.pu()

 turtle.goto(-30,25)
 turtle.seth(188)
 turtle.pd()
 turtle.circle(60,40)
 turtle.pu()

 # 睫毛
 turtle.pensize(1)
 turtle.goto(30,58)
 turtle.seth(20)
 turtle.pd()
 turtle.circle(20,20)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(28,62)
 turtle.seth(25)
 turtle.pd()
 turtle.circle(20,12)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(-30,58)
 turtle.seth(160)
 turtle.pd()
 turtle.circle(-20,20)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(-28,62)
 turtle.seth(165)
 turtle.pd()
 turtle.circle(-20,12)
 turtle.pu()

运行上述代码,我们可以看到鼠头可以完整地绘制出来了,如下动图所示:

通过Turtle库在Python中绘制一个鼠年福鼠

二、绘制身体

接着,咱们来绘制老鼠的身体。我们(https://zmister.com)画的这个老鼠是一个穿着财神服站立拱手的老鼠,所以它的身体需要重点突出的是服装:

def body():
 # 左手
 turtle.goto(-25,8)
 turtle.seth(240)
 turtle.pd()
 turtle.circle(150,15)
 turtle.seth(270)
 turtle.circle(40,15)
 turtle.circle(15,65)
 turtle.seth(0)
 turtle.forward(10)
 turtle.circle(10,100)
 turtle.seth(90)
 turtle.forward(5)
 turtle.circle(10,100)
 turtle.seth(180)
 turtle.forward(10)
 turtle.pu()
 # 右手
 turtle.goto(25,8)
 turtle.seth(-60)
 turtle.pd()
 turtle.circle(-150,15)
 turtle.seth(270)
 turtle.circle(-40,15)
 turtle.circle(-15,65)
 turtle.seth(180)
 turtle.forward(10)
 turtle.circle(-10,100)
 turtle.seth(90)
 turtle.forward(5)
 turtle.circle(-10,100)
 turtle.seth(0)
 turtle.forward(10)
 turtle.pu()

 # 袍子
 turtle.goto(-30,-48)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(30)
 turtle.circle(10,100)
 turtle.seth(0)
 turtle.forward(38)
 turtle.circle(10,100)
 turtle.seth(90)
 turtle.forward(30)
 turtle.pu()

 # 领口
 turtle.goto(-20,4)
 turtle.pd()
 turtle.seth(300)
 turtle.circle(30,20)
 turtle.seth(0)
 turtle.forward(25)
 turtle.seth(30)
 turtle.circle(30,20)
 turtle.pu()

 # 官带
 turtle.goto(-7,-38)
 turtle.seth(0)
 turtle.pd()
 turtle.forward(15)
 turtle.pu()
 turtle.goto(-30,-54)
 turtle.pd()
 turtle.forward(60)
 turtle.pu()

 # 袍子上的波浪
 turtle.goto(-30,-80)
 turtle.pd()
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.pu()

 turtle.goto(-25,-85)
 turtle.pd()
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.pu()

运行上述代码,我们可以看到老鼠身体的绘制过程,如下动图所示:

通过Turtle库在Python中绘制一个鼠年福鼠

这里先不将其汇合在一起,待几个部件都完成之后,我们再将其组成一个完整地老鼠。

三、绘制手

上面绘制的身体还缺了两只手,对了,还有袍子上的一个大金钱,我们将其补上:

def hands():
 turtle.goto(-8, -25)
 turtle.pd()
 turtle.seth(30)
 turtle.forward(10)
 turtle.seth(0)
 turtle.circle(-10, 50)
 turtle.seth(210)
 turtle.forward(18)

 turtle.back(10)
 turtle.seth(-45)
 turtle.forward(10)
 turtle.back(10)
 turtle.seth(30)
 turtle.forward(8)
 turtle.seth(300)
 turtle.forward(5)
 turtle.pu()

 turtle.goto(0, -75)
 turtle.pd()
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10, 90)
 turtle.seth(0)
 turtle.circle(10, 90)
 turtle.seth(270)
 turtle.circle(10, 90)
 turtle.seth(180)
 turtle.circle(10, 90)
 turtle.pu()

拱手和金钱没有和身体结合在一起时,暂时看不出上面效果来,如下动图所示:

通过Turtle库在Python中绘制一个鼠年福鼠

四、绘制帽子

老鼠还戴了一顶金钱帽,咱们(zmister.com)现在给它加上:

def hat():
 # 画帽子
 turtle.goto(-20,98)
 turtle.pd()
 turtle.seth(80)
 turtle.forward(20)
 turtle.seth(60)
 turtle.circle(-20,140)
 turtle.seth(-85)
 turtle.forward(18)
 turtle.pu()

 turtle.goto(-20,98)
 turtle.pd()
 turtle.seth(80)
 turtle.forward(5)
 turtle.seth(30)
 turtle.forward(22)
 turtle.seth(-25)
 turtle.forward(24)
 turtle.pu()

 turtle.goto(0,127)
 turtle.pd()
 turtle.seth(0)
 turtle.circle(5)
 turtle.pu()

 turtle.goto(0,125)
 turtle.pd()
 turtle.seth(270)
 turtle.forward(10)
 turtle.pu()

 # 右边抖带
 turtle.goto(19,110)
 turtle.pd()
 turtle.seth(30)
 turtle.circle(40,50)
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10,90)
 turtle.seth(0)
 turtle.circle(10,90)
 turtle.seth(270)
 turtle.circle(10,90)
 turtle.seth(180)
 turtle.circle(10,90)
 turtle.pu()

 # 左边抖带
 turtle.goto(-19,110)
 turtle.pd()
 turtle.seth(150)
 turtle.circle(-40,50)
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10,90)
 turtle.seth(0)
 turtle.circle(10,90)
 turtle.seth(270)
 turtle.circle(10,90)
 turtle.seth(180)
 turtle.circle(10,90)
 turtle.pu()

帽子主要都是由圆构成,其绘制过程如下动图所示:

通过Turtle库在Python中绘制一个鼠年福鼠

五、绘制尾巴

先不着急为老鼠带上金钱帽,我们还忘记了老鼠有一根长长的尾巴,为它补上吧:

def tail():
 turtle.goto(30, -60)
 turtle.pd()
 turtle.seth(20)
 turtle.circle(40, 80)
 turtle.circle(-20, 180)
 turtle.circle(-10, 90)

尾巴就是两个方向相反弧度不同的圆,效果我们就不演示了。最后将其结合在一起:

if __name__ == '__main__':
 head()
 body()
 hands()
 hat()
 tail()
 turtle.done()

我们就可以看到一个完整的鼠年福鼠绘制过程,如下动图所示:

通过Turtle库在Python中绘制一个鼠年福鼠

总结

以上所述是小编给大家介绍的通过Turtle库在Python中绘制一个鼠年福鼠,希望对大家有帮助!

Python 相关文章推荐
Python 字符串操作实现代码(截取/替换/查找/分割)
Jun 08 Python
python连接MySQL、MongoDB、Redis、memcache等数据库的方法
Nov 15 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
Mar 14 Python
Python 实现使用dict 创建二维数据、DataFrame
Apr 13 Python
python实现得到当前登录用户信息的方法
Jun 21 Python
使用Filter过滤python中的日志输出的实现方法
Jul 17 Python
python3.6编写的单元测试示例
Aug 17 Python
pycharm如何实现跨目录调用文件
Feb 28 Python
浅谈python的elementtree模块处理中文注意事项
Mar 06 Python
Python 列表推导式需要注意的地方
Oct 23 Python
使用python对excel表格处理的一些小功能
Jan 25 Python
selenium3.0+python之环境搭建的方法步骤
Feb 01 Python
python爬虫模块URL管理器模块用法解析
Feb 03 #Python
Tensorflow实现多GPU并行方式
Feb 03 #Python
python如何通过twisted搭建socket服务
Feb 03 #Python
关于Tensorflow分布式并行策略
Feb 03 #Python
基于python修改srt字幕的时间轴
Feb 03 #Python
Python实现不规则图形填充的思路
Feb 02 #Python
Python ORM编程基础示例
Feb 02 #Python
You might like
用PHP 4.2书写安全的脚本
2006/10/09 PHP
在PHP模板引擎smarty生成随机数的方法和math函数详解
2014/04/24 PHP
ecshop后台编辑器替换成ueditor编辑器
2015/03/03 PHP
PHP获取数组最大值下标的方法
2015/05/12 PHP
PHP PDOStatement::fetch讲解
2019/01/31 PHP
IE autocomplete internet explorer's autocomplete
2007/06/30 Javascript
用javascript获取当页面上鼠标光标位置和触发事件的对象的代码
2009/12/09 Javascript
jquery的ajax简单结构示例代码
2014/02/17 Javascript
JS实现的一个简单的Autocomplete自动完成例子
2014/04/16 Javascript
jquery ajax结合thinkphp的getjson实现跨域的方法
2016/06/06 Javascript
jquery.validate表单验证插件使用方法解析
2016/11/07 Javascript
bootstrap 表单验证使用方法
2017/01/11 Javascript
那些精彩的JavaScript代码片段
2017/01/12 Javascript
解决html-jquery/js引用外部图片时遇到看不了或出现403的问题
2017/09/22 jQuery
使用vue-cli导入Element UI组件的方法
2018/05/16 Javascript
简述vue-cli中chainWebpack的使用方法
2019/07/30 Javascript
layui原生表单验证的实例
2019/09/09 Javascript
vue 使用class创建和清除水印的示例代码
2020/12/25 Vue.js
jQuery实现全选按钮
2021/01/01 jQuery
[05:36]DOTA2 2015国际邀请赛中国区预选赛第四日TOP10
2015/05/29 DOTA
Python中文编码那些事
2014/06/25 Python
Python函数式编程指南(四):生成器详解
2015/06/24 Python
Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
2016/06/14 Python
详解Python如何生成词云的方法
2018/06/01 Python
python numpy存取文件的方式
2020/04/01 Python
python中的逆序遍历实例
2019/12/25 Python
如何基于pythonnet调用halcon脚本
2020/01/20 Python
Python基于内置库pytesseract实现图片验证码识别功能
2020/02/24 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
2020/06/02 Python
python反扒机制的5种解决方法
2021/02/06 Python
5分钟实现Canvas鼠标跟随动画背景
2019/11/18 HTML / CSS
如何查看浏览器对html5的支持情况
2020/12/15 HTML / CSS
意大利时尚精品店:Nugnes 1920
2020/02/10 全球购物
法国春天百货官网:Printemps.com
2020/06/29 全球购物
2016幼儿园教师节新闻稿
2015/11/25 职场文书
Python基于Tkinter开发一个爬取B站直播弹幕的工具
2021/05/06 Python