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 示例分享---逻辑推理编程解决八皇后
Jul 20 Python
Python中使用PIPE操作Linux管道
Feb 04 Python
Python实现将n个点均匀地分布在球面上的方法
Mar 12 Python
python提取内容关键词的方法
Mar 16 Python
Python文件与文件夹常见基本操作总结
Sep 19 Python
200行自定义python异步非阻塞Web框架
Mar 15 Python
python距离测量的方法
Mar 06 Python
python打包生成的exe文件运行时提示缺少模块的解决方法
Oct 31 Python
很酷的python表白工具 你喜欢我吗
Apr 11 Python
用Python配平化学方程式的方法
Jul 20 Python
Python库skimage绘制二值图像代码实例
Apr 10 Python
OpenCV+python实现膨胀和腐蚀的示例
Dec 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新手上路(十)
2006/10/09 PHP
php 数组的创建、调用和更新实现代码
2009/03/09 PHP
了解Joomla 这款来自国外的php网站管理系统
2010/03/11 PHP
php判断变量类型常用方法
2012/04/24 PHP
PHP静态调用非静态方法的应用分析
2013/05/02 PHP
PHP实现链表的定义与反转功能示例
2018/06/09 PHP
Aster vs KG BO3 第二场2.18
2021/03/10 DOTA
基于jquery的兼容各种浏览器的iframe自适应高度的脚本
2010/08/13 Javascript
JavaScript中的几个关键概念的理解-原型链的构建
2011/05/12 Javascript
jquery 获取自定义属性(attr和prop)的实现代码
2012/06/27 Javascript
网站如何做到完全不需要jQuery也可以满足简单需求
2013/06/27 Javascript
用js闭包的方法实现多点标注冒泡示例
2014/05/29 Javascript
用javascript关闭本窗口技巧小结
2014/09/05 Javascript
AngularJS学习笔记之TodoMVC的分析
2015/02/22 Javascript
jQuery实现渐变下拉菜单的简单方法
2015/03/11 Javascript
jquery控制页面的展开和隐藏实现方法(推荐)
2016/10/15 Javascript
给easyui的datebox控件添加清空按钮的实现方法
2016/11/09 Javascript
Javascript中常用类型的格式化方法小结
2016/12/26 Javascript
微信小程序 label 组件详解及简单实例
2017/01/10 Javascript
JS如何判断浏览器类型和详细区分IE各版本浏览器
2017/03/04 Javascript
修改 bootstrap table 默认detailRow样式的实例代码
2017/07/21 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
学习python (1)
2006/10/31 Python
Python WEB应用部署的实现方法
2019/01/02 Python
selenium+python环境配置教程详解
2019/05/28 Python
Python正则表达式匹配日期与时间的方法
2019/07/07 Python
Pandas操作CSV文件的读写实现方法
2019/11/13 Python
python实现上传文件到linux指定目录的方法
2020/01/03 Python
Python中使用socks5设置全局代理的方法示例
2020/04/15 Python
开普敦通行证:Cape Town Pass
2019/07/18 全球购物
飞利浦西班牙官方网站:Philips西班牙
2020/02/17 全球购物
安全生产投入制度
2014/01/29 职场文书
教师通用专业自荐书范文
2014/02/11 职场文书
实习单位评语
2014/04/26 职场文书
国家税务局领导班子对照检查材料思想汇报
2014/10/04 职场文书
Pandas数据结构之Series的使用
2022/03/31 Python