一、绘制基础图形
在Python中,我们可以使用turtle模块绘制基础图形。我们可以使用turtle.pensize()定义画笔的粗细,使用turtle.pencolor()定义画笔的颜色,使用turtle.fillcolor()定义填充颜色。
import turtle
#绘制一个实心圆
turtle.pensize(2) #定义画笔粗细
turtle.fillcolor("red") #定义填充颜色
turtle.begin_fill() #开始填充
turtle.circle(50) #绘制圆形
turtle.end_fill() #结束填充
上述代码会绘制一个实心圆,其中turtle.circle(50)表示绘制半径为50的圆形。
二、绘制人物头部
人物头部通常是一个椭圆形,我们可以使用turtle模块的ellipse()方法来绘制椭圆形。
import turtle
#绘制头部
turtle.pensize(2)
turtle.fillcolor("#FED7F2") #定义填充颜色
turtle.begin_fill()
turtle.ellipse(80,50) #绘制椭圆形
turtle.end_fill()
上述代码会绘制一个椭圆形作为人物的头部。
三、绘制人物身体
绘制人物身体通常是一个矩形,使用turtle模块的rectangle()方法可以很方便地绘制矩形。
import turtle
#绘制身体
turtle.pensize(2)
turtle.fillcolor("#FFE5B4") #定义填充颜色
turtle.begin_fill()
turtle.rectangle(100,50) #绘制矩形
turtle.end_fill()
上述代码会绘制一个矩形作为人物的身体。
四、绘制人物四肢
人物四肢通常是由直线和圆弧组成,我们可以使用turtle模块的circle()和forward()方法来绘制。
import turtle
#绘制左手
turtle.pensize(2)
turtle.penup() #抬起画笔
turtle.goto(-50,-75) #定位到左手的位置
turtle.pendown() #放下画笔
turtle.right(35) #将画笔向右旋转35度
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(40,120) #绘制左手弧形
turtle.right(110)
turtle.forward(50) #绘制左手的直线
turtle.right(110)
turtle.circle(-40,120) #绘制左手另一侧的弧形
turtle.end_fill()
#绘制右手,代码与左手类似
turtle.penup()
turtle.goto(50,-75)
turtle.pendown()
turtle.right(125)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(-40,120)
turtle.right(110)
turtle.forward(50)
turtle.right(110)
turtle.circle(40,120)
turtle.end_fill()
#绘制左脚
turtle.penup()
turtle.goto(-30,-125)
turtle.pendown()
turtle.left(90)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(30,180)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.circle(30,180)
turtle.end_fill()
#绘制右脚,代码与左脚类似
turtle.penup()
turtle.goto(30,-125)
turtle.pendown()
turtle.left(90)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(-30,180)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.circle(-30,180)
turtle.end_fill()
上述代码会绘制一个带有四肢的人物。
五、绘制人物头发和面部特征
人物头发和面部特征可以使用turtle模块的pencolor()方法来定义颜色,然后使用ellipse()方法来绘制。
import turtle
#绘制头发
turtle.pensize(2)
turtle.penup()
turtle.goto(-40,45)
turtle.pendown()
turtle.pencolor("black") #定义颜色为黑色
turtle.ellipse(30,50) #绘制头发
#绘制左眼
turtle.penup()
turtle.goto(-25,25)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(10) #绘制眼白
turtle.end_fill()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(5) #绘制眼珠
turtle.end_fill()
#绘制右眼,代码与左眼类似
turtle.penup()
turtle.goto(25,25)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
#绘制嘴巴
turtle.penup()
turtle.goto(-15,0)
turtle.pendown()
turtle.pencolor("red") #定义颜色为红色
turtle.right(90)
turtle.circle(15,180) #绘制半圆弧
上述代码会绘制出带有头发和面部特征的人物。
六、完整代码
import turtle
#绘制一个实心圆
turtle.pensize(2)
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
#绘制头部
turtle.pensize(2)
turtle.fillcolor("#FED7F2")
turtle.begin_fill()
turtle.ellipse(80,50)
turtle.end_fill()
#绘制身体
turtle.pensize(2)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.rectangle(100,50)
turtle.end_fill()
#绘制左手
turtle.pensize(2)
turtle.penup()
turtle.goto(-50,-75)
turtle.pendown()
turtle.right(35)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(40,120)
turtle.right(110)
turtle.forward(50)
turtle.right(110)
turtle.circle(-40,120)
turtle.end_fill()
#绘制右手
turtle.penup()
turtle.goto(50,-75)
turtle.pendown()
turtle.right(125)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(-40,120)
turtle.right(110)
turtle.forward(50)
turtle.right(110)
turtle.circle(40,120)
turtle.end_fill()
#绘制左脚
turtle.penup()
turtle.goto(-30,-125)
turtle.pendown()
turtle.left(90)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(30,180)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.circle(30,180)
turtle.end_fill()
#绘制右脚
turtle.penup()
turtle.goto(30,-125)
turtle.pendown()
turtle.left(90)
turtle.fillcolor("#FFE5B4")
turtle.begin_fill()
turtle.circle(-30,180)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.circle(-30,180)
turtle.end_fill()
#绘制头发
turtle.pensize(2)
turtle.penup()
turtle.goto(-40,45)
turtle.pendown()
turtle.pencolor("black")
turtle.ellipse(30,50)
#绘制左眼
turtle.penup()
turtle.goto(-25,25)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
#绘制右眼
turtle.penup()
turtle.goto(25,25)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
#绘制嘴巴
turtle.penup()
turtle.goto(-15,0)
turtle.pendown()
turtle.pencolor("red")
turtle.right(90)
turtle.circle(15,180)
turtle.done()
原创文章,作者:IHKJO,如若转载,请注明出处:https://www.506064.com/n/371542.html
微信扫一扫
支付宝扫一扫