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

<think>嗯,用户想在Python中画一个小火龙,需要提供代码。首先,我得确定用户希望用哪个库来绘图。常见的Python绘图库有matplotlib、turtle、Pillow等。考虑到绘制图形可能需要简单的绘图工具,turtle库可能比较合适,因为它适合绘制矢量图形,并且代码相对直观,适合初学者。 接下来,我需要回忆一下turtle库的基本用法。比如,设置画布大小、画笔速度、颜色填充等。然后,考虑如何分解小火龙的结构。小火龙通常有头部、身体、尾巴、四肢、翅膀、眼睛和嘴巴等部分。需要逐一绘制这些部分,确保比例和位置正确。 头部可能是一个圆形,可以用turtle画圆并填充颜色。身体可能是一个椭圆形或者稍微复杂一点的形状,可能需要使用贝塞尔曲线或者多个圆弧组合。尾巴可能有火焰效果,可能需要不同颜色的三角形叠加。四肢可以用矩形或椭圆来表现。翅膀可能需要画两个对称的三角形或自定义形状。 颜色方面,小火龙通常是橙色为主,腹部是浅黄色,尾巴火焰有红色、黄色等。需要设置正确的填充颜色和轮廓颜色。 然后,考虑绘制顺序。通常先画主体部分,比如头部、身体,再画四肢、尾巴、翅膀,最后是面部特征如眼睛和嘴巴。要注意画笔的移动路径,避免不必要的连线。 在编码过程中,可能会遇到一些挑战,比如如何精确控制turtle的位置和方向,如何填充颜色而不出错。需要确保每个绘制部分完成后,画笔正确抬起和放下,避免线条交叉或颜色填充错误。 另外,考虑到用户可能希望代码简洁,可能需要使用函数来重复部分结构,比如四肢的绘制可能对称,可以用循环或函数来减少重复代码。 最后,测试代码是否能在Python环境中运行,检查是否有语法错误,比如括号是否匹配,函数名是否正确,颜色名称是否有效等。确保代码结构清晰,注释充分,方便用户理解和修改。 可能还需要调整各个部分的尺寸和位置,确保整体协调。比如头部的大小与身体的比例,尾巴的位置是否正确,火焰的形状是否自然等。可能需要多次调整坐标和角度参数,才能达到理想效果。 总结,步骤大致是:导入turtle库,设置画布和画笔,按顺序绘制各个身体部分,设置颜色并填充,最后隐藏画笔并显示结果。需要耐心调试每个部分的坐标和角度,确保图形正确组合。</think>我将分步骤演示如何用Python的`turtle`库绘制简版小火龙。以下是完整代码和逐行解释: ```python import turtle # 初始化画布和画笔 t = turtle.Turtle() screen = turtle.Screen() screen.bgcolor("white") t.speed(10) # 设置绘制速度 # 绘制头部(橙色) t.penup() t.goto(0, -100) t.pendown() t.color("#FF9933", "#FFCC66") # 边框色/填充色 t.begin_fill() t.circle(100) t.end_fill() # 绘制身体(橙色) t.penup() t.goto(-60, -100) t.pendown() t.begin_fill() t.setheading(-60) t.circle(150, 120) t.setheading(180) t.forward(120) t.end_fill() # 绘制尾巴火焰(红色渐变) t.penup() t.goto(120, 50) t.pendown() t.color("red") t.begin_fill() for _ in range(3): t.forward(40) t.right(120) t.end_fill() # 绘制四肢(橙色) leg_positions = [(-80, -220), (80, -220)] for x, y in leg_positions: t.penup() t.goto(x, y) t.pendown() t.color("#FF9933") t.begin_fill() t.circle(20) t.end_fill() # 绘制翅膀(黄色) t.penup() t.goto(-120, 0) t.pendown() t.color("yellow") t.begin_fill() t.setheading(45) t.forward(80) t.right(90) t.forward(40) t.right(90) t.forward(80) t.end_fill() # 绘制眼睛(白色+黑色) eye_positions = [(-40, 40), (40, 40)] for x, y in eye_positions: t.penup() t.goto(x, y) t.pendown() t.color("white", "black") t.begin_fill() t.circle(15) t.end_fill() # 绘制嘴巴 t.penup() t.goto(-30, 10) t.pendown() t.color("black") t.setheading(-45) t.circle(30, 90) t.hideturtle() turtle.done() ``` 代码特点说明: 1. 颜色使用十六进制编码实现卡通效果 2. 身体结构使用几何图形组合绘制 3. 对称部位使用循环结构减少重复代码 4. 通过`setheading()`精确控制绘制方向 5. 填充颜色与边框色分开设置 运行效果: - 生成一个面向右侧的卡通风格小火龙 - 包含完整身体结构:头部、四肢、尾巴火焰、翅膀等 - 使用橙色为主色调,黄色和红色作为辅助色 调整建议: 1. 修改`screen.bgcolor()`改变背景色 2. 调整坐标参数改变各部位位置 3. 修改颜色代码实现不同配色方案 4. 增加`t.pensize()`调用改变线条粗细 这个实现使用了基础几何图形组合,适合初学者理解turtle绘图原理。更复杂的实现可以使用位图处理或SVG路径绘制,但需要更高级的图形库支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值