Python turtle库画图

分享了使用Python的Turtle模块首次绘制五星红旗的经历,详细记录了绘制过程,包括设置画笔颜色、绘制国旗的红色背景及黄色五角星,以及调整星星位置等步骤。

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

第一次尝试用turtle画图,很艰难、不完美但十分具有纪念意义!
在这里插入图片描述
希望可以和各位多多交流,共同进步!在这里插入图片描述

#五星红旗

import turtle


turtle.speed(10)
turtle.hideturtle()  #隐藏图标

turtle.pencolor("red")

#外框
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("red")
turtle.fd(600)
turtle.right(90)
turtle.fd(400)
turtle.right(90)
turtle.fd(600)
turtle.right(90)
turtle.fd(400)
turtle.right(90)
turtle.end_fill()

turtle.pencolor("yellow")

#大星星
turtle.penup()
turtle.goto(-275, 115)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("yellow")
turtle.forward(90)
turtle.right(144)
turtle.forward(90)
turtle.right(144)
turtle.forward(90)
turtle.right(144)
turtle.forward(90)
turtle.right(144)
turtle.forward(90)
turtle.end_fill()

#小星星1
turtle.seth(0)
turtle.left(25)
turtle.penup()
turtle.goto(-187, 169)
turtle.pendown()


turtle.begin_fill()
turtle.fillcolor("yellow")
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.end_fill()

#小星星2
turtle.penup()
turtle.goto(-275, 115)
turtle.pendown()
turtle.seth(0)
turtle.left(21)
turtle.penup()
turtle.goto(-147, 133)
turtle.pendown()

turtle.begin_fill()
turtle.fillcolor("yellow")
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.end_fill()

#小星星3
turtle.penup()
turtle.goto(-275, 115)
turtle.pendown()
turtle.seth(0)
turtle.right(21)
turtle.penup()
turtle.goto(-147, 93)
turtle.pendown()


turtle.begin_fill()
turtle.fillcolor("yellow")
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.end_fill()

#小星星4
turtle.penup()
turtle.goto(-275, 115)
turtle.pendown()
turtle.seth(0)
turtle.right(25)
turtle.penup()
turtle.goto(-187, 41)
turtle.pendown()


turtle.begin_fill()
turtle.fillcolor("yellow")
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.right(144)
turtle.forward(30)
turtle.end_fill()
绘制小马宝莉(My Little Pony)这样的复杂图像在 Python 的 `turtle` 中是完全可行的,但需要较为细致的路径规划和颜色填充。由于小马宝莉的形象具有明显的卡通特征,包括复杂的轮廓、渐变色和细节纹理,因此通常需要结合以下方法实现: - 使用多个 `turtle` 实例绘制不同的部分,例如身体、头部、尾巴、鬃毛等。 - 通过 `begin_fill()` 和 `end_fill()` 方法填充颜色。 - 使用 `circle()`、`goto()`、`penup()` 和 `pendown()` 等方法绘制曲线和轮廓。 - 通过设置不同的颜色实现渐变效果或高亮部分。 以下是一个简化的示例,用于绘制一个具有基本轮廓和颜色的小马形象: ```python import turtle def draw_pony_head(): pony = turtle.Turtle() pony.speed(3) # 设置头部轮廓 pony.begin_fill() pony.color("pink") pony.circle(50) pony.end_fill() # 设置眼睛 pony.penup() pony.goto(-15, 60) pony.pendown() pony.begin_fill() pony.color("white") pony.circle(5) pony.end_fill() pony.penup() pony.goto(15, 60) pony.pendown() pony.begin_fill() pony.circle(5) pony.end_fill() # 设置鼻子 pony.penup() pony.goto(0, 50) pony.pendown() pony.begin_fill() pony.color("purple") pony.circle(5) pony.end_fill() # 设置耳朵 pony.penup() pony.goto(-10, 90) pony.pendown() pony.begin_fill() pony.color("pink") pony.goto(-20, 120) pony.goto(0, 110) pony.goto(-10, 90) pony.end_fill() pony.penup() pony.goto(10, 90) pony.pendown() pony.begin_fill() pony.goto(20, 120) pony.goto(0, 110) pony.goto(10, 90) pony.end_fill() # 初始化屏幕 screen = turtle.Screen() screen.bgcolor("lightblue") draw_pony_head() turtle.done() ``` 此代码绘制了一个简化的小马头部轮廓,包含耳朵、眼睛和鼻子等基本特征。如果要绘制更完整的形象,可以扩展以下内容: - **身体和四肢**:通过绘制椭圆或组合直线和曲线实现。 - **尾巴和鬃毛**:使用多段曲线或填充多边形来模拟毛发。 - **渐变色和图案**:通过多层填充和颜色叠加实现更丰富的视觉效果。 - **动画效果**:结合 `turtle` 的移动和旋转功能,为小马添加动态表现。 在实际开发过程中,可以先使用绘图软件设计轮廓,再将其转化为 `turtle` 命令以提高效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值