Python turtle模块实战:绘制机器猫的完整代码

你的程序是否编译成功,下面是绘制机器猫的完整代码,如果出现错误,请及时与正确代码进行对照修改:

机器猫的完整代码:

from turtle import *

def drawRound(size,filled):         #绘制圆形
    pendown()        
    if filled==True:  #判断是否填充颜色
        begin_fill() 
    setheading(180)  
    circle(size,360) 
    if filled==True: 
        end_fill()   

def drawRect(length,width,filled):  #绘制矩形
    setheading(0)    
    pendown()        
    if filled==True: 
        begin_fill() 
    forward(length)  
    right(90)        
    forward(width)   
    right(90)        
    forward(length)  
    right(90)        
    forward(width)   
    if filled==True: 
        end_fill()  
   
def head():     #头
    #画个蓝色的大圆
    color("blue","blue")
    penup()
    goto(0,100)
    drawRound(75,True)
    #画个白色的小圆
    color("white","white")
    penup()
    goto(0,72)
    drawRound(60,True)
   
def eyes():     #眼睛
    #左眼眶
    color("black","white")
    penup()
    goto(-15,80)
    drawRound(17,True)
    #右眼眶
    color("black","white")
    penup()
    goto(19,80)
    drawRound(17,True)
    #左眼珠
    color("black","black")
    penup()
    goto(-8,70)
    drawRound(6,True)   
    color("white","white")
    penup()
    goto(-8,66)
    drawRound(2,True)
    #右眼珠
    color("black","black")
    penup()
    goto(12,70)
    drawRound(6,True)   
    color("white","white")
    penup()
    goto(12,66)
    drawRound(2,True)   
   
def nose():     #鼻子
    color("red","red")
    penup()
    goto(0,40)
    drawRound(7,True)
  
def mouth():    #嘴巴
    #嘴
    color("black","black")
    penup()
    goto(-30,-20)
    pendown()
    setheading(-27)
    circle(70,55)

    #人中
    penup()
    goto(0,26)    
    pendown()
    goto(0,-25)

def whiskers():     #胡须   
    color("black","black")
    #左边中间的胡子
    penup()
    goto(10,5)    
    pendown()
    goto(-40,5)
    #右边中间的胡子
    penup()
    goto(10,5)    
    pendown()
    goto(40,5)
    #左上的胡子
    penup()
    goto(-10,15)    
    pendown()
    goto(-40,20)
    #右上的胡子
    penup()
    goto(10,15)    
    pendown()
    goto(40,20)
    #左下的胡子
    penup()
    goto(-10,-5)    
    pendown()
    goto(-40,-10)
    #右下的胡子
    penup()
    goto(10,-5)    
    pendown()
    goto(40,-10)
   
def body():         #身体
    #蓝色的身体
    color("blue","blue")
    penup()
    goto(-50,-40)
    drawRect(100,80,True)
  
    #白色的大肚子
    color("white","white")
    penup()
    goto(0,-30)
    drawRound(40,True)
    #红色丝带
    color("red","red")
    penup()
    goto(-60,-35)
    drawRect(120,10,True)
    #白色的腿
    color("white","white")
    penup()
    goto(15,-127)
    pendown()
    begin_fill()
    setheading(90)
    circle(14,180)
    end_fill()
   
def feet():             #脚
    #左脚
    color("black","white")
    penup()
    goto(-30,-110)
    drawRound(20,True)  
    #右脚
    color("black","white")
    penup()
    goto(30,-110)
    drawRound(20,True)
def arms():             #胳膊
    #左胳膊
    color("blue","blue")
    penup()
    begin_fill()
    goto(-51,-50)    
    pendown()
    goto(-51,-75)
    left(70)
    goto(-76,-85)
    left(70)
    goto(-86,-70)
    left(70)
    goto(-51,-50)
    end_fill()
    #右胳膊
    color("blue","blue")
    penup()
    begin_fill()
    goto(49,-50)    
    pendown()
    goto(49,-75)
    left(70)
    goto(74,-85)
    left(70)
    goto(84,-70)
    left(70)
    goto(49,-50)
    end_fill()
   
def hands():            #手
    #左手
    color("black","white")
    penup()
    goto(-90,-71)
    drawRound(15,True)
    #右手
    color("black","white")
    penup()
    goto(90,-71)
    drawRound(15,True)
   
def bell():             #铃铛
    #黄色实心圆表示铜铃
    color("yellow","yellow")
    penup()
    goto(0,-41)
    drawRound(8,True)
    #黑色矩形表示花纹
    color("black","black")
    penup()
    goto(-10,-47)    
    drawRect(20,4,False)
    #黑色实心圆表示撞击的金属丸
    color("black","black")
    penup()
    goto(0,-53)
    drawRound(2,True)
   
def package():          #口袋
    #半圆
    color("black","black")
    penup()
    goto(-25,-70)
    pendown()
    setheading(-90)
    circle(25,180)
    goto(-25,-70) 
    hideturtle()

#设置窗口大小
setup(500,500)
#设置画笔
speed(10)      
#shape("turtle")
colormode(255) 
#绘制机器猫
head()              #头
eyes()              #眼睛
nose()              #鼻子
mouth()             #嘴
whiskers()          #胡子
body()              #身体
feet()              #脚
arms()              #胳膊
hands()             #手
bell()              #铃铛
package()           #口袋