首页 > Python入门教程 > Python turtle模块
阅读数:23
Python turtle模块实例:绘制小猪佩奇(中)
接着上一教程,我们继续绘制小猪佩奇。
调用这个函数,看一下其绘制效果,如图 1 所示。

图 1
调用这个函数,看一下绘制效果,如图 2 所示。

图 2
调用这个函数,看一下绘制效果,如图 3 所示。

图 3
调用这个函数,看看绘制效果,如图 4 所示。

图 4
cheek() 函数
接下来,我们定义 cheek() 函数,它绘制一个红色的圆,用来表示腮红。cheek() 函数代码如下所示。def cheek(): penup() goto(80,10) setheading(0) color((255,155,192)) pendown() begin_fill() circle(30) end_fill()
调用这个函数,看一下其绘制效果,如图 1 所示。

图 1
mouth() 函数
接下来我们定义 mouth() 函数,就是绘制一个红色弧线,用来表示嘴巴。mouth() 函数的代码如下所示。def mouth(): penup() goto(-20,30) color(239,69,19) pendown() setheading(-80) circle(35,120)
调用这个函数,看一下绘制效果,如图 2 所示。

图 2
body() 函数
接下来,我们定义 body() 函数,用来绘制身体。body() 函数的代码如下所示。def body(): color("red",(255,99,71)) #身体左边的曲线 penup() goto(-32,-8) pendown() begin_fill() setheading(-130) circle(100,10) circle(300,30) #身体底边 setheading(0) forward(230) #身体右边的曲线 setheading(90) circle(300,30) circle(100,3) color((255,155,192),(255,100,100)) #把脸上的下巴颏画出来,避免填充时覆盖掉 setheading(-135) circle(-80,63) circle(-150,24) end_fill()
调用这个函数,看一下绘制效果,如图 3 所示。

图 3
hands() 函数
接下来,我们定义 hands() 函数用来绘制手。hands() 函数的代码如下所示。def hands(): color((255,155,192)) # 左手的中间手指 penup() goto(-56,-45) pendown() setheading(-160) circle(300,15) #通过一个弧形表示左手另外两根手指 penup() setheading(90) forward(15) setheading(0) pendown() setheading(-10) circle(-20,90) # 右手的中间手指 penup() setheading(90) forward(30) setheading(0) forward(237) pendown() setheading(-20) circle(-300,15) #通过一个弧形表示另外两根手指 penup() setheading(90) forward(20) setheading(0) pendown() setheading(-170) circle(20,90)
调用这个函数,看看绘制效果,如图 4 所示。

图 4