转自:恋习Python(微信号:sldata2017)

感觉这个特别有意思,哈哈哈

观察这个图像可以发现,小猪佩奇在构图基本是各种曲线,类抛物线、类圆、类椭圆、类二次贝塞尔曲线。这里说的都是“类”,这也正是小猪佩奇的构图精髓,一种手绘风格,而不是标准刻板的线条。在前端技术选型上,画图首先想到的是svg、canvas,但它们本身就擅长画图,而且网上都有在线编辑svg的工具,这就没意思了,我想佩奇也不会答应的。于是我想用纯粹的Python来做,这样更有挑战,因为画图画曲线不是Python擅长的事情。

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

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

在海龟作图中,我们可以编写指令让一个虚拟的(想象中的)海龟在屏幕上来回移动。这个海龟带着一只钢笔,我们可以让海龟无论移动到哪都使用这只钢笔来绘制线条。通过编写代码,以各种很酷的模式移动海龟,我们可以绘制出令人惊奇的图片。使用海龟作图,我们不仅能够只用几行代码就创建出令人印象深刻的视觉效果,而且还可以跟随海龟看看每行代码如何影响到它的移动。这能够帮助我们理解代码的逻辑。所以海龟作图也常被用作新手学习 Python 的一种方式。更丰富详细的功能及知识可以参考:(官方文档:https://docs.python.org/3/library/turtle.html)

了解了tuttle的用法之后就可以开始实战了。

先上效果图:

from turtle import*def nose(x,y):#鼻子penup()#提起笔goto(x,y)#定位pendown()#落笔,开始画setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)begin_fill()#准备开始填充图形a=0.4for i in range(120):if 0<=i<30 or 60<=i<90:a=a+0.08left(3) #向左转3度forward(a) #向前走a的步长else:a=a-0.08left(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和fillcolorend_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.4for i in range(60):if 0<=i<30 or 60<=i<90:a=a+0.08lt(3) #向左转3度fd(a) #向前走a的步长else:a=a-0.08lt(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 body(x,y):#身体color("red",(255,99,71))penup()goto(x,y)pendown()begin_fill()setheading(-130)circle(100,10)circle(300,30)setheading(0)forward(230)setheading(90)circle(300,30)circle(100,3)color((255,155,192),(255,100,100))setheading(-135)circle(-80,63)circle(-150,24)end_fill()def hands(x,y):#手color((255,155,192))penup()goto(x,y)pendown()setheading(-160)circle(300,15)penup()setheading(90)forward(15)setheading(0)forward(0)pendown()setheading(-10)circle(-20,90)penup()setheading(90)forward(30)setheading(0)forward(237)pendown()setheading(-20)circle(-300,15)penup()setheading(90)forward(20)setheading(0)forward(0)pendown()setheading(-170)circle(20,90)def foot(x,y):#脚pensize(10)color((240,128,128))penup()goto(x,y)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)fd(20)pensize(10)color((240,128,128))penup()setheading(90)forward(40)setheading(0)forward(90)pendown()setheading(-90)forward(40)setheading(-180)color("black")pensize(15)fd(20)def tail(x,y):#尾巴pensize(4)color((255,155,192))penup()goto(x,y)pendown()seth(0)circle(70,20)circle(10,330)circle(70,30)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)       #嘴body(-32,-8)        #身体hands(-56,-45)      #手foot(2,-177)        #脚tail(148,-155)      #尾巴done()if __name__ == '__main__':main()

python 20秒画完小猪佩奇“社会人”相关推荐

  1. python画小猪佩奇视频_用 Python 20 秒画完小猪佩奇“社会人”!附效果视频+完整代码...

    原标题:用 Python 20 秒画完小猪佩奇"社会人"!附效果视频+完整代码 导读:今年社交平台上最火的带货女王是谁?范冰冰?杨幂?Angelababy?不,是猪猪女孩小猪佩奇. ...

  2. Python 20 秒画完小猪佩奇“社会人”!

    每天写代码的程序员,你们知道今年社交平台上最火的带货女王是谁吗?范冰冰?杨幂?Angelababy?不,是猪猪女孩小猪佩奇. 如果你经常用抖音.快手.B 站.知乎.微博--或者类似的任何一个内容或社交 ...

  3. 这个程序员用 Python 20 秒画完小猪佩奇“社会人”!

    点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 作者 | 丁彦军 责编 | 唐小引 每天写代码的程序员,你们知道今年社交平台上最火的带货女王 ...

  4. python写小猪佩奇_这个程序员用 Python 20 秒画完小猪佩奇“社会人”!

    点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 作者 | 丁彦军 责编 | 唐小引 每天写代码的程序员,你们知道今年社交平台上最火的带货女王 ...

  5. 用 Python 20秒画完小猪佩奇“社会人”

    Python中文社区全球Python中文开发者的精神部落 今年社交平台上最火的带货女王是谁?范冰冰?杨幂?Angelababy?不,是猪猪女孩小猪佩奇. 如果你经常用抖音.快手.B站.知乎.微博--或 ...

  6. turtle 20秒画完小猪佩奇“社会人”

    转载:https://blog.csdn.net/csdnsevenn/article/details/80650456 图片源自网络 作者 丁彦军 如需转载,请联系原作者授权. 今年社交平台上最火的 ...

  7. python画图小猪佩奇_吊炸天!Python 20秒画出小猪佩奇

    原标题:吊炸天!Python 20秒画出小猪佩奇 今年社交平台上的带货女王是谁?范冰冰?杨幂?Angelababy?不,是猪猪女孩小猪佩奇. 小猪佩奇在构图基本是各种曲线,类抛物线.类圆.类椭圆.类二 ...

  8. 用python turtle画人_Python中如何用turtle库画出小猪佩奇社会人

    1 from turtle import * 2 3 def nose(x,y):#鼻子 4 penup()#提起笔 5 goto(x,y)#定位 6 pendown()#落笔,开始画 7 sethe ...

  9. 小猪佩奇python_一段代码画出小猪佩奇社会人 Python

    复制内容到剪贴板 代码:# coding:utf-8 import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.color(( ...

最新文章

  1. mysql query日期_如何获取mysql中两个日期之间的日期列表select query
  2. 深入Java泛型(四):RxJava中泛型的使用分析
  3. iPhone开发之 ARC是什么
  4. Acronis True Image无法卸载或者卸载导致无法开机解决办法
  5. android鼠标滚轮事件坐标,android 处理鼠标滚轮事件 【转】
  6. 设计模式 原型模式_原型设计模式:创建另一个小车
  7. 计算机网络西北大学,西北大学计算机网络复习资料 (拟).doc
  8. 数字证书转换cer---pem
  9. Spring Cloud连载(2)搭建开发环境
  10. win10python安装失败_python3.6 + win10 安装 python-Levenshtein 错误记录
  11. QQ号能否成为互联网通行证?
  12. 计算机应用基础知识竞赛题,计算机基础知识题库
  13. html中使用js实现体彩11选5随机选号
  14. python opencv 摄像头亮度_python 基于opencv操作摄像头
  15. MongoDB 文档操作之插入、查询
  16. android lame,Android 采用Lame编码器编码mp3文件
  17. 千橡校内网更名人人网
  18. ARM与裸机开发教程
  19. vc中cout如何解除fixed控制_C++ fixed用法详解
  20. 超级爆笑学生作文大全

热门文章

  1. springboot微服务注册到nacos平台
  2. Leetcode PHP题解--D70 784. Letter Case Permutation
  3. 计算机报录比多少算高,报录比多少合适?怎么算好考?我来告诉你答案
  4. 热像仪和夜视仪的区别
  5. 从FastReport.NET导出文件时,如何配置Acrobat PDF阅览器选项
  6. 虚拟现实游戏的十大误区
  7. 《暮光之城●破晓(下)》
  8. hnustoj 2108 湖南科技大学2019年大学生计算机程序设计新生赛
  9. 小熊派 移植TencentOS-tiny+EC20+MQTT对接腾讯云
  10. 俄语专业个人简历范文