使用Python的Turtle库绘制中国火箭模型,点燃航天梦!

引言

在编程教育中,turtle模块是一个非常受欢迎的图形化编程工具,尤其适合初学者学习和实践。它允许我们通过控制一个可以在屏幕上移动的“小乌龟”来绘制各种形状和图案,从而让编程变得直观且有趣。

Turtle库简介

turtle是Python标准库的一部分,它提供了一个简单而强大的绘图环境。用户可以通过发送指令(如前进、后退、左转、右转)给一个虚拟的“乌龟”,并让它在屏幕上绘制出路径。此外,turtle还支持填充颜色、改变线条宽度和颜色等功能,使得创建复杂图形变得容易。

基本使用
  • t.forward(distance):向前移动指定距离。
  • t.backward(distance):向后移动指定距离。
  • t.right(angle):向右转指定角度。
  • t.left(angle):向左转指定角度。
  • t.penup():提起笔,移动时不会绘制。
  • t.pendown():放下笔,移动时会绘制。
  • t.goto(x, y):直接移动到屏幕上的指定坐标。
  • t.color(color):设置线条颜色。
  • t.fillcolor(color):设置填充颜色。
  • t.begin_fill():开始填充。
  • t.end_fill():结束填充。
  • t.write(text):在当前位置书写文本。
  • t.dot(size):绘制一个点。
  • t.hideturtle():隐藏乌龟。
运行效果

在这里插入图片描述

案例:绘制中国火箭模型

下面的代码展示了如何使用turtle库绘制一个中国火箭模型,并在其上添加国旗和文字。我们将逐行解析代码,了解它是如何工作的。

import turtle as t
import math as m

# 设置画布大小和背景色
t.setup(600, 700)
t.bgcolor("#1F4091")

# 定义跳跃函数,使乌龟移动到指定位置
def jump(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()

# 定义正弦和余弦函数用于绘制火箭头部曲线
def draw_x(a, i):
    angle = m.radians(i)
    return a * m.sin(angle)

def draw_y(b, i):
    angle = m.radians(i)
    return b * m.cos(angle)

# 绘制火箭主体
jump(-6, 256)
t.pensize(3)
t.color("#EEEFF2")
t.begin_fill()
for i in range(45):
    x = draw_x(42, i)+(-6)
    y = draw_y(256, i)
    t.goto(x, y)
# ... (省略部分代码)
t.end_fill()

# 绘制火箭上的国旗和文字
jump(-22, 191)
t.fillcolor("red")
t.begin_fill()
t.fd(30)
t.right(90)
t.fd(20)
t.right(90)
t.fd(30)
t.right(90)
t.fd(20)
t.end_fill()

# ... (省略部分代码)

# 显示点击坐标和乌龟当前角度
def get_click_coord(x, y):
    print("点击坐标点:({}, {})".format(x, y))
    print("海龟当前角度:{}".format(t.heading()))

canvas = t.Screen()
canvas.onscreenclick(get_click_coord)
canvas.mainloop()

# 隐藏海龟
t.hideturtle()
t.done()
完整代码

import turtle as t
import math as m

t.setup(600, 700)
t.bgcolor("#1F4091")
t.color('red')


def jump(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()


def draw_x(a, i):
    angle = m.radians(i)
    return a * m.sin(angle)


def draw_y(b, i):
    angle = m.radians(i)
    return b * m.cos(angle)


# 画出火箭主体
jump(-6, 256)
t.pensize(3)
t.color("#EEEFF2")
t.fillcolor()
t.begin_fill()
for i in range(45):
    x = draw_x(42, i)+(-6)
    y = draw_y(256, i)
    t.goto(x, y)
t.right(90)
t.fd(60)
t.right(30)
t.fd(10)
t.left(30)
t.fd(175)
t.seth(-60)
t.fd(60)
t.seth(-90)
t.fd(160)
t.seth(-45)
t.fd(23)
t.seth(-90)
t.fd(20)
t.right(90)
t.fd(24)
t.right(90)
t.fd(7)
t.left(90)
t.fd(25)
t.right(90)
t.fd(45)

t.left(90)
t.fd(47)

t.left(90)
t.fd(45)
t.right(90)
t.fd(25)
t.left(90)
t.fd(7)
t.right(90)
t.fd(24)
t.right(90)
t.fd(20)
t.right(45)
t.fd(23)
t.left(45)
t.fd(160)
t.right(30)
t.fd(60)
t.left(30)
t.fd(175)
t.left(30)
t.fd(7)
t.right(30)
t.fd(60)


for i in range(47):
    x = draw_x(-42, i)+(-6)
    y = draw_y(256, i)
    t.goto(x, y)
t.end_fill()


jump(-22, 229)
t.color("#7B7E85")
t.pensize(1)
t.seth(0)
t.fd(34)

# 火箭上的国旗
jump(-22, 191)
t.fillcolor("red")
t.begin_fill()
t.fd(30)
t.right(90)
t.fd(20)
t.right(90)
t.fd(30)
t.right(90)
t.fd(20)
t.end_fill()

# 画火箭上国旗上的星星
jump(-18, 180)
t.pencolor("yellow")
t.begin_fill()
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(5)
    t.right(144)
t.end_fill()

t.tracer(False)
jump(-11, 187)
t.seth(0)
t.left(45)
t.begin_fill()
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(2)
    t.right(144)
t.end_fill()

jump(-8, 185)
t.seth(0)
t.left(45)
t.begin_fill()
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(2)
    t.right(144)
t.end_fill()

jump(-8, 181)
t.seth(0)
t.left(45)
t.begin_fill()
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(2)
    t.right(144)
t.end_fill()

jump(-11, 179)
t.seth(0)
t.left(45)
t.begin_fill()
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(2)
    t.right(144)
t.end_fill()


jump(-15, 157)
t.color("black")
t.write("CHINA", font=("Arial", 5))

t.color("#7B7E85")
jump(-28, 148)
t.dot(5)  # 点绘制函数
jump(-15, 148)
t.dot(5)
jump(0, 148)
t.dot(5)
jump(15, 148)
t.dot(5)
jump(-33, 140)
t.seth(0)
t.fd(53)
t.color("#7B7E85")
jump(-31, 112)
t.fd(50)
jump(-33, 5)
t.fd(50)
jump(-33, -60)
t.fd(50)
jump(-30, -78)
t.fd(45)

t.color("red")
jump(-33, 124)
t.fd(53)
jump(-31, 80)
t.fd(49)



jump(-18, 98)
t.seth(0)
t.begin_fill()
t.color("yellow")
t.fillcolor("yellow")
for i in range(1, 6):
    t.forward(20)
    t.right(144)
t.end_fill()

t.tracer(True)
word = "中国航天"
y = 50
delta_y = 35
for c in word:
    t.penup()
    t.setx(-15)
    t.sety(y)
    t.color('blue')
    t.write(c, font=("宋体", 15, "bold"))
    y -= delta_y


t.color("#7B7E85")
t.pensize(3)
jump(-32, -61)
t.seth(-90)
t.fd(196)
jump(15, -59)
t.fd(196)

t.seth(0)
jump(-66, -110)
t.fd(34)
jump(-66, -130)
t.fd(34)

jump(16, -110)
t.fd(34)
jump(16, -130)
t.fd(34)

jump(-30, -214)
t.fd(46)
jump(-30, -200)
t.fd(46)

jump(-59, -137)
t.color('red')
for i in range(15):
    if(i % 5 == 0):
        jump(-59 + 16, -(137 + (i * 10)))
        t.dot(5)
    jump(-59, -(137 + (i * 10)))
    t.dot(5)
jump(43, -137)
t.color('red')
for i in range(15):
    if (i % 5 == 0):
        jump(43 - 16, -(137 + (i * 10)))
        t.dot(5)
    jump(43, -(137 + (i * 10)))
    t.dot(5)


jump(-65, -271)
t.color("#7B7E85")
t.begin_fill()
t.fillcolor()
t.seth(-90)
t.fd(19)
t.right(45)
t.fd(22)
t.seth(90)
t.fd(20)
t.end_fill()

jump(50, -271)
t.begin_fill()
t.fillcolor()
t.seth(-90)
t.fd(19)
t.left(45)
t.fd(22)
t.seth(90)
t.fd(20)
t.end_fill()

jump(-30, -258)
t.seth(180)
t.begin_fill()
t.fillcolor()
t.fd(10)
t.left(90)
t.fd(42)
t.left(90)
t.fd(10)
t.end_fill()

jump(13, -258)
t.seth(0)
t.begin_fill()
t.fillcolor()
t.fd(10)
t.right(90)
t.fd(42)
t.right(90)
t.fd(10)
t.end_fill()

jump(-31, -259)
t.color('black')
t.begin_fill()
t.fillcolor()
t.seth(0)
t.fd(43)
t.right(90)
t.fd(20)
t.right(90)
t.fd(43)
t.right(90)
t.fd(20)
t.end_fill()

t.hideturtle()

def get_click_coord(x, y):
    print("点击坐标点:({}, {})".format(x, y))
    print("海龟当前角度:{}".format(t.heading()))


canvas = t.Screen()
canvas.onscreenclick(get_click_coord)
canvas.mainloop()

# 隐藏海龟
t.hideturtle()
t.done()

通过这个项目,你不仅可以学习到turtle的基本操作,还能掌握如何运用循环和函数来构建更复杂的程序逻辑。这是一次很好的编程练习,能够帮助提升你的编程技能,尤其是对于图形绘制的理解和实践。

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
通过使用pythonturtle绘制图形,我得到了很多收获。 首先,使用pythonturtle绘制图形可以帮助我更好地理解和应用编程的概念。在绘制图形的过程中,我需要使用很多编程基础知识和技巧,例如变量、循环、条件判断等等。通过实践绘制图形,我可以更深入地理解这些概念,并且能够将它们应用到其他编程任务中。 其次,使用pythonturtle可以培养我的创造力和想象力。在绘制图形的过程中,我可以自由发挥,设计出各种有趣的形状和图案。这让我感受到编程的乐趣,并且可以通过图形来表达自己的想法和创意。 此外,使用pythonturtle还可以提高我的问题解决能力。在绘制图形的过程中,我会遇到各种各样的问题,例如如何控制海龟的移动、如何调整图形的形状等等。通过不断尝试和调试,我能够找到解决问题的方法,并且学会了如何通过编程来解决实际问题。 最后,使用pythonturtle绘制图形还可以提升我的耐心和坚持能力。有时候绘制复杂的图形需要花费很长时间,但是我需要持续进行尝试和调整,直到达到预期的效果。这让我学会了面对困难时的耐心和坚持,同时也增加了我的毅力。 总之,通过使用pythonturtle绘制图形,我不仅学到了有关编程的知识和技巧,还培养了创造力、问题解决能力、耐心和坚持能力。这些收获对于我以后的学习和工作都非常有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写代码的小朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值