使用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的类实例属性访问规则探讨
Jan 30 Python
python分析nignx访问日志脚本分享
Feb 26 Python
Python函数式编程指南(三):迭代器详解
Jun 24 Python
python利用Guetzli批量压缩图片
Mar 23 Python
python编程实现希尔排序
Apr 13 Python
Tensorflow之Saver的用法详解
Apr 23 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
python 的 scapy库,实现网卡收发包的例子
Jul 23 Python
python修改文件内容的3种方法详解
Nov 15 Python
Python捕获异常堆栈信息的几种方法(小结)
May 18 Python
Python发送邮件实现基础解析
Aug 14 Python
Python答题卡识别并给出分数的实现代码
Jun 22 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
玩家交还《星际争霸》原始码光盘 暴雪报以厚礼
2017/05/05 星际争霸
php多重接口的实现方法
2015/06/20 PHP
PHP基于迭代实现文件夹复制、删除、查看大小等操作的方法
2017/08/11 PHP
PHP针对redis常用操作实例详解
2019/08/17 PHP
ExtJS4 动态生成的grid导出为excel示例
2014/05/02 Javascript
js实现鼠标触发图片抖动效果的方法
2015/02/27 Javascript
javascript运算符——位运算符全面介绍
2016/07/14 Javascript
js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
2016/08/02 Javascript
jQuery使用DataTable实现删除数据后重新加载功能
2017/02/27 Javascript
EasyUI为Numberbox添加blur事件的方法
2017/03/05 Javascript
addeventlistener监听scroll跟touch(实例讲解)
2017/08/04 Javascript
element ui分页多选,翻页记忆的实例
2019/09/03 Javascript
Element-UI+Vue模式使用总结
2020/01/02 Javascript
js+canvas实现转盘效果(两个版本)
2020/09/13 Javascript
[02:44]重置世界,颠覆未来——DOTA2 7.23版本震撼上线
2019/12/01 DOTA
基于python批量处理dat文件及科学计算方法详解
2018/05/08 Python
python实现图片转字符小工具
2019/04/30 Python
Python异步操作MySQL示例【使用aiomysql】
2019/05/16 Python
基于MSELoss()与CrossEntropyLoss()的区别详解
2020/01/02 Python
纯CSS3实现绘制各种图形实现代码详细整理
2012/12/26 HTML / CSS
YOOX美国官方网站:全球著名的多品牌时尚网络概念店
2016/09/11 全球购物
英国知名的护肤彩妆与时尚配饰大型综合零售电商:Unineed
2016/11/21 全球购物
Carter’s OshKosh加拿大:购买婴幼儿服装和童装
2018/11/27 全球购物
实现strstr功能,即在父串中寻找子串首次出现的位置
2016/08/05 面试题
商务助理岗位职责
2013/11/13 职场文书
迎元旦广播稿
2014/02/22 职场文书
中学校庆方案
2014/03/17 职场文书
电脑售后服务承诺书
2014/03/27 职场文书
奥林匹克的口号
2014/06/13 职场文书
2015年五四青年节演讲稿
2015/03/18 职场文书
《平行四边形的面积》教学反思
2016/02/16 职场文书
nginx简单配置多个server的方法
2021/03/31 Servers
完美解决golang go get私有仓库的问题
2021/05/05 Golang
Python中else的三种使用场景
2021/06/16 Python
MySQL连接控制插件介绍
2021/09/25 MySQL
Python Numpy库的超详细教程
2022/04/06 Python