這兩天刷爆朋友圈的莫過于一則「啥是佩奇」的視頻短片环鲤,看完之后不由的感嘆一句,好久沒見過這么溫情幽默的廣告了憎兽!
作為一個python的學(xué)習(xí)者冷离,讓我萌生了用python畫社會人的想法吵冒。
看這個圖像可以發(fā)現(xiàn),佩奇由各種曲線西剥、類拋物線痹栖、類圓、類橢圓等組成瞭空。這里提到的“類”揪阿,是小豬佩奇的構(gòu)圖精髓,一種手繪風(fēng)格咆畏,而不是標(biāo)準(zhǔn)刻板的線條南捂。
思路如下:選好畫板大小,設(shè)置好畫筆顏色旧找,粗細(xì)溺健,定位好位置,依次畫鼻子钮蛛,頭矿瘦、耳朵,眼睛愿卒,腮缚去,嘴,身體琼开,手腳易结,尾巴。
環(huán)境
語言:python3.7
編輯器:Pycharm
具體代碼如下:
fromturtleimport*
defnose(x,y):#鼻子
penup()#提起筆
goto(x,y)#定位
pendown()#落筆柜候,開始畫
setheading(-30)#將烏龜?shù)姆较蛟O(shè)置為to_angle/為數(shù)字(0-東搞动、90-北、180-西渣刷、270-南)
begin_fill()#準(zhǔn)備開始填充圖形
a=0.4
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.08
left(3)#向左轉(zhuǎn)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)#返回或設(shè)置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()
defhead(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
foriinrange(60):
if0<=i<30or60<=i<90:
a=a+0.08
lt(3)#向左轉(zhuǎn)3度
fd(a)#向前走a的步長
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
defears(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()
defeyes(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()
defcheek(x,y):#腮
color((255,155,192))
penup()
goto(x,y)
pendown()
setheading(0)
begin_fill()
circle(30)
end_fill()
defmouth(x,y):#嘴
color(239,69,19)
penup()
goto(x,y)
pendown()
setheading(-80)
circle(30,40)
circle(40,80)
defbody(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()
defhands(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)
deffoot(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)
deftail(x,y):#尾巴
pensize(4)
color((255,155,192))
penup()
goto(x,y)
pendown()
seth(0)
circle(70,20)
circle(10,330)
circle(70,30)
defsetting():#參數(shù)設(shè)置
pensize(4)
hideturtle()#使烏龜無形(隱藏)
colormode(255)#將其設(shè)置為1.0或255.隨后 顏色三元組的r鹦肿,g,b值必須在0 .. cmode范圍內(nèi)
color((255,155,192),"pink")
setup(840,500)
speed(10)
defmain():
setting()#畫布辅柴、畫筆設(shè)置
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()
從代碼來看箩溃,我們可以發(fā)現(xiàn),主要用到了turtle庫碌嘀,它是python內(nèi)置的一個比較有趣味的模塊涣旨,俗稱「海龜繪圖」,它是基于tkinter模塊打造的股冗,提供了一些簡單的繪圖工具霹陡。
如果你想了解turtle庫更多信息,可以訪問官方文檔:https://docs.python.org/3/library/turtle.html.
總結(jié):通過trutle庫實(shí)現(xiàn)基本的圓、橢圓烹棉、曲線等攒霹,難點(diǎn)在于,如何定位每個部分的位置(畫之前可以先畫個草圖)浆洗。
利用turtle庫催束,我還畫過玫瑰花,作為一個python學(xué)習(xí)者辅髓,還不趕快把這么牛逼的撩妹技能學(xué)到手。后臺回復(fù)「玫瑰花」就可以獲得源碼少梁。
今天的兩個小項(xiàng)目適合入門學(xué)習(xí)者練手洛口,光跑一遍程序,就會讓你對python上癮凯沪,還等什么呢第焰?趕緊操練起來吧。