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代理抓取并验证使用多线程实现
May 03 Python
Django框架下在URLconf中指定视图缓存的方法
Jul 23 Python
星球大战与Python之间的那些事
Jan 07 Python
python直接访问私有属性的简单方法
Jul 25 Python
python如何为被装饰的函数保留元数据
Mar 21 Python
对numpy Array [: ,] 的取值方法详解
Jul 02 Python
如何在Django配置文件里配置session链接
Aug 06 Python
Python中SQLite如何使用
May 27 Python
Django CBV模型源码运行流程详解
Aug 17 Python
python画图时设置分辨率和画布大小的实现(plt.figure())
Jan 08 Python
Python基础之常用库常用方法整理
Apr 30 Python
浏览器常用基本操作之python3+selenium4自动化测试(基础篇3)
May 21 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生成html文件方法总结
2014/12/01 PHP
PHP获取一年中每个星期的开始和结束日期的方法
2015/02/12 PHP
为你总结一些php信息函数
2015/10/21 PHP
PHP如何实现跨域
2016/05/30 PHP
PHP加密解密类实例代码
2016/07/20 PHP
jquery实现保存已选用户
2014/07/21 Javascript
node.js中的url.format方法使用说明
2014/12/10 Javascript
浅谈Nodejs观察者模式
2015/10/13 NodeJs
JavaScript必知必会(九)function 说起 闭包问题
2016/06/08 Javascript
Javarscript中模块(module)、加载(load)与捆绑(bundle)详解
2017/05/28 Javascript
vue 自定义全局方法,在组件里面的使用介绍
2018/02/28 Javascript
使用vue中的v-for遍历二维数组的方法
2018/03/07 Javascript
关于HTML5的data-*自定义属性的总结
2018/05/05 Javascript
[38:21]2014 DOTA2国际邀请赛中国区预选赛5.21 TongFu VS LGD-CDEC
2014/05/22 DOTA
从零学Python之入门(四)运算
2014/05/27 Python
Python回调函数用法实例详解
2015/07/02 Python
Python使用combinations实现排列组合的方法
2018/11/13 Python
对python条件表达式的四种实现方法小结
2019/01/30 Python
pytorch nn.Conv2d()中的padding以及输出大小方式
2020/01/10 Python
TensorFlow获取加载模型中的全部张量名称代码
2020/02/11 Python
Python变量及数据类型用法原理汇总
2020/08/06 Python
用OpenCV进行年龄和性别检测的实现示例
2021/01/29 Python
Lookfantastic台湾:英国彩妆美发保养购物网
2018/03/26 全球购物
Agoda.com官方网站:便宜预订全球酒店,高达80%的折扣
2018/04/04 全球购物
智能室内花园:Click & Grow
2021/01/29 全球购物
成品仓管员岗位职责
2013/12/11 职场文书
毕业生就业自荐书
2013/12/15 职场文书
小学生国旗下演讲稿
2014/04/25 职场文书
市场营销专业自荐书
2014/06/10 职场文书
2014年大学生党员评议表自我评价
2014/09/20 职场文书
监察局领导班子四风问题整改措施思想汇报
2014/10/05 职场文书
鲁冰花观后感
2015/06/10 职场文书
学习雷锋主题班会
2015/08/14 职场文书
2015年幼儿园师德师风建设工作总结
2015/10/23 职场文书
2016中秋节广告语
2016/01/28 职场文书
在windows server 2012 r2中安装mysql的详细步骤
2022/07/23 Servers