使用python畫個(gè)小豬佩奇的示例代碼
基本原理
選好畫板大小,設(shè)置好畫筆顏色、粗細(xì),定位好位置,依次畫鼻子、頭、耳朵、眼睛、腮、嘴、身體、手腳、尾巴,完事兒。
都知道,Turtle 是 Python 內(nèi)置的一個(gè)比較有趣味的模塊,俗稱“海龜繪圖”,它是基于 Tkinter 模塊打造,提供一些簡單的繪圖工具。
在海龜作圖中,我們可以編寫指令讓一個(gè)虛擬的(想象中的)海龜在屏幕上來回移動(dòng)。這個(gè)海龜帶著一只鋼筆,我們可以讓海龜無論移動(dòng)到哪都使用這只鋼筆來繪制線條。通過編寫代碼,以各種很酷的模式移動(dòng)海龜,我們可以繪制出令人驚奇的圖片。使用海龜作圖,我們不僅能夠只用幾行代碼就創(chuàng)建出令人印象深刻的視覺效果,而且還可以跟隨海龜看看每行代碼如何影響到它的移動(dòng)。這能夠幫助我們理解代碼的邏輯。
所以,海龜作圖也常被用作新手學(xué)習(xí) Python 的一種方式。更豐富詳細(xì)的功能及知識(shí)可以參考官方文檔:
https://docs.python.org/3/library/turtle.html
from turtle import*
def nose(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
for i in range(120):
if 0<=i<30 or 60<=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()
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) #向左轉(zhuǎn)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(): #參數(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)
def main():
setting() #畫布、畫筆設(shè)置
nose(-100,100) #鼻子
head(-69,167) #頭
ears(0,160) #耳朵
eyes(0,140) #眼睛
cheek(80,10) #腮
mouth(-20,30) #嘴
done()
if __name__ == '__main__':
main()
總結(jié)
以上所述是小編給大家介紹的使用python畫個(gè)小豬佩奇的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python?Pipeline處理數(shù)據(jù)工作原理探究
如果你是一個(gè)Python開發(fā)者,你可能聽過"pipeline"這個(gè)術(shù)語,但?pipeline?到底是什么,它又有什么用呢?在這篇文章中,我們將探討?Python?中的?pipeline?概念,它們是如何工作的,以及它們?nèi)绾螏椭憔帉懜逦?、更高效的代碼2024-01-01
Python在終端通過pip安裝好包以后在Pycharm中依然無法使用的問題(三種解決方案)
這篇文章主要介紹了Python在終端通過pip安裝好包以后在Pycharm中依然無法使用的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Django admin.py 在修改/添加表單界面顯示額外字段的方法
今天小編就為大家分享一篇Django admin.py 在修改/添加表單界面顯示額外字段的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python中urllib.request和requests的使用及區(qū)別詳解
這篇文章主要介紹了python中urllib.request和requests的使用及區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

