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使用正则表达式检测密码强度源码分享
Jun 11 Python
Python psutil模块简单使用实例
Apr 28 Python
Python获取央视节目单的实现代码
Jul 25 Python
Python使用multiprocessing实现一个最简单的分布式作业调度系统
Mar 14 Python
python 读取txt中每行数据,并且保存到excel中的实例
Apr 29 Python
用Python下载一个网页保存为本地的HTML文件实例
May 21 Python
python 从文件夹抽取图片另存的方法
Dec 04 Python
Numpy与Pytorch 矩阵操作方式
Dec 27 Python
Python基于Tkinter编写crc校验工具
May 06 Python
Pandas缺失值2种处理方式代码实例
Jun 13 Python
Pythonic版二分查找实现过程原理解析
Aug 11 Python
pycharm 2020 1.1的安装流程
Sep 29 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
Laravel框架自定义公共函数的引入操作示例
2019/04/16 PHP
jquery 图片轮换效果
2010/07/29 Javascript
JavaScript高级程序设计(第3版)学习笔记6 初识js对象
2012/10/11 Javascript
Javscript调用iframe框架页面中函数的方法
2014/11/01 Javascript
jQuery中fadein与fadeout方法用法示例
2016/09/16 Javascript
深入理解bootstrap框架之入门准备
2016/10/09 Javascript
如何在 Vue.js 中使用第三方js库
2017/04/25 Javascript
vue 实现 tomato timer(蕃茄钟)实例讲解
2017/07/24 Javascript
vue下跨域设置的相关介绍
2017/08/26 Javascript
Angular2仿照微信UI实现9张图片上传和预览的示例代码
2017/10/19 Javascript
原生js实现仿window10系统日历效果的实例
2017/10/31 Javascript
Vue项目webpack打包部署到Tomcat刷新报404错误问题的解决方案
2018/05/15 Javascript
记React connect的几种写法(小结)
2018/09/18 Javascript
vue项目环境变量配置的实现方法
2018/10/12 Javascript
通过JQuery,JQueryUI和Jsplumb实现拖拽模块
2019/06/18 jQuery
[01:08:09]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#1Liquid VS Alliance第二局
2016/03/02 DOTA
[44:40]2018DOTA2亚洲邀请赛3月30日 小组赛A组Liquid VS OG
2018/03/31 DOTA
[34:44]Liquid vs TNC Supermajor 胜者组 BO3 第二场 6.4
2018/06/05 DOTA
[00:13]天涯墨客二技能展示
2018/08/25 DOTA
python服务器端收发请求的实现代码
2014/09/29 Python
django使用图片延时加载引起后台404错误
2017/04/18 Python
python实现排序算法解析
2018/09/08 Python
浅谈python连续赋值可能引发的错误
2018/11/10 Python
python+opencv实现阈值分割
2018/12/26 Python
详解Python字典的操作
2019/03/04 Python
python通过matplotlib生成复合饼图
2020/02/06 Python
python获得命令行输入的参数的两种方式
2020/11/02 Python
html5 拖拽上传图片实例演示
2013/04/01 HTML / CSS
FLOS美国官网:意大利高级照明工艺的传奇
2018/08/07 全球购物
女性时尚在线:IVRose
2019/02/23 全球购物
环境工程大学生个人的自我评价
2013/10/08 职场文书
违反交通安全法检讨书
2014/10/24 职场文书
党的群众路线教育实践活动制度建设计划
2014/11/03 职场文书
爱岗敬业事迹材料
2014/12/24 职场文书
《草船借箭》教学反思
2016/02/23 职场文书
vue实现input输入模糊查询的三种方式
2022/08/14 Vue.js