turtle库画小老鼠
谈起turtle库大家都不陌生,但是用它来画画真的是一件很神奇的事情。想想之前只能用画笔绘画,现在却可以用代码来实现自己想要的作品。
That’s amazing!
好了,废话不多说。
今天就用python画一只新年老鼠。后期的颜色分享在下方链接。
先给大家简单介绍下Turtle库的应用吧。
turtle库的坐标体系
按照高中数学所学的象限一样,将坐标系分为四个象限,画笔想往哪个方向画就对应到哪个象限,再加上对应的角度,就可以控制自己的画笔啦。
用turtle库绘画和徒手绘画是一个道理,每次画线条之前都需要抬手然后移动到自己想要的地方再落笔。这里就要用到函数:
penup():这个函数相当于一个抬手的动作。
goto():这个函数是一个瞬间移动,移动时不绘画。
pendown():这个就是一个落笔,笔尖落在想要的位置上方。
当然,在绘画之前可以选择画布的背景颜色。
screensize(wide,long,“color”)
pensize(width):笔的粗细。
speed(num),以及绘画的速度。num越大绘制速度越快。
pencolor():笔的颜色。
在画好图形需要填充时
begin_fill():开始填充
color():设置填充颜色
end_fill():填充结束
我们还可以画一个圆:circle(radius,extent=None)
radius:弧形半径。正值:半径在画笔左侧,负值相反
extent:弧形的角度(注意!是相对角度),当值为None或空时,绘制整个圆形。
当extent不为0时,可以绘制一个圆。
在画之前要决定好自己画的位置,定好坐标,这样才更能有效率地画画。
下面是 小老鼠
的代码。
// 小老鼠.py
import turtle#导入turtle库
turtle.screensize(800,600,"#B22222")#设置画布背景颜色
turtle.pensize(2)#设置画笔宽度
turtle.pencolor("gray")
turtle.speed(60)
#画左耳朵
turtle.penup()
turtle.goto(-45,80)
turtle.pd()
turtle.begin_fill()
turtle.color("Silver")
turtle.circle(30)
turtle.end_fill()
turtle.penup()
turtle.goto(-45,85)
turtle.pd()
turtle.begin_fill()
turtle.color("#FAFFF0")
turtle.circle(25)
turtle.end_fill()
#画右耳朵
turtle.penup()
turtle.goto(45,80)
turtle.pd()
turtle.begin_fill()
turtle.color("Silver")
turtle.circle(30)
turtle.end_fill()
turtle.penup()
turtle.goto(45,85)
turtle.pd()
turtle.begin_fill()
turtle.color("#FAFFF0")
turtle.circle(25)
turtle.end_fill()
#画帽子
turtle.penup()
turtle.goto(0,90)
turtle.pd()
turtle.begin_fill()
turtle.color("#191970")
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(0,130)
turtle.pd()
turtle.begin_fill()
turtle.color("#FFFFE0")
turtle.circle(2)
turtle.end_fill()
#画圆
turtle.penup()
turtle.goto(0,0)
turtle.pd()
turtle.begin_fill()
turtle.color("Silver")
turtle.circle(55)
turtle.end_fill()
turtle.color("gray")
turtle.circle(55)
#画左眼睛
turtle.penup()
turtle.goto(-25,65)
turtle.pd()
turtle.begin_fill()
turtle.color("black")
turtle.circle(5)
turtle.end_fill()
#画右眼睛
turtle.penup()
turtle.goto(25,65)
turtle.pd()
turtle.begin_fill()
turtle.color("black")
turtle.circle(5)
turtle.end_fill()
turtle.hideturtle()
#画鼻子
turtle.penup()
turtle.goto(-9,30)
turtle.pd()
turtle.begin_fill()
turtle.color("#FAFFF0")
turtle.circle(11)
turtle.end_fill()
turtle.penup()
turtle.goto(9,30)
turtle.pd()
turtle.begin_fill()
turtle.color("#FAFFF0")
turtle.circle(11)
turtle.end_fill()
turtle.penup()
turtle.goto(0,45)
turtle.pd()
turtle.begin_fill()
turtle.color("black")
turtle.circle(5)
turtle.end_fill()
#画嘴巴
turtle.penup()
turtle.goto(0,45)
turtle.pd()
turtle.right(25)
turtle.forward(10)
turtle.penup()
turtle.goto(0,45)
turtle.pd()
turtle.left(55)
turtle.forward(-10)
turtle.color("black")
#画左胡子
turtle.penup()
turtle.goto(-45,50)
turtle.pd()
turtle.rt(40)
turtle.fd(15)
turtle.penup()
turtle.goto(-45,30)
turtle.pd()
turtle.lt(25)
turtle.fd(20)
#画右胡子
turtle.penup()
turtle.goto(45,50)
turtle.pd()
turtle.lt(180)
turtle.fd(15)
turtle.penup()
turtle.goto(45,30)
turtle.pd()
turtle.lt(150)
turtle.backward(20)
#画身体
pen=turtle.Turtle() #定义画笔实例
pen.pensize(2)#设置画笔宽度
pen.pencolor("gray")
pen.speed(50)
pen.penup()
pen.goto(0,25)
pen.pd()
a=1
pen.begin_fill()
pen.color("Silver")
#循环画椭圆
for i in range(120):
if 0<=i<30 or 60<=i<90: #控制a的变化
a=a+0.2
pen.lt(-3) #向左转3度
pen.fd(a) #向前走a的步长
else:
a=a-0.2
pen.lt(-3)
pen.fd(a)
pen.end_fill()
#画肚皮
pen=turtle.Turtle() #定义画笔实例
pen.pensize(2)#设置画笔宽度
pen.pencolor("#FAFFF0")
pen.speed(10)
pen.penup()
pen.goto(0,-18)
pen.pd()
a=1
pen.begin_fill()
pen.color("#FAFFF0")
#循环画椭圆
for i in range(120):
if 0<=i<30 or 60<=i<90: #控制a的变化
a=a+0.1
pen.lt(-3) #向左转3度
pen.fd(a) #向前走a的步长
else:
a=a-0.1
pen.lt(-3)
pen.fd(a)
pen.end_fill()
pen.hideturtle()
#画尾巴
turtle.pensize(4)
turtle.color("black")
turtle.pu()
turtle.goto(-25,-151)
turtle.pd()
turtle.circle(150, -50)
turtle.circle(60, -20)
turtle.circle(40, 30)
#画左胳膊
turtle.pensize(4)
turtle.color("darkgray")
turtle.pu()
turtle.goto(-48,-10)
turtle.pd()
turtle.circle(50,60)
#画左爪
turtle.pensize(10)
turtle.color("#FCE6C9")
turtle.fd(5)
#画右胳膊
turtle.pensize(4)
turtle.color("darkgray")
turtle.pu()
turtle.goto(48,-10)
turtle.pd()
turtle.circle(50,60)
#画右爪
turtle.pensize(10)
turtle.color("#FCE6C9")
turtle.fd(5)
#画底坐
turtle.pensize(15)
turtle.color("#FFFAF0")
turtle.pu()
turtle.goto(-50,-160)
turtle.pd()
turtle.seth(0)
turtle.forward(500)
#画脚丫
turtle.pensize(20)
turtle.color("#FCE6C9")
turtle.pu()
turtle.goto(-25,-151)
turtle.pd()
turtle.seth(0)
turtle.forward(3)
turtle.pu()
turtle.goto(25,-151)
turtle.pd()
turtle.seth(0)
turtle.forward(3)
#脸
turtle.pensize(2)
turtle.penup()
turtle.goto(0,0)
turtle.pd()
turtle.color("gray")
turtle.circle(55)
#画中国结
turtle.color("#FF0000")
turtle.pensize(3)
turtle.begin_fill()
turtle.pu()
turtle.goto(2,-30)
turtle.pd()
turtle.begin_fill()
turtle.color("#FF0000")
turtle.seth(90)
turtle.bk(10)
turtle.seth(-45)
turtle.fd(30)
turtle.seth(-135)
turtle.fd(30)
turtle.seth(-45)
turtle.bk(30)
turtle.seth(-135)
turtle.bk(30)
turtle.end_fill()
turtle.pensize(5)
turtle.pu()
turtle.goto(2,-90)
turtle.pd()
turtle.seth(90)
turtle.circle(10,20)
#画围巾
turtle.pensize(20)
turtle.color("pink")
turtle.pu()
turtle.goto(-35,6)
turtle.pd()
turtle.seth(190)
turtle.fd(5)
turtle.pu()
turtle.goto(-25,4)
turtle.pd()
turtle.color("pink")
turtle.seth(195)
turtle.fd(5)
#turtle.circle(-50, -130)
turtle.pu()
turtle.goto(-15,0)
turtle.pd()
turtle.color("pink")
turtle.seth(180)
turtle.fd(5)
turtle.pu()
turtle.goto(-5,0)
turtle.pd()
turtle.color("pink")
turtle.seth(170)
turtle.fd(5)
turtle.pu()
turtle.goto(5,0)
turtle.pd()
turtle.color("pink")
turtle.seth(160)
turtle.fd(5)
turtle.pu()
turtle.goto(15,0)
turtle.pd()
turtle.color("pink")
turtle.seth(170)
turtle.fd(5)
turtle.pu()
turtle.goto(25,0)
turtle.pd()
turtle.color("pink")
turtle.seth(180)
turtle.fd(5)
turtle.pu()
turtle.goto(35,0)
turtle.pd()
turtle.color("pink")
turtle.seth(180)
turtle.fd(5)
turtle.pu()
turtle.goto(35,-15)
turtle.pd()
turtle.color("pink")
turtle.seth(180)
turtle.fd(5)
turtle.pu()
turtle.goto(35,-25)
turtle.pd()
turtle.color("pink")
turtle.seth(180)
turtle.fd(5)
turtle.pu()
turtle.goto(35,-35)
turtle.pd()
turtle.color("#FFFFCD")
turtle.seth(180)
turtle.fd(5)
#画雪花
turtle.color('#FAFFF0')
turtle.fd_ls = [200,140,250,400,300,220,800,250,360,589,432,115,600,650,550,440]
turtle.lt_ls = [100,125,170,200,330,250,360,589,432,115,432,115,600,650,550,440,-100,-200,-333]
for i in range(15):
turtle.pu()
turtle.home()
turtle.lt(turtle.lt_ls[i])
turtle.fd(turtle.fd_ls[i])
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
turtle.fd(15)
#添加文字
turtle.pu()
turtle.goto(70,40)
turtle.pd()
turtle.color('#FFEBCD')
turtle.write('鼠年大吉 万事如意',font=("Arial",15,"normal"))
turtle.pu()
turtle.goto(-7,-72)
turtle.pd()
turtle.color('#000000')
turtle.write('福',font=("Arial",15,"normal"))
print(pen)
turtle.mainloop()
我在这里面所应用的是最简单的圆形画图,建议在画之前用坐标系确定好点的位置,这样画比较有效率。
在最后分享一个颜色盘给大家,快拿起你的画笔画出你喜欢的图案吧:
链接: https://blog.csdn.net/xiaoting451292510/article/details/8226325.