本次项目主要绘制各种水果
(附带代码运行结果及详细讲解)
一、苹果
代码讲解
导入所需要的模块
import turtle as t
设置画笔参数
t.pencolor("black")
t.fillcolor("red")
t.pensize(5)
绘制苹果
t.begin_fill()
t.circle(-100)
t.end_fill()
绘制上方的茎
t.pu()
t.goto(-30,-60)
t.setheading(-60)
t.pd()
t.pensize(8)
t.circle(30,120)
t.circle(30,-60)
t.left(90)
t.pensize(5)
t.circle(-340,30)
隐藏画笔并保持窗口显示
t.hideturtle()
t.done()
最终代码
import turtle as t
t.pencolor("black")
t.fillcolor("red")
t.pensize(5)
t.begin_fill()
t.circle(-100)
t.end_fill()
t.pu()
t.goto(-30,-60)
t.setheading(-60)
t.pd()
t.pensize(8)
t.circle(30,120)
t.circle(30,-60)
t.left(90)
t.pensize(5)
t.circle(-340,30)
t.hideturtle()
t.done()
二、葡萄
代码讲解
导入所需要的模块
import turtle as t
设置画笔参数
t.pencolor("black")
t.pensize(5)
t.speed(0)
绘制第一片叶子的外形
t.fillcolor("green")
t.begin_fill()
t.circle(80,150)
t.left(30)
t.circle(80,150)
t.end_fill()
绘制第一片叶子中间的纹路
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-160,45)
回到原点并做好绘制第二片叶子的准备
t.pu()
t.home()
t.right(90)
t.pd()
画第二片叶子的外轮廓
t.begin_fill()
t.circle(80,150)
t.left(30)
t.circle(80,150)
t.end_fill()
绘制第二片叶子的纹路
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-160,45)
定义画葡萄的函数
def grape(x,y):
t.pu()
t.goto(x,y)
t.pd()
t.begin_fill()
t.circle(40)
t.end_fill()
绘制第一层(最靠近叶子)的6个葡萄
t.fillcolor("purple")
t.pu()
t.goto(-80,50)
for i in range(6):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
绘制第二层5个葡萄
t.pu()
t.goto(-110,0)
t.setheading(180)
for i in range(5):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
绘制第三层4个葡萄
t.pu()
t.goto(-140,-30)
t.setheading(180)
for i in range(4):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
绘制第四层3个葡萄
t.pu()
t.goto(-170,-60)
t.setheading(180)
for i in range(3):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
绘制第五层2个葡萄
t.pu()
t.goto(-200,-90)
t.setheading(180)
for i in range(2):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
绘制最后一层1个葡萄
t.pu()
t.goto(-230,-120)
t.setheading(180)
grape(t.pos()[0],t.pos()[1])
隐藏画笔并保持窗口显示
t.hideturtle()
t.done()
最终代码:
import turtle as t
t.pencolor("black")
t.pensize(5)
t.speed(0)
t.fillcolor("green")
t.begin_fill()
t.circle(80,150)
t.left(30)
t.circle(80,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-160,45)
t.pu()
t.home()
t.right(90)
t.pd()
t.begin_fill()
t.circle(80,150)
t.left(30)
t.circle(80,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-160,45)
t.pu()
t.home()
t.setheading(180)
def grape(x,y):
t.pu()
t.goto(x,y)
t.pd()
t.begin_fill()
t.circle(40)
t.end_fill()
t.fillcolor("purple")
t.pu()
t.goto(-80,50)
for i in range(6):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
t.pu()
t.goto(-110,0)
t.setheading(180)
for i in range(5):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
t.pu()
t.goto(-140,-30)
t.setheading(180)
for i in range(4):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
t.pu()
t.goto(-170,-60)
t.setheading(180)
for i in range(3):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
t.pu()
t.goto(-200,-90)
t.setheading(180)
for i in range(2):
grape(t.pos()[0],t.pos()[1])
t.pu()
t.circle(180,-20)
t.pd()
t.pu()
t.goto(-230,-120)
t.setheading(180)
grape(t.pos()[0],t.pos()[1])
t.hideturtle()
t.done()
三、草莓
代码讲解
导入所需要的模块
import turtle as t
设置画笔初始参数
t.pensize(5)
t.pencolor("black")
t.speed(0)
设置叶子的参数
t.pu()
t.goto(10,-40)
t.fillcolor("green")
t.pd()
像画葡萄的代码一样,改一点大小,不做详细讲解
t.begin_fill()
t.left(90)
t.circle(40,150)
t.left(30)
t.circle(40,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-80,45)
t.pu()
t.goto(10,-40)
t.seth(-30)
t.pd()
t.begin_fill()
t.circle(40,150)
t.left(30)
t.circle(40,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-80,45)
设置草莓初始参数
t.pu()
t.seth(60)
t.goto(-50,-70)
t.pd()
绘制草莓
t.fillcolor("red")
t.begin_fill()
t.circle(-80,120)
t.goto(35,t.pos()[1]-100)
t.goto(-50,-70)
t.end_fill()
绘制草莓上的籽儿
t.pu()
t.goto(-10,-85)
t.dot(10)
t.goto(5,-75)
t.dot(10)
t.goto(25,-95)
t.dot(10)
t.goto(15,-105)
t.dot(10)
t.goto(35,-115)
t.dot(10)
t.goto(55,-75)
t.dot(10)
隐藏画笔并保持窗口显示
t.hideturtle()
t.done()
最终代码
import turtle as t
t.pensize(5)
t.pencolor("black")
t.speed(0)
t.pu()
t.goto(10,-40)
t.fillcolor("green")
t.pd()
t.begin_fill()
t.left(90)
t.circle(40,150)
t.left(30)
t.circle(40,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-80,45)
t.pu()
t.goto(10,-40)
t.seth(-30)
t.pd()
t.begin_fill()
t.circle(40,150)
t.left(30)
t.circle(40,150)
t.end_fill()
t.left(120)
t.pu()
t.fd(10)
t.pd()
t.circle(-80,45)
t.pu()
t.seth(60)
t.goto(-50,-70)
t.pd()
t.fillcolor("red")
t.begin_fill()
t.circle(-80,120)
t.goto(35,t.pos()[1]-100)
t.goto(-50,-70)
t.end_fill()
t.pu()
t.goto(-10,-85)
t.dot(10)
t.goto(5,-75)
t.dot(10)
t.goto(25,-95)
t.dot(10)
t.goto(15,-105)
t.dot(10)
t.goto(35,-115)
t.dot(10)
t.goto(55,-75)
t.dot(10)
t.hideturtle()
t.done()
本次详细讲解了三种水果的绘制方法,之后还会继续更新更多内容
关注我,订阅免费的Turtle画图专栏,查看更多的turtle画图教学吧!