使用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 22 Python
Python+PIL实现支付宝AR红包
Feb 09 Python
用TensorFlow实现戴明回归算法的示例
May 02 Python
python RabbitMQ 使用详细介绍(小结)
Nov 08 Python
几个适合python初学者的简单小程序,看完受益匪浅!(推荐)
Apr 16 Python
Django后端接收嵌套Json数据及解析详解
Jul 17 Python
Python Django中的STATIC_URL 设置和使用方式
Mar 27 Python
Python3批量创建Crowd用户并分配组
May 20 Python
Python基于Socket实现简易多人聊天室的示例代码
Nov 29 Python
python绕过图片滑动验证码实现爬取PTA所有题目功能 附源码
Jan 06 Python
Python Django项目和应用的创建详解
Nov 27 Python
Python利用Turtle绘制哆啦A梦和小猪佩奇
Apr 04 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
PHP一些有意思的小区别
2006/12/06 PHP
php getsiteurl()函数
2009/09/05 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
php数组合并与拆分实例分析
2015/06/12 PHP
使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能示例
2017/09/15 PHP
gearman中任务的优先级和返回状态实例分析
2020/02/27 PHP
基于PHP实现解密或加密Cloudflar邮箱保护
2020/06/24 PHP
弹出广告特效(一个IP只弹出一次)的代码
2007/07/27 Javascript
js 模拟实现类似c#下的hashtable的简单功能代码
2010/01/24 Javascript
javascript下数值型比较难点说明
2010/06/07 Javascript
基于jQuery替换table中的内容并显示进度条的代码
2011/08/02 Javascript
jquery实现带单选按钮的表格行选中时高亮显示
2013/08/01 Javascript
JQ获取动态加载的图片大小的正确方法分享
2013/11/08 Javascript
图解Sublime Text3使用技巧
2015/12/21 Javascript
AngularJs bootstrap搭载前台框架——js控制部分
2016/09/01 Javascript
vue实现ToDoList简单实例
2017/02/07 Javascript
Angular中使用MathJax遇到的一些问题
2017/12/15 Javascript
nodejs 最新版安装npm 的使用详解
2018/01/18 NodeJs
使用Vue.js和Flask来构建一个单页的App的示例
2018/03/21 Javascript
JavaScript继承定义与用法实践分析
2018/05/28 Javascript
写一个Vue Popup组件
2019/02/25 Javascript
微信小程序 弹窗输入组件的实现解析
2019/08/12 Javascript
JavaScript快速调试的两个技巧
2020/11/04 Javascript
Python中使用PIL库实现图片高斯模糊实例
2015/02/08 Python
在Python中编写数据库模块的教程
2015/04/29 Python
简单介绍Python中用于求最小值的min()方法
2015/05/15 Python
Python中常用操作字符串的函数与方法总结
2016/02/04 Python
Python读取Excel表格,并同时画折线图和柱状图的方法
2018/10/14 Python
Django 模型类(models.py)的定义详解
2019/07/19 Python
Python 一键获取百度网盘提取码的方法
2019/08/01 Python
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
个人能力自我鉴赏
2014/01/25 职场文书
关于安全的演讲稿
2014/05/09 职场文书
小学生国庆65周年演讲稿范文(2篇)
2014/09/21 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
公务员的复习计划书,请收下!
2019/07/15 职场文书