python,你也和小猪佩奇一样社会了!

简介: python,你也和小猪佩奇一样社会了!

还记得,曾几何时,python是一门非常强大的语言,很单纯,也很快乐,曾经python对我的唯一用途就是网络爬虫。


但是,


随着时间的推移,python变的越来越社会,他的科学计算库也崛起了,自然语言处理(NLP)的库也出来了,还有很多,TensorFlow for python,matplotylib,pillow,itchat,PyQt等等,多到数不清。


小编:python,是你社会了!


小编:我们小猪佩奇需要向你致敬!


python说:“好说,好说,那我就来画个小猪佩奇给你看看!”


小编:你还能画小猪佩奇?这么6?


python:那必须,谁让我是社会的python!


小编顿时对python又充满崇拜的眼神(尽管它社会了)


python:行吧,在此之前,请允许我介绍一下我的一个工具,可以吗?


小编:Ok,请开始你的表演!


python开始了它的装逼。


首先,python想和我们介绍的是 Turtle库。

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在

一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。


介绍完之后我们就直接上效果图了!

640.jpg


很犀利!



下面我们就来看看这个强大的库吧!



首先,安装turtle:

pip install turtle

640.png


安装结束,下面我们来接触这个库的方法了!

1. 画布(canvas)

       画布就是turtle为我们展开用于绘图区域,我们可以设置它的大小和初始位置。

       设置画布大小

        turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素), 高, 背景颜色。

       如:turtle.screensize(800,600, "green")

              turtle.screensize() #返回默认大小(400, 300)

       turtle.setup(width=0.5, height=0.75, startx=None, starty=None),参数:width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例,(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。


2. 绘图命令


        操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令。

(1)    画笔运动命令

命令

说明

turtle.forward(distance)

向当前画笔方向移动distance像素长度

turtle.backward(distance)

向当前画笔相反方向移动distance像素长度

turtle.right(degree)

顺时针移动degree°

turtle.left(degree)

逆时针移动degree°

turtle.pendown()

移动时绘制图形,缺省时也为绘制

turtle.goto(x,y)

将画笔移动到坐标为x,y的位置

turtle.penup()

提起笔移动,不绘制图形,用于另起一个地方绘制

turtle.circle()

画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

setx( )

将当前x轴移动到指定位置

sety( )

将当前y轴移动到指定位置

setheading(angle)

设置当前朝向为angle角度

home()

设置当前画笔位置为原点,朝向东。

dot(r)

绘制一个指定直径和颜色的圆点


(2)     画笔控制命令

命令

说明

turtle.fillcolor(colorstring)

绘制图形的填充颜色

turtle.color(color1, color2)

同时设置pencolor=color1, fillcolor=color2

turtle.filling()

返回当前是否在填充状态

turtle.begin_fill()

准备开始填充图形

turtle.end_fill()

填充完成

turtle.hideturtle()

隐藏画笔的turtle形状

turtle.showturtle()

显示画笔的turtle形状



(3)    全局控制命令

命令

说明

turtle.clear()

清空turtle窗口,但是turtle的位置和状态不会改变

turtle.reset()

清空窗口,重置turtle状态为起始状态

turtle.undo()

撤销上一个turtle动作

turtle.isvisible()

返回当前turtle是否可见

stamp()

复制当前图形

turtle.write(s [,font=("font-name",font_size,"font_type")])

写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项


(4)    其他命令

命令

说明

turtle.mainloop()或turtle.done()

启动事件循环 -调用Tkintermainloop函数。

必须是乌龟图形程序中的最后一个语句。

turtle.mode(mode=None)

设置乌龟模式(“standard”“logo”“world”)并执行重置。如果没有给出模式,则返回当前模式。

turtle.delay(delay=None)

设置或返回以毫秒为单位的绘图延迟。

turtle.begin_poly()

开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。

turtle.end_poly()

停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。

turtle.get_poly()

返回最后记录的多边形。


到现在,我们已经把turtle模块的大部分一些画图用到方法都列举出来了,既然有了方法,咱们就可以开始画图了!


我们每画一个部位,就把这个部位封装在一个函数中,然后调用即可,首先我们来画鼻子:

def nose(x,y):#鼻子
    t.pu()
    t.goto(x,y)
    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()

看效果:

640.png


猪鼻子画完了我们现在开始画社会人的头!

def head(x,y):#头
    t.color((255,155,192),"pink")
    t.pu()
    t.goto(x,y)
    t.seth(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()

查看效果:

640.png


下面我们来画耳朵:

def ears(x,y): #耳朵
    t.color((255,155,192),"pink")
    t.pu()
    t.goto(x,y)
    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()


耳朵还是比较简单的,接下来我们来画眼睛:

def eyes(x,y):#眼睛
    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()

看看效果:

640.png

感觉还不错


下面我们来画腮红和嘴巴:

def cheek(x,y):#腮
    t.color((255,155,192))
    t.pu()
    t.goto(x,y)
    t.pd()
    t.seth(0)
    t.begin_fill()
    t.circle(30)
    t.end_fill()
def mouth(x,y): #嘴
    t.color(239,69,19)
    t.pu()
    t.goto(x,y)
    t. pd()
    t.seth(-80)
    t.circle(30,40)
    t.circle(40,80)

很简单,就是两个圆和一条线:


640.jpg

下面我们把身体画出来:


def body(x,y):#身体
    t.color("red",(255,99,71))
    t.pu()
    t.goto(x,y)
    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()
def hands(x,y):#手
    t.color((255,155,192))
    t.pu()
    t.goto(x,y)
    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)
def foot(x,y):#脚
    t.pensize(10)
    t.color((240,128,128))
    t.pu()
    t.goto(x,y)
    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)

顺便把手和脚一步到位,

最后还有个小尾巴:

def tail(x,y):#尾巴
    t.pensize(4)
    t.color((255,155,192))
    t.pu()
    t.goto(x,y)
    t.pd()
    t.seth(0)
    t.circle(70,20)
    t.circle(10,330)
    t.circle(70,30)


画完这些,我们的小猪佩奇就算画完了,就是最初的效果了


640.jpg




Ok,我们的python已经搞定社会人了!

相关文章
|
4月前
|
人工智能 供应链 数据可视化
【python】python国内社会消费品零售总额数据分析(代码+数据+报告)【独一无二】
【python】python国内社会消费品零售总额数据分析(代码+数据+报告)【独一无二】
【python】python国内社会消费品零售总额数据分析(代码+数据+报告)【独一无二】
|
11月前
|
Python
编程新手?跟着这个教程,用Python画出小猪佩奇
编程新手?跟着这个教程,用Python画出小猪佩奇
176 1
编程新手?跟着这个教程,用Python画出小猪佩奇
|
Python
哆啦A梦和小猪佩奇(Python实现)
哆啦A梦和小猪佩奇(Python实现)
218 0
|
Python
Python画樱花树代码和小猪佩奇
Python画樱花树代码和小猪佩奇
|
移动开发 Python
Python边做边学︱小猪佩奇游戏
学习 Python ,不仅可以在玩游戏中增长知识,还可以保持一颗童心未泯的心。小猪佩奇、哆啦A 梦、小蜜蜂、一休等动画片相信很多读者都看过,可以用 Python 描绘出动画片中的主角,甚至可以制作一张个人的素描画。
4675 0
|
程序员 Python
啥是佩奇,让程序员用python来告诉你,哈哈
啥是佩奇,让程序员用python来告诉你,哈哈
啥是佩奇,让程序员用python来告诉你,哈哈
小猪佩奇的4种python玩法,带你趣味学python!(二)
小猪佩奇的4种python玩法,带你趣味学python!(二)
小猪佩奇的4种python玩法,带你趣味学python!(二)
小猪佩奇的4种python玩法,带你趣味学python!(一)
小猪佩奇的4种python玩法,带你趣味学python!(一)
小猪佩奇的4种python玩法,带你趣味学python!(一)
|
Android开发
Python爬虫入门教程 45-100 Charles抓取兔儿故事-下载小猪佩奇故事-手机APP爬虫部分
1. Charles抓取兔儿故事背景介绍 之前已经安装了Charles,接下来我将用两篇博客简单写一下关于Charles的使用,今天抓取一下兔儿故事里面关于小猪佩奇的故事。爬虫编写起来核心的重点是分析到链接,只要把链接分析到,剩下的就好办了。
1242 0
|
4天前
|
数据采集 机器学习/深度学习 人工智能
Python编程入门:从零基础到实战应用
【9月更文挑战第15天】本文将引导读者从零开始学习Python编程,通过简单易懂的语言和实例,帮助初学者掌握Python的基本语法和常用库,最终实现一个简单的实战项目。文章结构清晰,分为基础知识、进阶技巧和实战应用三个部分,逐步深入,让读者在学习过程中不断积累经验,提高编程能力。