Python Turtle 小项目 4

本次项目主要绘制各种水果

(附带代码运行结果及详细讲解)


一、苹果

代码讲解

导入所需要的模块

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画图教学吧!

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值