Python自带的turtle海龟绘图库功能十分强大,使用起来也很简单方便,今天我们就使用海龟绘图画一个我们都很喜欢的卡通形象-哆啦A梦头像。我们将整个头像分为几个部分分别定义相关的绘制函数,下面分别来看下:

头(head)
#头 def head(): t.pensize(8) t.fillcolor("#00A1E8") t.begin_fill() t.circle(120) t.end_fill()
head函数效果
脸(face)
def face(): t.pensize(3) t.fillcolor("white") t.begin_fill() t.circle(100) t.end_fill()
face函数效果
鼻子(nose)
def nose(): t.penup() t.home() t.goto(0, 134) t.pendown() t.pensize(4) t.fillcolor("#EA0014") t.begin_fill() t.circle(18) t.end_fill() #鼻子上的白点 t.penup() t.goto(7, 155) t.pensize(2) t.color("white", "white") t.pendown() t.begin_fill() t.circle(4) t.end_fill()
nose函数效果
眼睛(eyes)
#眼睛 def eye(x, y): t.penup() t.goto(x, y) t.setheading(0) t.pensize(4) t.pendown() t.color("black", "white") t.begin_fill() a = 0.4 for i in range(120): if (0 <= i < 30) or (60 <= i < 90): a = a+0.08 t.left(3) #向左转3度 t.forward(a) #向前走a的步长 else: a = a-0.08 t.left(3) t.forward(a) t.end_fill() #绘制眼睛 def eyes(x, y, style=0): eye(x, y) if style==0: #眨眼样式 t.penup() t.goto(-38,190) t.pensize(8) t.pendown() t.right(-30) t.forward(15) t.right(70) t.forward(15) else: #眼珠 t.penup() t.goto(23, 185) t.pensize(4) t.pendown() t.color("black", "black") t.begin_fill() t.circle(13) t.end_fill() #眼白 t.pu() t.goto(17, 190) t.pensize(2) t.pd() t.color("white", "white") t.begin_fill() t.circle(5) t.end_fill()
eyes函数效果
胡子(whiskers)
#胡子 def whiskers(iscenter, x=0, y=0, f=0, h=0): #中间胡子 if iscenter==1: t.penup() t.home() t.goto(0, 134) t.pensize(4) t.pencolor('black') t.pendown() t.right(90) t.forward(40) else: t.penup() t.home() t.setheading(h) t.goto(x, y) t.pensize(3) t.pencolor("black") t.pendown() t.forward(f)
whiskers函数效果
嘴(mouth)
#嘴 def mouth(): t.penup() t.goto(-70, 70) t.pendown() t.color("black", "red") t.pensize(6) t.setheading(-60) t.begin_fill() t.circle(80, 40) t.circle(80, 80) t.end_fill() t.penup() t.home() t.goto(-80,70) t.pendown() t.forward(160) #舌头 t.penup() t.home() t.goto(-50,50) t.pendown() t.pensize(1) t.fillcolor("#eb6e1a") t.setheading(40) t.begin_fill() t.circle(-40, 40) t.circle(-40, 40) t.setheading(40) t.circle(-40, 40) t.circle(-40, 40) t.setheading(220) t.circle(-80, 40) t.circle(-80, 40) t.end_fill()
mouth函数效果
项圈(collar)
#项圈 def collar(): t.penup() t.goto(-70, 12) t.pensize(14) t.pencolor("red") t.pendown() t.setheading(-20) t.circle(200, 40)
collar函数效果
铃铛(bell)
#铃铛 def bell(): t.penup() t.goto(0, -46) t.pendown() t.pensize(3) t.color("black", "#f8d102") t.begin_fill() t.circle(25) t.end_fill() t.penup() t.goto(-5, -40) t.pendown() t.pensize(2) t.color("black", "#79675d") t.begin_fill() t.circle(5) t.end_fill() t.pensize(3) t.right(115) t.forward(7)
调用各函数
t.speed(10) #画笔速度为10 t.hideturtle() #隐藏画笔 head() #头 face() #脸 nose() #鼻子 eyes(-30, 160) #左眼 eyes(30, 160, 1) #右眼 whiskers(1) #中间胡子 #右边胡子 whiskers(0, 0, 124, 80, 10) whiskers(0, 0, 114, 90, 0) whiskers(0, 0, 104, 80, -10) #左边胡子 whiskers(0, 0, 124, 80, 170) whiskers(0, 0, 114, 90, 180) whiskers(0, 0, 104, 80, -170) mouth() #嘴 collar() #项圈 bell() #铃铛
最终效果如下
最终效果


还没有评论,来说两句吧...