Python Turtle 小项目 4

本文详细介绍了使用Python的turtle库绘制苹果、葡萄和草莓的步骤,通过实例展示了如何运用turtle进行图形绘制,适合初学者了解基本图形编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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


一、苹果

代码讲解

导入所需要的模块

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

### Python Turtle库项示例与教程 #### 使用Turtle库绘制复杂图形 Turtle库作为Python标准库的一部分,提供了强大的功能来实现各种复杂的图形绘制。以下是几个常见的项示例及其代码。 --- #### 示例1:自动画树 此项展示了如何利用递归方法绘制一棵分形树。通过调整参数可以改变树木的形状和大小。 ```python from turtle import * colormode(255) lt(90) lv = 14 l = 120 s = 45 width(lv) r = 0 g = 0 b = 0 pencolor(r, g, b) penup() bk(l) pendown() fd(l) def draw_tree(length, level): global r, g, b w = width() width(w * 3.0 / 4.0) r += 1 g += 2 b += 3 pencolor(r % 200, g % 200, b % 200) length *= 0.75 lt(s) fd(length) if level < lv: draw_tree(length, level + 1) bk(length) rt(2 * s) fd(length) if level < lv: draw_tree(length, level + 1) bk(length) lt(s) width(w) speed("fastest") draw_tree(l, 4) done() ``` 这段代码实现了动态变化的颜色以及分支结构[^3]。 --- #### 示例2:绘制五角星 这是一个简单的例子,用于展示如何使用循环绘制多个五角星。 ```python from turtle import * def draw_star(x, y): pu() goto(x, y) pd() seth(0) for _ in range(5): fd(40) rt(144) for x in range(0, 250, 50): draw_star(x, 0) done() ``` 该程序定义了一个`draw_star`函数并多次调用它,在不同位置绘制五角星[^3]。 --- #### 示例3:螺旋线图案 下面是一个有趣的螺旋线图案生成器,其中每一步都会稍微旋转角度以形成美丽的曲线效果。 ```python import turtle as t t.speed('fastest') for i in range(18): t.forward(i*10) t.right(90) t.done() ``` 这里通过不断增加步长和固定转向角度构建出了一个逐渐扩大的方形螺旋路径[^4]。 --- #### 示例4:响应鼠标点击事件 还可以结合用户的交互操作完成更有趣的应用场景,比如让用户决定下一步要做什么。 ```python import turtle as t s = t.getscreen() s.onclick(t.goto) # 当用户单击屏幕时,海龟会移动到那个点 t.done() ``` 这允许玩家控制角色的位置,增加了趣味性和互动性[^2]。 --- ### 总结 以上仅列举了一些基本但非常实用的例子,实际上基于这些基础知识还能开发更多创意十足的作品。无论是教育还是娱乐领域,Turtle都展现出了其独特魅力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值