首页 > 编程语言 >使用Python的Turtle库绘制中国火箭模型,点燃航天梦!

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

时间:2024-07-20 17:58:27浏览次数:12  
标签:Turtle right Python seth jump fd 90 绘制 fill

引言

在编程教育中,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的基本操作,还能掌握如何运用循环和函数来构建更复杂的程序逻辑。这是一次很好的编程练习,能够帮助提升你的编程技能,尤其是对于图形绘制的理解和实践。

标签:Turtle,right,Python,seth,jump,fd,90,绘制,fill
From: https://blog.csdn.net/qq_23488347/article/details/140573525

相关文章

  • 用Python调整图片尺寸教程【附源码】
    就像我们学习数学一样,多做题多练习,才会熟能生巧。更多项目源码,实战案例,文件夹领取方式在文末学习Python也是一样,练习才是学好Python的最优解。只有不停的敲代码,不断练习,才能不断进步。实操练习你是否遇到过上传照片有大小要求?不管是上传证件照报名参赛,总会有一个不要......
  • 干货 |Python中的循环结构
    应用场景我们在写程序的时候,一定会遇到需要重复执行某条或某些指令的场景。例如用程序控制机器人踢足球,如果机器人持球而且还没有进入射门范围,那么我们就要一直发出让机器人向球门方向移动的指令。在这个场景中,让机器人向球门方向移动就是一个需要重复的动作,当然这里还会......
  • 干货| Python代码性能优化总结
    本文会介绍不少的Python代码加速运行的技巧。在深入代码优化细节之前,需要了解一些代码优化基本原则。第一个基本原则:不要过早优化很多人一开始写代码就奔着性能优化的目标,“让正确的程序更快要比让快速的程序正确容易得多”。因此,优化的前提是代码能正常工作。过早地进......
  • 在pyspark(python)中将json字符串扩展到多列
    我需要将Json对象(b列)扩展到多列。从此表中,A列B列id1[{a:1,b:'letter1'}]id2[{a:1,b:'letter2',c:3,d:4}]对......
  • python实现文件的读写
    python实现文件的读写路径的书写:open("E:\\ai_03\\code\\ai_03_python\\day07\\data.txt")#两个斜杠open(r"E:\ai_03\code\ai_03_python\day07\data.txt","w",encoding="utf8")#建议使用读文件读文件的格式要以读文件的模式打开一个文件对象,使用Python......
  • python模块化设计
    在Python中,模块化是将代码分解为独立的功能块,并通过导入和使用这些功能块来实现代码复用和组织的一种方式。模块化的编程风格使得代码更易于维护、扩展和测试。以下是Python实现模块化的一些常用方法:使用import语句导入模块:可以使用import语句导入其他Python文件(.py文件)作为......
  • 5分钟解锁python多线程
    以下是一个使用Python多线程的简单示例代码:importthreadingdefprint_numbers():foriinrange(1,6):print(i)defprint_letters():forletterin['A','B','C','D','E']:print(letter)if__nam......
  • 看过来!看过来!python九大数据类型大整合!
    目录一、Int(整型)二、Float(浮点型)三、Bool(布尔类型)四、Str(字符串)(1)拼接:(2)格式化:(3)查找和替换:(4)分割和连接:(5)大小写转换:(6)去除空白字符:五、None(空值)初始化变量作为函数的返回值:在条件语句中检查:六、List(列表)创建List访问List元素修改ListList的遍历七......
  • win系统 python 安装 osgeo库安装(最简单)
    Python osgeo库安装用法介绍安装使用osgeo库,本质是安装gdal一、下载对应python版本压缩包下载地址在结尾二、解压压缩包在解压之后的文件夹当中,找到这两个文件夹三、复制文件夹到python安装目录当中如python环境文件夹路径为D:\Local\Programs\miniconda3\envs\py31......
  • 【Python】使用库 -- 详解
    库就是别人已经写好了的代码,可以让我们直接拿来用。一个编程语言能不能流行起来,一方面取决于语法是否简单方便容易学习,一方面取决于生态是否完备。所谓的“生态” 指的就是语言是否有足够丰富的库,来应对各种各样的场景。在实际开发中,也并非所有的代码都自己手写,而是要充分利......