Python使用turtle库绘制小猪佩奇(实例代码)


Posted in Python onJanuary 16, 2020

turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制。turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言。

turtle库绘制图形有一个基本框架:一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形。刚开始绘制时,小海龟位于画布正中央,此处坐标为(0,0),前进方向为水平右方。
在Python3系列版本安装目录的Lib文件夹下可以找到turtle.py文件。

下面通过代码给大家介绍Python使用turtle库绘制小猪佩奇,

具体代码如下所示:

# -*- coding:utf-8 -*-
import turtle as t
import time
def main():
 t.pensize(4)
 t.hideturtle()
 t.colormode(255)
 t.color((255, 155, 192), "pink")
 t.setup(840, 500)
 t.speed(10)
 # 鼻子
 t.pu()
 t.goto(-100, 100)
 t.pd()
 t.seth(-30)
 t.begin_fill()
 a = 0.4
 for i in range(120):
  if 0 <= i < 30 or 60 <= i < 90:
   a = a + 0.08
   t.lt(3) # 向左转3度
   t.fd(a) # 向前走a的步长
  else:
   a = a - 0.08
   t.lt(3)
   t.fd(a)
 t.end_fill()
 t.pu()
 t.seth(90)
 t.fd(25)
 t.seth(0)
 t.fd(10)
 t.pd()
 t.pencolor(255, 155, 192)
 t.seth(10)
 t.begin_fill()
 t.circle(5)
 t.color(160, 82, 45)
 t.end_fill()
 t.pu()
 t.seth(0)
 t.fd(20)
 t.pd()
 t.pencolor(255, 155, 192)
 t.seth(10)
 t.begin_fill()
 t.circle(5)
 t.color(160, 82, 45)
 t.end_fill()
 # 头
 t.color((255, 155, 192), "pink")
 t.pu()
 t.seth(90)
 t.fd(41)
 t.seth(0)
 t.fd(0)
 t.pd()
 t.begin_fill()
 t.seth(180)
 t.circle(300, -30)
 t.circle(100, -60)
 t.circle(80, -100)
 t.circle(150, -20)
 t.circle(60, -95)
 t.seth(161)
 t.circle(-300, 15)
 t.pu()
 t.goto(-100, 100)
 t.pd()
 t.seth(-30)
 a = 0.4
 for i in range(60):
  if 0 <= i < 30 or 60 <= i < 90:
   a = a + 0.08
   t.lt(3) # 向左转3度
   t.fd(a) # 向前走a的步长
  else:
   a = a - 0.08
   t.lt(3)
   t.fd(a)
 t.end_fill()
 # 耳朵
 t.color((255, 155, 192), "pink")
 t.pu()
 t.seth(90)
 t.fd(-7)
 t.seth(0)
 t.fd(70)
 t.pd()
 t.begin_fill()
 t.seth(100)
 t.circle(-50, 50)
 t.circle(-10, 120)
 t.circle(-50, 54)
 t.end_fill()
 t.pu()
 t.seth(90)
 t.fd(-12)
 t.seth(0)
 t.fd(30)
 t.pd()
 t.begin_fill()
 t.seth(100)
 t.circle(-50, 50)
 t.circle(-10, 120)
 t.circle(-50, 56)
 t.end_fill()
 # 眼睛
 t.color((255, 155, 192), "white")
 t.pu()
 t.seth(90)
 t.fd(-20)
 t.seth(0)
 t.fd(-95)
 t.pd()
 t.begin_fill()
 t.circle(15)
 t.end_fill()
 t.color("black")
 t.pu()
 t.seth(90)
 t.fd(12)
 t.seth(0)
 t.fd(-3)
 t.pd()
 t.begin_fill()
 t.circle(3)
 t.end_fill()
 t.color((255, 155, 192), "white")
 t.pu()
 t.seth(90)
 t.fd(-25)
 t.seth(0)
 t.fd(40)
 t.pd()
 t.begin_fill()
 t.circle(15)
 t.end_fill()
 t.color("black")
 t.pu()
 t.seth(90)
 t.fd(12)
 t.seth(0)
 t.fd(-3)
 t.pd()
 t.begin_fill()
 t.circle(3)
 t.end_fill()
 # 腮
 t.color((255, 155, 192))
 t.pu()
 t.seth(90)
 t.fd(-95)
 t.seth(0)
 t.fd(65)
 t.pd()
 t.begin_fill()
 t.circle(30)
 t.end_fill()
 # 嘴
 t.color(239, 69, 19)
 t.pu()
 t.seth(90)
 t.fd(15)
 t.seth(0)
 t.fd(-100)
 t.pd()
 t.seth(-80)
 t.circle(30, 40)
 t.circle(40, 80)
 # 身体
 t.color("red", (255, 99, 71))
 t.pu()
 t.seth(90)
 t.fd(-20)
 t.seth(0)
 t.fd(-78)
 t.pd()
 t.begin_fill()
 t.seth(-130)
 t.circle(100, 10)
 t.circle(300, 30)
 t.seth(0)
 t.fd(230)
 t.seth(90)
 t.circle(300, 30)
 t.circle(100, 3)
 t.color((255, 155, 192), (255, 100, 100))
 t.seth(-135)
 t.circle(-80, 63)
 t.circle(-150, 24)
 t.end_fill()
 # 手
 t.color((255, 155, 192))
 t.pu()
 t.seth(90)
 t.fd(-40)
 t.seth(0)
 t.fd(-27)
 t.pd()
 t.seth(-160)
 t.circle(300, 15)
 t.pu()
 t.seth(90)
 t.fd(15)
 t.seth(0)
 t.fd(0)
 t.pd()
 t.seth(-10)
 t.circle(-20, 90)
 t.pu()
 t.seth(90)
 t.fd(30)
 t.seth(0)
 t.fd(237)
 t.pd()
 t.seth(-20)
 t.circle(-300, 15)
 t.pu()
 t.seth(90)
 t.fd(20)
 t.seth(0)
 t.fd(0)
 t.pd()
 t.seth(-170)
 t.circle(20, 90)
 # 脚
 t.pensize(10)
 t.color((240, 128, 128))
 t.pu()
 t.seth(90)
 t.fd(-75)
 t.seth(0)
 t.fd(-180)
 t.pd()
 t.seth(-90)
 t.fd(40)
 t.seth(-180)
 t.color("black")
 t.pensize(15)
 t.fd(20)
 t.pensize(10)
 t.color((240, 128, 128))
 t.pu()
 t.seth(90)
 t.fd(40)
 t.seth(0)
 t.fd(90)
 t.pd()
 t.seth(-90)
 t.fd(40)
 t.seth(-180)
 t.color("black")
 t.pensize(15)
 t.fd(20)
 # 尾巴
 t.pensize(4)
 t.color((255, 155, 192))
 t.pu()
 t.seth(90)
 t.fd(70)
 t.seth(0)
 t.fd(95)
 t.pd()
 t.seth(0)
 t.circle(70, 20)
 t.circle(10, 330)
if __name__ == '__main__':
 # 画小猪佩奇
 main()
 time.sleep(10)

执行结果:

Python使用turtle库绘制小猪佩奇(实例代码)

总结

以上所述是小编给大家介绍的Python使用turtle库绘制小猪佩奇,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python程序设计入门(5)类的使用简介
Jun 16 Python
Python批量按比例缩小图片脚本分享
May 21 Python
Python创建二维数组实例(关于list的一个小坑)
Nov 07 Python
《Python学习手册》学习总结
Jan 17 Python
python使用itchat实现手机控制电脑
Feb 22 Python
利用Python求阴影部分的面积实例代码
Dec 05 Python
python print出共轭复数的方法详解
Jun 25 Python
Pandas-Cookbook 时间戳处理方式
Dec 07 Python
在Python中实现函数重载的示例代码
Dec 12 Python
Python celery原理及运行流程解析
Jun 13 Python
用python爬虫批量下载pdf的实现
Dec 01 Python
使用Django的JsonResponse返回数据的实现
Jan 15 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
Jan 16 #Python
python如何使用Redis构建分布式锁
Jan 16 #Python
Python中url标签使用知识点总结
Jan 16 #Python
PyTorch的SoftMax交叉熵损失和梯度用法
Jan 15 #Python
pytorch方法测试——激活函数(ReLU)详解
Jan 15 #Python
pytorch的batch normalize使用详解
Jan 15 #Python
pytorch方法测试详解——归一化(BatchNorm2d)
Jan 15 #Python
You might like
什么是MVC,好东西啊
2007/05/03 PHP
php 静态变量的初始化
2009/11/15 PHP
PHP中3种生成XML文件方法的速度效率比较
2012/10/06 PHP
基于curl数据采集之单页面采集函数get_html的使用
2013/04/28 PHP
解密ThinkPHP3.1.2版本之模板继承
2014/06/19 PHP
php绘制一个扇形的方法
2015/01/24 PHP
PHP脚本自动识别验证码查询汽车违章
2016/12/20 PHP
php+mysql开发中的经验与常识小结
2019/03/25 PHP
一个基于jquery的文本框记数器
2012/09/19 Javascript
jQuery实现简单的间隔向上滚动效果
2015/03/09 Javascript
Jquery1.9.1源码分析系列(十五)动画处理之外篇
2015/12/04 Javascript
微信小程序视图template模板引用的实例详解
2017/09/20 Javascript
jQuery创建折叠式菜单
2019/06/15 jQuery
使用Promise封装小程序wx.request的实现方法
2019/11/13 Javascript
Element Breadcrumb 面包屑的使用方法
2020/07/26 Javascript
nodejs使用Sequelize框架操作数据库的实现
2020/10/21 NodeJs
javascript中闭包closure的深入讲解
2021/03/03 Javascript
Python创建模块及模块导入的方法
2015/05/27 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
Python使用修饰器进行异常日志记录操作示例
2019/03/19 Python
python二维码操作:对QRCode和MyQR入门详解
2019/06/24 Python
python 执行终端/控制台命令的例子
2019/07/12 Python
使用matplotlib动态刷新指定曲线实例
2020/04/23 Python
Python实现清理微信僵尸粉功能示例【基于itchat模块】
2020/05/29 Python
python读取hdfs上的parquet文件方式
2020/06/06 Python
pytorch 计算ConvTranspose1d输出特征大小方式
2020/06/23 Python
css3 border-radius属性详解
2017/07/05 HTML / CSS
localstorage和sessionstorage使用记录(推荐)
2017/05/23 HTML / CSS
英国袜子店:Sock Shop
2017/01/11 全球购物
读书伴我成长演讲稿
2014/05/07 职场文书
求职信格式要求
2014/05/23 职场文书
员工离职通知函
2015/04/25 职场文书
东京审判观后感
2015/06/01 职场文书
青涩记忆观后感
2015/06/18 职场文书
Python虚拟环境virtualenv是如何使用的
2021/06/20 Python
python神经网络Xception模型
2022/05/06 Python