1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 如何用Python画一只肥肥的柯基狗狗——turtle库绘制椭圆与弧线实践

如何用Python画一只肥肥的柯基狗狗——turtle库绘制椭圆与弧线实践

时间:2022-02-24 02:26:45

相关推荐

如何用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.04else:len -= 0.04fd(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.04else:len -= 0.04fd(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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。