在上一节教程中,我们已经对小猪佩奇进行了分析,现在我们就来进行绘制。
设置画布和画笔
首先,我们定义一个 setting() 函数,用它来设置画布和画笔。setting() 函数的代码如下所示。
def setting():
setup(800,500)
pensize(4)
hideturtle()
colormode(255)
speed(10)
setting() 函数主要做一些绘制前的设置和准备工作。它先将画布设置为 800 像素宽和 500 像素高的大小。然后设置画笔的大小为 4,隐藏小海龟图标。调用 colormode(255) 设置 RGB 色彩值范围为 0~255。调用 speed(10) 将画笔速度设置为 10。
nose() 函数
接下来,我们先定义绘制鼻子的函数,该函数的代码如下所示。
def nose():
penup()
goto(-100,100)
setheading(-30)
color((255,155,192),"pink") #画笔色是浅粉,填充色是粉色
pendown()
begin_fill()
#绘制一个椭圆作为鼻子的轮廓
segment=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
segment= segment+0.08 #加速
left(3) #向左转3度
forward(segment) #画直线
else:
segment= segment-0.08 #减速
left(3) #向左转3度
forward(segment) #画直线
end_fill()
#左鼻孔
penup()
setheading(90)
forward(25)
setheading(0)
forward(10)
color((255,155,192),(160,82,45)) #画笔色是浅粉,填充色是黄土赭色
pendown()
begin_fill()
circle(5)
end_fill()
#右鼻孔
penup()
setheading(0)
forward(20)
pendown()
begin_fill()
circle(5)
end_fill()
首先调用 penup() 函数拿起画笔,避免在画布上留下痕迹。然后调用 goto() 函数将画笔定位到指定坐标。调用 setheading() 设置画笔启动时运动的方向。然后调用 color() 函数,将画笔的颜色设置为浅粉色,将填充的颜色也设置为粉色,这是佩奇的标志性的颜色。
然后调用 pendown() 函数,落下画笔,现在画笔的任何移动都会留下轨迹。接下来,我们绘制了一个椭圆,绘制方法在之前已经介绍过,这里不再详述。这样鼻子的轮廓就绘制完成了。
下面我们来绘制佩奇的鼻孔。还是拿起画笔,将画笔定位到指定坐标,设置启动时运动的方向,指定画笔颜色和填充颜色,落下画笔。然后画一个圆。这样就画好了左鼻孔,右鼻孔的代码也类似,这里就不再赘述。调用这个函数,看一下绘制效果,如图 1 所示。
图 1
head() 函数
接下来我们定义 head() 函数,它用来绘制头部。head() 函数的代码如下所示。
def head():
penup()
goto(-69,167)
pendown()
color((255,155,192),"pink")
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)
segment=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
segment= segment+0.08
left(3)
forward(segment)
else:
segment= segment-0.08
left(3)
forward(segment)
end_fill()
拿起画笔,将画笔定位到指定坐标,落下画笔。指定画笔颜色和填充颜色。调用 begin_fill() 函数开始填充。设置启动时运动的方向,然后通过绘制几条弧线把头绘制出来。
接下来,又重新拿起画笔,将画笔定位到 (−100,100),这是画鼻子时的位置。然后勾画出右半个鼻子的轮廓,也就是半个椭圆。这样做是为了避免填充头的时候把鼻子覆盖掉。最后调用 end_fill() 函数结束填充。
调用这个函数,看一下其绘制效果,如图 2 所示。
图 2
ears() 函数
接下来我们定义 ears() 函数,用来绘制耳朵。ears() 函数的代码如下所示。
def ears():
color((255,155,192),"pink")
#左耳朵
penup()
goto(0,160)
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()
代码和前面类似,这里不再赘述。调用这个 ears() 函数,看一下其绘制效果,如图 3 所示。
图 3
eyes() 函数
接下来我们定义 eyes() 函数,用来绘制眼睛。eyes() 函数的代码如下所示。
def eyes():
#左眼眶
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()
setheading(90)
forward(-25)
setheading(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()
调用这个函数,看看绘制效果,如图 4 所示。
图 4