在微信上看到一個(gè)未完成的小豬佩奇,使用 python 內(nèi)置的 turtle 繪制惶楼,代碼是我整理完善的轮蜕,直接獻(xiàn)上。
# coding:utf-8
import turtle as tur
def nose(x,y):#鼻子
tur.penup()#提起筆
tur.goto(x,y)#定位
tur.pendown()#落筆收壕,開始畫
tur.setheading(-30)#將烏龜?shù)姆较蛟O(shè)置為to_angle/為數(shù)字(0-東妓灌、90-北、180-西蜜宪、270-南)
tur.begin_fill()#準(zhǔn)備開始填充圖形
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
tur.left(3) #向左轉(zhuǎn)3度
tur.forward(a) #向前走a的步長
else:
a=a-0.08
tur.left(3)
tur.forward(a)
tur.end_fill()#填充完成
tur.penup()
tur.setheading(90)
tur.forward(25)
tur.setheading(0)
tur.forward(10)
tur.pendown()
tur.pencolor(255,155,192)#畫筆顏色
tur.setheading(10)
tur.begin_fill()
tur.circle(5)
tur.color(160,82,45)#返回或設(shè)置pencolor和fillcolor
tur.end_fill()
tur.penup()
tur.setheading(0)
tur.forward(20)
tur.pendown()
tur.pencolor(255,155,192)
tur.setheading(10)
tur.begin_fill()
tur.circle(5)
tur.color(160,82,45)
tur.end_fill()
def head(x,y):#頭
tur.color((255,155,192),"pink")
tur.penup()
tur.goto(x,y)
tur.setheading(0)
tur.pendown()
tur.begin_fill()
tur.setheading(180)
tur.circle(300,-30)
tur.circle(100,-60)
tur.circle(80,-100)
tur.circle(150,-20)
tur.circle(60,-95)
tur.setheading(161)
tur.circle(-300,15)
tur.penup()
tur.goto(-100,100)
tur.pendown()
tur.setheading(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
tur.lt(3) #向左轉(zhuǎn)3度
tur.forward(a) #向前走a的步長
else:
a=a-0.08
tur.lt(3)
tur.forward(a)
tur.end_fill()
def ears(x,y): #耳朵
tur.color((255,155,192),"pink")
tur.penup()
tur.goto(x,y)
tur.pendown()
tur.begin_fill()
tur.setheading(100)
tur.circle(-50,50)
tur.circle(-10,120)
tur.circle(-50,54)
tur.end_fill()
tur.penup()
tur.setheading(90)
tur.forward(-12)
tur.setheading(0)
tur.forward(30)
tur.pendown()
tur.begin_fill()
tur.setheading(100)
tur.circle(-50,50)
tur.circle(-10,120)
tur.circle(-50,56)
tur.end_fill()
def eyes(x,y):#眼睛
tur.color((255,155,192),"white")
tur.penup()
tur.setheading(90)
tur.forward(-20)
tur.setheading(0)
tur.forward(-95)
tur.pendown()
tur.begin_fill()
tur.circle(15)
tur.end_fill()
tur.color("black")
tur.penup()
tur.setheading(90)
tur.forward(12)
tur.setheading(0)
tur.forward(-3)
tur.pendown()
tur.begin_fill()
tur.circle(3)
tur.end_fill()
tur.color((255,155,192),"white")
tur.penup()
tur.seth(90)
tur.forward(-25)
tur.seth(0)
tur.forward(40)
tur.pendown()
tur.begin_fill()
tur.circle(15)
tur.end_fill()
tur.color("black")
tur.penup()
tur.setheading(90)
tur.forward(12)
tur.setheading(0)
tur.forward(-3)
tur.pendown()
tur.begin_fill()
tur.circle(3)
tur.end_fill()
def cheek(x,y):#腮
tur.color((255,155,192))
tur.penup()
tur.goto(x,y)
tur.pendown()
tur.setheading(0)
tur.begin_fill()
tur.circle(30)
tur.end_fill()
def mouth(x,y): #嘴
tur.color(239,69,19)
tur.penup()
tur.goto(x,y)
tur.pendown()
tur.setheading(-80)
tur.circle(30,40)
tur.circle(40,80)
def body(x,y): #身子
tur. color("red", (255, 99, 71))
tur. penup()
tur. goto(x,y)
tur. pendown()
tur. begin_fill()
tur. seth(-130)
tur. circle(100, 10)
tur. circle(300, 30)
tur. seth(0)
tur. forward(230)
tur. seth(90)
tur. circle(300, 30)
tur. circle(100, 3)
tur. color((255, 155, 192), (255, 100, 100))
tur. seth(-135)
tur. circle(-80, 63)
tur. circle(-150, 24)
tur. end_fill()
def hands(x,y): #手
tur. color((255, 155, 192))
tur. penup()
tur. seth(90)
tur. forward(-40)
tur. seth(0)
tur. forward(-27)
tur. pendown()
tur. seth(-160)
tur. circle(300, 15)
tur. penup()
tur. seth(90)
tur. forward(15)
tur. seth(0)
tur. forward(0)
tur. pendown()
tur. seth(-10)
tur. circle(-20, 90)
tur. penup()
tur. seth(90)
tur. forward(30)
tur. seth(0)
tur. forward(237)
tur. pendown()
tur. seth(-20)
tur. circle(-300, 15)
tur. penup()
tur. seth(90)
tur. forward(20)
tur. seth(0)
tur. forward(0)
tur. pendown()
tur. seth(-170)
tur. circle(20, 90)
def feet(x,y): #腳
tur. pensize(10)
tur. color((240, 128, 128))
tur. penup()
tur. seth(90)
tur. forward(-75)
tur. seth(0)
tur. forward(-180)
tur. pendown()
tur. seth(-90)
tur. forward(40)
tur. seth(-180)
tur. color("black")
tur. pensize(15)
tur. forward(20)
tur. pensize(10)
tur. color((240, 128, 128))
tur. penup()
tur. seth(90)
tur. forward(40)
tur. seth(0)
tur. forward(90)
tur. pendown()
tur. seth(-90)
tur. forward(40)
tur. seth(-180)
tur. color("black")
tur. pensize(15)
tur. forward(20)
def tail(x,y): #尾巴
tur. pensize(4)
tur. color((255, 155, 192))
tur. penup()
tur. seth(90)
tur. forward(70)
tur. seth(0)
tur. forward(95)
tur. pendown()
tur. seth(0)
tur. circle(70, 20)
tur. circle(10, 330)
tur. circle(70, 30)
def setting(): #參數(shù)設(shè)置
tur.pensize(4)
tur.hideturtle() #使烏龜無形(隱藏)
tur.colormode(255) #將其設(shè)置為1.0或255.隨后 顏色三元組的r虫埂,g,b值必須在0 .. cmode范圍內(nèi)
tur.color((255,155,192),"pink")
tur.setup(840,500)
tur.speed(10)
def main():
setting() #畫布圃验、畫筆設(shè)置
nose(-100,100) #鼻子
head(-69,167) #頭
ears(0,160) #耳朵
eyes(0,0) #眼睛
cheek(80,10) #腮
mouth(-20,30) #嘴
body(-32,-8)
hands(0,0)
feet(0,0)
tail(0,0)
tur.done()
if __name__ == '__main__':
main()