python pos函数_如何用Python画一只肥肥的柯基狗狗—turtle库绘制椭圆与弧线实践
生活随笔
收集整理的這篇文章主要介紹了
python pos函数_如何用Python画一只肥肥的柯基狗狗—turtle库绘制椭圆与弧线实践
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
歷時4天,利用工作之余的細碎時間, 修修改改,終于把這只丑萌的小鼓臉柯基畫了出來,我也有狗啦~code的過程多坎坷,完成時就有多快樂!成果如下:
- 初學turtle時所畫的這只柯基,由于對turtle中靈活強大的circle()函數理解和應用不夠自如,???????自定義的畫弧函數化簡為繁了。
- 繪制對象的關鍵坐標點確實需要反復嘗試,在繪制這樣一幅turtle畫作時,耐心是遠比技術更被需要的。
繪制柯基時主要用到了以下幾種幾何圖形: 橢圓——柯基眼部(定義函數會更方便,這里可以進一步完善) 弧線——柯基耳部、鼻部、面部。
(利用"一步一拐曲線繪制法"定義畫弧函數) 圓——柯基嘴部白圈(直接利用turtle的circle() 函數) 矩形——柯基眼睛白光塊(定義函數) 等邊三角形——柯基鼻頭(定義函數) 感興趣的小伙伴請自行了解一下代碼:
from turtle import *pensize(5)speed(0)##color('#F4A460')#橘黃##color('#FFE4E1')#肉粉 ##【背景圓】color('#B088FF')#淺紫pu()goto(0,-200)pd()begin_fill()circle(200)end_fill() ##定義畫弧函數def Arc(initial_degree,range_num,step,rotate_degree): seth(initial_degree) for n in range(range_num): fd(step) rt(rotate_degree)# ##定義填充矩形函數def Rect(x,y,height,width): pu() goto(x,y) pd() begin_fill() goto(x+width,y) goto(x+width,y+height) goto(x,y+height) goto(x,y) end_fill() ##定義繪制填充等邊三角形函數 def Triangle(x,y,side_length):#等邊三角形底邊左角 pu() goto(x,y) pd() begin_fill() seth(0) fd(side_length) rt(120)#lt()是正立三角形 fd(side_length) rt(120)#lt()是正立三角形 fd(side_length) end_fill() #中軸線——輔助繪圖線#color("green")#Rect(-200,0,1,400)#x軸#Rect(0,-200,400,1)#y軸 ##【圖層1——面部輪廓】color('#F4A460')#橘黃#左耳pu()goto(-83.13,-10.94)pd()begin_fill() Arc(120,145,1,1/4)goto(-30,50)end_fill() #右耳pu()goto(83.13,-10.94)#(88.13,10.94)pd()begin_fill()Arc(60,145,1,-1/4)goto(30,50)end_fill() #腮幫#右腮幫pu()goto(83.13,-10.94)#0pd()begin_fill()Arc(-35,135,1,9/11)#1#print(pos()) #下巴#pencolor("yellow")Arc(-145,70,1,3/10)#右半下頜2#print(pos()) #pencolor("red")Arc(-175,40,1,1/5)#下巴連接線3#print(pos()) #pencolor("pink")Arc(168,70,1,3/10)#左半下頜4#print(pos()) #左腮幫#pencolor("grey")Arc(146,135,1,9/11)#5#print(pos()) #兩耳連接pu()goto(-30,50)Arc(15,80,1,1/2) end_fill() ##【圖層2——耳部輪廓】color('pink')#FFC0CB#左耳pu()goto(-42,50)pd()begin_fill()Arc(-164,55,1,-7/8)Arc(120,100,1,1/3)goto(-42,50)end_fill() #右耳pu()goto(42,50)pd()begin_fill()Arc(-16,55,1,7/8)#(81.13,15.94)#print(pos())Arc(60,100,1,-1/3)#(104.15,111.82)#print(pos())goto(42,50)end_fill() ##【圖層3——眼部輪廓】#左黑眼豆豆pu()goto(-46,-8)pd()color("black")seth(180)len = 0.3begin_fill()for k in range(2): # 雙弧繪制橢圓 for j in range(60): if j < 30: len += 0.04 else: len -= 0.04 fd(len) lt(3)end_fill()#左眼白光color("white")Rect(-43,-38,6,2) #右黑眼豆豆pu()goto(46,-8)pd()color("black")seth(180)len = 0.3begin_fill()for k in range(2): # 將相同的動作重復做一遍 for j in range(60): if j < 30: len += 0.04 else: len -= 0.04 fd(len) lt(3)end_fill()#右眼白光color("white")Rect(40,-38,6,2) ##【圖層4——白鼻子輪廓】pu()goto(10,50)pd()goto(-10,50)color("white")begin_fill()Arc(-82,140,1,1/7)#結束角度A=-82-140*1/7=-102Arc(-112,20,1.1,-1.2)#結束角度B=-112+20*1.2=-88#setx(-xcor())goto(-xcor(),ycor())sethArc(88,20,1.1,-1.2)#求A的y軸對稱角度Arc(102,140,1,1/7)#求8的y軸對稱角度goto(10,50)end_fill()pd() #圓嘴pu()goto(0,-150)seth(0)pd()begin_fill()circle(35)end_fill() #黑鼻頭color("black")Triangle(-10,-120,20) end_fill() hideturtle()done()完善后的代碼已打包成python教程,更多內容及素材、源碼、關注公眾號python社區營
總結
以上是生活随笔為你收集整理的python pos函数_如何用Python画一只肥肥的柯基狗狗—turtle库绘制椭圆与弧线实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测不准原理主要指向微观
- 下一篇: 虚幻引擎3的代码讲解