python,你也和小豬佩奇一樣社會了!

  • 2019 年 10 月 6 日
  • 筆記

還記得,曾幾何時,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)位置開始,它根據一組函數指令的控制,在這個平面坐標系中移動,從而在它爬行的路徑上繪製了圖形。

介紹完之後我們就直接上效果圖了!

很犀利!

下面我們就來看看這個強大的庫吧!

首先,安裝turtle:

pip install turtle

安裝結束,下面我們來接觸這個庫的方法了!

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()

啟動事件循環 -調用Tkinter的mainloop函數。必須是烏龜圖形程序中的最後一個語句。

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()

看效果:

豬鼻子畫完了我們現在開始畫社會人的頭!

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()

查看效果:

下面我們來畫耳朵:

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()

看看效果:

感覺還不錯

下面我們來畫腮紅和嘴巴:

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)

很簡單,就是兩個圓和一條線:

下面我們把身體畫出來:

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)

畫完這些,我們的小豬佩奇就算畫完了,就是最初的效果了

Ok,我們的python已經搞定社會人了!