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内置函数的用法实例教程
Sep 08 Python
跟老齐学Python之编写类之一创建实例
Oct 11 Python
python实现根据主机名字获得所有ip地址的方法
Jun 28 Python
python轻松查到删除自己的微信好友
Jan 10 Python
python实现折半查找和归并排序算法
Apr 14 Python
Python初学时购物车程序练习实例(推荐)
Aug 08 Python
python matplotlib坐标轴设置的方法
Dec 05 Python
python 获取当天每个准点时间戳的实例
May 22 Python
Python实现深度遍历和广度遍历的方法
Jan 22 Python
Python实现最大子序和的方法示例
Jul 05 Python
PyCharm更改字体和界面样式的方法步骤
Sep 27 Python
Python如何将图像音视频等资源文件隐藏在代码中(小技巧)
Feb 16 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
php setcookie(name, value, expires, path, domain, secure) 参数详解
2013/06/28 PHP
PHP页面输出时js设置input框的选中值
2016/09/30 PHP
数据结构之利用PHP实现二分搜索树
2020/10/25 PHP
原生JavaScript+LESS实现瀑布流
2014/12/12 Javascript
JavaScript 事件入门知识
2015/04/13 Javascript
NodeJS实现阿里大鱼短信通知发送
2016/01/17 NodeJs
JS动态计算移动端rem的解决方案
2016/10/14 Javascript
微信小程序 石头剪刀布实例代码
2017/01/04 Javascript
JavaScript循环_动力节点Java学院整理
2017/06/28 Javascript
如何用input标签和jquery实现多图片的上传和回显功能
2018/05/16 jQuery
详解jquery和vue对比
2019/04/16 jQuery
浅谈使用nodejs搭建web服务器的过程
2020/07/20 NodeJs
原生js实现滑块区间组件
2021/01/20 Javascript
python调用shell的方法
2013/11/20 Python
使用Python装饰器在Django框架下去除冗余代码的教程
2015/04/16 Python
Django发送html邮件的方法
2015/05/26 Python
python爬取w3shcool的JQuery课程并且保存到本地
2017/04/06 Python
python利用MethodType绑定方法到类示例代码
2017/08/27 Python
机器学习python实战之手写数字识别
2017/11/01 Python
python类的方法属性与方法属性的动态绑定代码详解
2017/12/27 Python
浅谈flask源码之请求过程
2018/07/26 Python
Python中函数的基本定义与调用及内置函数详解
2019/05/13 Python
python实现对象列表根据某个属性排序的方法详解
2019/06/11 Python
关于Python-faker的函数效果一览
2019/11/28 Python
Python实现自动访问网页的例子
2020/02/21 Python
英国现代家具和照明购物网站:Heal’s
2019/10/30 全球购物
大学三年的自我评价
2013/12/25 职场文书
法学毕业生自我鉴定
2014/01/31 职场文书
乡镇三严三实学习心得体会
2014/10/13 职场文书
教育合作协议范本
2014/10/17 职场文书
2015年护士医德医风自我评价
2015/03/03 职场文书
小区环境卫生倡议书
2015/04/29 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
高三生物教学反思
2016/02/22 职场文书
CSS实现章节添加自增序号的方法
2021/06/23 HTML / CSS
httpclient调用远程接口的方法
2022/08/14 Java/Android