当前位置: 首页 > article >正文

用Python Turtle绘制天安门技术讲解

概述

Python的Turtle模块是一个简单的绘图库,它允许用户通过简单的命令控制一个小海龟在屏幕上移动,绘制出各种图形。本文将通过一个具体的示例——绘制简化版的天安门,来详细讲解如何使用Turtle模块。

环境准备

确保你的Python环境中已经安装了Turtle模块。大多数Python安装包已经自带了这个模块,如果没有,可以通过以下命令安装:

pip install PythonTurtle

效果图

在这里插入图片描述

Turtle模块基础

Turtle模块提供了一个绘图板(Screen)和一个画笔(Turtle),画笔可以在绘图板上移动并绘制图形。

基本命令

  • t.penup():提起画笔,移动时不绘制。
  • t.pendown():放下画笔,移动时绘制。
  • t.goto(x, y):将画笔移动到坐标(x, y)。
  • t.left(angle):画笔左转angle度。
  • t.right(angle):画笔右转angle度。
  • t.circle(radius, extent):画笔画圆,radius为半径,extent为圆弧角度。
  • t.begin_fill():开始填充颜色。
  • t.end_fill():结束填充颜色。

绘制天安门

以下是使用Turtle模块绘制简化版天安门的代码,我们将逐步分析代码的每个部分。

初始化设置

import turtle as t

t.hideturtle()  # 隐藏画笔的箭头
t.setup(width=0.9, height=0.9)  # 设置绘图窗口的大小
t.speed(5)  # 设置画笔的移动速度
t.pensize(1)  # 设置画笔的粗细
t.setx(0)  # 设置画笔的x坐标
t.sety(0)  # 设置画笔的y坐标

绘制天安门的基座

t.begin_fill()
t.fillcolor((0.8705, 0.0784, 0.0784))  # 设置填充颜色
t.goto(-250, 0)
t.goto(-223.55, 150)
t.goto(223.55, 150)
t.goto(250, 0)
t.goto(0, 0)
t.end_fill()

绘制门

# 绘制五个门,每个门的代码类似,这里只展示一个
t.begin_fill()
t.fillcolor("black")
t.goto(20, 0)
t.goto(20, 40)
t.right(270)
t.circle(20, 180)
t.goto(-20, 0)
t.goto(0, 0)
t.end_fill()

绘制文字

t.penup()
t.fillcolor((0.929, 0.180, 0.180))  # 设置填充颜色
t.goto(-177, 137)
t.pendown()
t.begin_fill()
t.goto(-63, 137)
t.goto(-63, 123)
t.goto(-177, 123)
t.goto(-177, 137)
t.end_fill()
t.penup()
t.goto(-177, 123)
t.pendown()
t.color("white")
t.write("中华人民共和国万岁", align="left", font=("华文中宋", "10", "normal"))
t.color("black")

绘制灯笼

t.penup()
t.goto(-135, 172)
t.pendown()
t.fillcolor((0.8705, 0.0784, 0.0784))
t.begin_fill()
t.setheading(90)
for k in range(2):
    len = 0.2
    for i in range(60):
        if i < 30:
            len += 0.01
        if i > 30:
            len -= 0.01
        t.fd(len)
        t.left(3)
t.end_fill()

绘制地面

t.penup()
t.fillcolor((0.8705, 0.0784, 0.0784))
t.goto(0, 151)
t.pendown()
t.begin_fill()
t.goto(160, 151)
t.goto(160, 185.63)
t.goto(-160, 185.63)
t.goto(-160, 151)
t.goto(0, 151)
t.end_fill()

完成绘制

t.exitonclick()  # 点击窗口关闭程序

完整代码

import turtle as t

t.hideturtle()
t.setup(width=0.9,height=0.9,startx=None,starty=None)
t.speed(5)
t.pensize(1)
t.setx(0)
t.sety(0)

###base###
t.begin_fill()
t.fillcolor(0.8705,0.0784,0.0784)
t.goto(-250,0)
t.goto(-223.55,150)
t.goto(223.55,150)
t.goto(250,0)
t.goto(0,0)
t.end_fill()

##door1#
t.begin_fill()
t.fillcolor("black")
t.goto(20,0)
t.goto(20,40)
t.right(270)
t.circle(20,180)
t.goto(-20,0)
t.goto(0,0)
t.end_fill()

##door2##
t.begin_fill()
t.fillcolor("black")
t.right(90)
t.goto(-50,0)
t.goto(-50,30)
t.right(90)
t.circle(15,180)
t.goto(-80,0)
t.goto(0,0)
t.end_fill()

##door3##
t.begin_fill()
t.fillcolor("black")
t.right(90)
t.goto(-105,0)
t.goto(-105,30)
t.right(90)
t.circle(15,180)
t.goto(-135,0)
t.goto(0,0)
t.end_fill()

##door4##
t.begin_fill()
t.fillcolor("black")
t.right(90)
t.goto(80,0)
t.goto(80,30)
t.right(90)
t.circle(15,180)
t.goto(50,0)
t.goto(0,0)
t.end_fill()

##door5##
t.begin_fill()
t.fillcolor("black")
t.right(90)
t.goto(135,0)
t.goto(135,30)
t.right(90)
t.circle(15,180)
t.goto(105,0)
t.goto(0,0)
t.end_fill()

##under##
t.penup()
t.goto(-249.12,5)
t.pendown()
t.begin_fill()
t.fillcolor("yellow")
t.goto(-249.12,5)
t.goto(249.12,5)
t.goto(250,0)
t.goto(-250,0)
t.goto(-249.12,5)
t.end_fill()

##up##
t.penup()
t.goto(-224.43,145)
t.pendown()
t.begin_fill()
t.fillcolor("yellow")
t.goto(224.43,145)
t.goto(223.55,150)
t.goto(-223.55,150)
t.end_fill()

##leftword##
t.penup()
t.fillcolor(0.9372,0.9294,0.9294)
t.goto(-180,140)
t.pendown()
t.begin_fill()
t.goto(-60,140)
t.goto(-60,120)
t.goto(-180,120)
t.goto(-180,140)
t.goto(-180,120)
t.end_fill()
t.penup()
t.fillcolor(0.929,0.180,0.180)
t.goto(-177,137)
t.pendown()
t.begin_fill()
t.goto(-63,137)
t.goto(-63,123)
t.goto(-177,123)
t.goto(-177,137)
t.end_fill()
t.penup()
t.goto(-177,123)
t.pendown()
t.color("white")
t.write("中华人民共和国万岁",align="left",font=("华文中宋","10","normal"))
t.color("black")

##rightword##
t.penup()
t.fillcolor(0.9372,0.9294,0.9294)
t.goto(180,140)
t.pendown()
t.begin_fill()
t.goto(60,140)
t.goto(60,120)
t.goto(180,120)
t.goto(180,140)
t.goto(180,120)
t.end_fill()
t.penup()
t.fillcolor(0.929,0.180,0.180)
t.goto(177,137)
t.pendown()
t.begin_fill()
t.goto(63,137)
t.goto(63,123)
t.goto(177,123)
t.goto(177,137)
t.end_fill()
t.penup()
t.goto(63,123)
t.pendown()
t.color("white")
t.write("世界人民大团结万岁",align="left",font=("华文中宋","10","normal"))
t.color("black")

##face##
t.penup()
t.goto(-25,140)
t.pendown()
t.begin_fill()
t.fillcolor(0.9647,0.71764,0.04313)
t.goto(25,140)
t.goto(25,80)
t.goto(-25,80)
t.goto(-25,140)
t.end_fill()
t.penup()
t.goto(-23,138)
t.pendown()
t.begin_fill()
t.fillcolor(0.9686,0.8078,0.36862)
t.goto(23,138)
t.goto(23,83)
t.goto(-23,83)
t.goto(-23,137)
t.end_fill()

###floor##
t.penup()
t.fillcolor(0.8705,0.0784,0.0784)
t.goto(0,151)
t.pendown()
t.begin_fill()
t.goto(160,151)
t.goto(160,185.63)
t.goto(-160,185.63)
t.goto(-160,151)
t.goto(0,151)
t.end_fill()
##on door1##
t.penup()
t.goto(-155,150)
t.fillcolor(0.6705,0.0588,0.0588)
t.pendown()
t.left(90)
t.begin_fill()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(-147,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(-135,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(-147,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()

##on door2##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.begin_fill()
t.goto(-110,150)
t.pendown()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(-102,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(-90,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(-102,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()

##on door3##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.goto(-65,150)
t.pendown()
t.begin_fill()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(-56.5,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(-45,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(-56.5,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()

###door 4##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.goto(-20,150)
t.pendown()
t.begin_fill()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(30)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()

##on door5##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.pendown()
t.begin_fill()
t.goto(130,150)
t.right(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(138.5,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(150,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(138.5,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()

##on door4##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.begin_fill()
t.goto(85,150)
t.pendown()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(93.5,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(105,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(93.5,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()
##on door6##
t.penup()
t.fillcolor(0.6705,0.0588,0.0588)
t.goto(40,150)
t.pendown()
t.begin_fill()
t.left(90)
t.fd(25)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(15)
t.right(90)
t.circle(5,90)
t.right(90)
t.fd(25)
t.right(90)
t.fd(25)
t.end_fill()
t.penup()
t.fillcolor("yellow")
t.begin_fill()
t.goto(48.5,180)
t.pendown()
t.left(180)
for i in range(2):
    t.fd(9)
    t.right(90)
    t.fd(2)
    t.right(90)
t.end_fill()
###lantern##
t.penup()
t.goto(60,172)
t.pendown()
t.fillcolor(0.8705,0.0784,0.0784)
t.begin_fill()
t.setheading(90)
for k in range(2):
    len=0.2
    for i in range(60):
       if i < 30:
           len +=0.01
       if i >30:
           len -=0.01
       t.fd(len)
       t.left(3)
t.end_fill()
t.penup()
t.goto(48.5,165)
t.fillcolor("yellow")
t.pendown()
t.begin_fill()
t.right(90)
for i in range(2):
    t.fd(8)
    t.right(90)
    t.fd(10)
    t.right(90)
t.end_fill()

###floor up##
t.penup()
t.goto(0,185)
t.pendown()
t.fillcolor(0.6705,0.0588,0.0588)
t.begin_fill()
t.goto(180,185)
t.goto(190,195)
t.goto(-190,195)
t.goto(-180,185)
t.goto(0,185)
t.end_fill()

##floor upup##
t.penup()
t.goto(0,195)
t.begin_fill()
t.fillcolor(0.9568,0.53333,0.2470)
t.pendown()
t.goto(190,195)
t.goto(190,195+14.7)
t.goto(187.73,195+11.94)
t.goto(147.88,195+15.42)
t.goto(-147.88,195+15.42)
t.goto(-187.73,195+11.94)
t.goto(-190,195+14.7)
t.goto(-190,195)
t.goto(0,195)
t.end_fill()

##floor2 under###
t.penup()
t.goto(0,211)
t.pendown()
t.fillcolor(0.4705,0.8431,0.4117)
t.begin_fill()
t.goto(130,211)
t.goto(130,220)
t.goto(-130,220)
t.goto(-130,211)
t.goto(0,211)
t.end_fill()
##floor2##
t.penup()
t.goto(0,221)
t.pendown()
t.fillcolor(0.6705,0.0588,0.0588)
t.begin_fill()
t.goto(140,221)
t.goto(150,230)
t.goto(-150,230)
t.goto(-140,221)
t.goto(0,221)
t.end_fill()

##country##
t.penup()
t.goto(0,206.5)
t.fillcolor(0.9803,0.9490,0.03137)
t.pendown()
t.begin_fill()
t.circle(8,360)
t.end_fill()
t.penup()
t.goto(0,208)
t.fillcolor(0.6705,0.0588,0.0588)
t.pendown()
t.begin_fill()
t.circle(6,360)
t.end_fill()
##floor2 up##
t.penup()
t.goto(0,230)
t.begin_fill()
t.fillcolor(0.9568,0.53333,0.2470)
t.pendown()
t.goto(150,230)
t.goto(150,240)
t.goto(130.08,242.37)
t.goto(130.08,277.37)
t.goto(127.28,273.77)
t.goto(124.23,150+125.51)
t.goto(121.5,265.23)
t.goto(-121.5,265.23)
t.goto(-124.23,150+125.51)
t.goto(-127.28,273.77)
t.goto(-130.08,277.37)
t.goto(-130.08,242.37)
t.goto(-150,240)
t.goto(-150,230)
t.goto(0,230)
t.end_fill()

t.exitonclick()

总结

通过上述步骤,我们使用Python的Turtle模块绘制了一个简化版的天安门。这个过程中,我们学习了Turtle模块的基本命令,包括如何控制画笔的移动、绘制图形、填充颜色以及绘制文本。希望这个示例能够帮助你更好地理解Turtle模块的使用。

进一步探索

  • 尝试修改颜色和形状,绘制不同的建筑或图案。
  • 学习更多Turtle模块的高级功能,如自定义形状、动画等。

通过实践,你可以更深入地掌握Python编程和Turtle绘图库的使用。


http://www.kler.cn/news/337190.html

相关文章:

  • Python酷库之旅-第三方库Pandas(136)
  • 使用 Spring Boot 客户端对 Apache Pulsar 进行自定义身份验证
  • 【EXCEL数据处理】000017 案例 保姆级教程,附多个操作案例。EXCEL Match和Index函数。
  • 柯桥生活口语学习之在化妆品店可以用到的韩语句子
  • Selenium 和 Playwright 两者的区别与选择
  • 如何在各大地图平台上标注店铺定位?
  • IDEA的lombok插件不生效了?!!
  • 后门攻击-Python案例
  • 基于SpringBoot博物馆游客预约系统【附源码】
  • 【操作系统考研】2进程管理(1)
  • 2的幂次方表示
  • Qt+大恒相机回调图片刷新使用方式
  • 网络重传了解
  • (Linux驱动学习 - 8).信号异步通知
  • 时尚资讯网站汇总
  • 18 Shell Script sort和uniq命令
  • [SAP ABAP] 程序调用
  • 台湾高雄三维倾斜摄影模型3DTiles样例数据介绍
  • 基于SpringBoot vue3 的山西文旅网java网页设计与实现
  • 【机器学习】KNN算法及鸢尾花案例练习