使用python画个小猪佩奇的示例代码


Posted in Python onJune 06, 2018

使用python画个小猪佩奇的示例代码 

本原理

选好画板大小,设置好画笔颜色、粗细,定位好位置,依次画鼻子、头、耳朵、眼睛、腮、嘴、身体、手脚、尾巴,完事儿。

都知道,Turtle 是 Python 内置的一个比较有趣味的模块,俗称“海龟绘图”,它是基于 Tkinter 模块打造,提供一些简单的绘图工具。

在海龟作图中,我们可以编写指令让一个虚拟的(想象中的)海龟在屏幕上来回移动。这个海龟带着一只钢笔,我们可以让海龟无论移动到哪都使用这只钢笔来绘制线条。通过编写代码,以各种很酷的模式移动海龟,我们可以绘制出令人惊奇的图片。使用海龟作图,我们不仅能够只用几行代码就创建出令人印象深刻的视觉效果,而且还可以跟随海龟看看每行代码如何影响到它的移动。这能够帮助我们理解代码的逻辑。

所以,海龟作图也常被用作新手学习 Python 的一种方式。更丰富详细的功能及知识可以参考官方文档:

https://docs.python.org/3/library/turtle.html

from turtle import*
def nose(x,y):#鼻子
 penup()#提起笔
 goto(x,y)#定位
 pendown()#落笔,开始画
 setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
 begin_fill()#准备开始填充图形
 a=0.4
 for i in range(120):
  if 0<=i<30 or 60<=i<90:
   a=a+0.08
   left(3) #向左转3度
   forward(a) #向前走a的步长
  else:
   a=a-0.08
   left(3)
   forward(a)
 end_fill()#填充完成
 penup()
 setheading(90)
 forward(25)
 setheading(0)
 forward(10)
 pendown()
 pencolor(255,155,192)#画笔颜色
 setheading(10)
 begin_fill()
 circle(5)
 color(160,82,45)#返回或设置pencolor和fillcolor
 end_fill()
 penup()
 setheading(0)
 forward(20)
 pendown()
 pencolor(255,155,192)
 setheading(10)
 begin_fill()
 circle(5)
 color(160,82,45)
 end_fill()
def head(x,y):#头
 color((255,155,192),"pink")
 penup()
 goto(x,y)
 setheading(0)
 pendown()
 begin_fill()
 setheading(180)
 circle(300,-30)
 circle(100,-60)
 circle(80,-100)
 circle(150,-20)
 circle(60,-95)
 setheading(161)
 circle(-300,15)
 penup()
 goto(-100,100)
 pendown()
 setheading(-30)
 a=0.4
 for i in range(60):
  if 0<=i<30 or 60<=i<90:
   a=a+0.08
   lt(3) #向左转3度
   fd(a) #向前走a的步长
  else:
   a=a-0.08
   lt(3)
   fd(a)
 end_fill()
def ears(x,y): #耳朵
 color((255,155,192),"pink")
 penup()
 goto(x,y)
 pendown()
 begin_fill()
 setheading(100)
 circle(-50,50)
 circle(-10,120)
 circle(-50,54)
 end_fill()
 penup()
 setheading(90)
 forward(-12)
 setheading(0)
 forward(30)
 pendown()
 begin_fill()
 setheading(100)
 circle(-50,50)
 circle(-10,120)
 circle(-50,56)
 end_fill()
def eyes(x,y):#眼睛
 color((255,155,192),"white")
 penup()
 setheading(90)
 forward(-20)
 setheading(0)
 forward(-95)
 pendown()
 begin_fill()
 circle(15)
 end_fill()
 color("black")
 penup()
 setheading(90)
 forward(12)
 setheading(0)
 forward(-3)
 pendown()
 begin_fill()
 circle(3)
 end_fill()
 color((255,155,192),"white")
 penup()
 seth(90)
 forward(-25)
 seth(0)
 forward(40)
 pendown()
 begin_fill()
 circle(15)
 end_fill()
 color("black")
 penup()
 setheading(90)
 forward(12)
 setheading(0)
 forward(-3)
 pendown()
 begin_fill()
 circle(3)
 end_fill()
def cheek(x,y):#腮
 color((255,155,192))
 penup()
 goto(x,y)
 pendown()
 setheading(0)
 begin_fill()
 circle(30)
 end_fill()
def mouth(x,y): #嘴
 color(239,69,19)
 penup()
 goto(x,y)
 pendown()
 setheading(-80)
 circle(30,40)
 circle(40,80)
def setting():   #参数设置
 pensize(4)
 hideturtle()  #使乌龟无形(隐藏)
 colormode(255)  #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内
 color((255,155,192),"pink")
 setup(840,500)
 speed(10)
def main():
 setting()   #画布、画笔设置
 nose(-100,100)  #鼻子
 head(-69,167)  #头
 ears(0,160)   #耳朵
 eyes(0,140)   #眼睛
 cheek(80,10)  #腮
 mouth(-20,30)  #嘴
 done()
if __name__ == '__main__':
 main()

总结

以上所述是小编给大家介绍的使用python画个小猪佩奇的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Jun 23 Python
你真的了解Python的random模块吗?
Dec 12 Python
python3.6.3+opencv3.3.0实现动态人脸捕获
May 25 Python
Python 数据可视化pyecharts的使用详解
Jun 26 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
redis数据库及与python交互用法简单示例
Nov 01 Python
flask框架蓝图和子域名配置详解
Jan 25 Python
OpenCV中VideoCapture类的使用详解
Feb 14 Python
python DES加密与解密及hex输出和bs64格式输出的实现代码
Apr 13 Python
Python lxml库的简单介绍及基本使用讲解
Dec 22 Python
python Polars库的使用简介
Apr 21 Python
python控制台打印log输出重复的解决方法
May 14 Python
python re模块的高级用法详解
Jun 06 #Python
Python实现的爬取网易动态评论操作示例
Jun 06 #Python
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
Jun 06 #Python
利用python如何处理百万条数据(适用java新手)
Jun 06 #Python
Python3实现的Mysql数据库操作封装类
Jun 06 #Python
python操作redis方法总结
Jun 06 #Python
目前最全的python的就业方向
Jun 05 #Python
You might like
在数据量大(超过10万)的情况下
2007/01/15 PHP
关于查看MSSQL 数据库 用户每个表 占用的空间大小
2013/06/21 PHP
Linux系统中为php添加pcntl扩展
2016/08/28 PHP
php版微信小店API二次开发及使用示例
2016/11/12 PHP
PHP+Ajax无刷新带进度条图片上传示例
2017/02/08 PHP
win7 wamp 64位 php环境开启curl服务遇到的问题及解决方法
2018/09/16 PHP
页面载入结束自动调用js函数示例
2013/09/23 Javascript
js截取小数点后几位的写法
2013/11/14 Javascript
如何从jQuery的ajax请求中删除X-Requested-With
2013/12/11 Javascript
jquery 选取方法都有哪些
2014/05/18 Javascript
使用JQuery实现智能表单验证功能
2016/03/08 Javascript
AngularJs directive详解及示例代码
2016/09/01 Javascript
jQuery实现的简单拖拽功能示例
2016/09/13 Javascript
NodeJS整合银联网关支付(DEMO)
2016/11/09 NodeJs
JS继承与闭包及JS实现继承的三种方式
2017/10/15 Javascript
vue-cli配置文件——config篇
2018/01/04 Javascript
对Layer UI 模块化的用法详解
2019/09/26 Javascript
微信小程序工具函数封装
2019/10/28 Javascript
vue中用 async/await 来处理异步操作
2020/07/18 Javascript
python编程实现随机生成多个椭圆实例代码
2018/01/03 Python
python爬取哈尔滨天气信息
2018/07/14 Python
Python编程flask使用页面模版的方法
2018/12/28 Python
python抖音表白程序源代码
2019/04/07 Python
手把手教你pycharm专业版安装破解教程(linux版)
2019/09/26 Python
Python 高效编程技巧分享
2020/09/10 Python
携程旅行网:中国领先的在线旅行服务公司
2017/02/17 全球购物
Giuseppe Zanotti美国官方网站:将鞋履视为高级时装般精心制作
2018/02/06 全球购物
英国独特家具和家庭用品购物网站:Cuckooland
2020/08/30 全球购物
大学生物业管理求职信
2013/10/24 职场文书
业务部门经理岗位职责
2014/02/23 职场文书
领导失职检讨书
2014/02/24 职场文书
2014年司法局工作总结
2014/12/11 职场文书
业务员辞职信范文
2015/03/02 职场文书
攻击最高的10只幽灵系神奇宝贝,坚盾剑怪排第一,第五最为可怕
2022/03/18 日漫
PostgreSQL出现死锁该如何解决
2022/05/30 PostgreSQL
Python中的协程(Coroutine)操作模块(greenlet、gevent)
2022/05/30 Python