Python利用Turtle库绘制一个小老虎
作者:车厘子@ 发布时间:2023-06-13 11:35:02
标签:Python,Turtle,老虎
导语
哈喽铁汁们好久不见吖~小编已经复工了于是马不停蹄赶来给大家准备新年礼物算开工礼物吧!
海龟来作图
虎年就是要画老虎
2022不用纸和笔~
今晚画老虎~
一二三四五
老虎宝宝示意图
虎年怎么能少得了老虎?画只虎头虎脑的可爱老虎,点燃除夕夜。不用纸和笔,就靠Python海龟作图,小朋友赶紧代码敲起来吧!
1.定义库以及初始化界面
def laohu():
import turtle as t
# 设置幕布大小及颜色
t.screensize(50, 50, bg='yellow')
t.title("老虎宝宝")
t.shape("classic")
t.pensize(10)
t.color("orange")
t.fillcolor("pink")
t.speed(100)
t.hideturtle()
2.画出左右两只耳朵
# 左耳
t.penup()
t.goto(-105, 97)
t.setheading(160)
t.begin_fill()
t.pendown()
t.circle(-30, 230)
t.setheading(180)
t.circle(37, 90)
t.end_fill()
# 右耳
t.penup()
t.goto(105, 97)
t.setheading(20)
t.begin_fill()
t.pendown()
t.circle(30, 230)
t.setheading(0)
t.circle(-37, 90)
t.end_fill()
3.画出小老虎头部轮廓
# 头部轮廓
t.penup()
t.goto(-67, 140)
t.setheading(30)
t.pendown()
t.circle(-134, 60)
t.penup()
t.goto(-50, -25)
t.setheading(180)
t.pendown()
t.circle(-100, 30)
t.circle(-30, 90)
t.setheading(100)
t.circle(-200, 20)
t.penup()
t.goto(50, -25)
t.setheading(0)
t.pendown()
t.circle(100, 30)
t.circle(30, 90)
t.setheading(80)
t.circle(200, 20)
4. 画出老虎的两只眼睛
# 两虎眼
# 左眼
t.penup()
t.goto(-90, 25)
t.setheading(-45)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(-53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(19, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(-60, 57)
t.setheading(30)
t.pendown()
t.circle(-12, 60)
# 右眼
t.penup()
t.goto(90, 25)
t.setheading(45)
t.pensize(2)
t.fillcolor("orange")
t.begin_fill()
t.pendown()
# 椭圆绘制技巧
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.1
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.1
t.lt(3)
t.fd(a)
t.end_fill()
t.fillcolor("pink")
t.penup()
t.goto(53, 43)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(13, 360)
t.end_fill()
t.penup()
t.pensize(4)
t.goto(60, 57)
t.setheading(150)
t.pendown()
t.circle(12, 60)
5.画出老虎的鼻子和嘴巴
# 鼻子和嘴吧
t.penup()
t.goto(-16, 20)
t.setheading(-90)
t.fillcolor("pink")
t.begin_fill()
t.pendown()
a = 0.2
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3)
t.fd(a)
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
t.goto(-24, 0)
t.setheading(-60)
t.pendown()
t.circle(28, 120)
6.画出小老虎的左右肢体和脚趾
# 小老虎肢体
# 左肢
t.color("orange")
t.penup()
t.goto(-65, -24)
t.setheading(-140)
t.begin_fill()
t.pendown()
t.circle(100, 40)
t.setheading(180)
t.circle(30, 40)
t.setheading(-40)
t.circle(40, 40)
t.setheading(-150)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.lt(3)
t.fd(a)
t.setheading(93)
t.circle(-150, 30)
t.end_fill()
t.penup()
t.goto(-85, -115)
t.setheading(-150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
# 每个脚趾绘制函数
def toe(x, y):
t.begin_fill()
t.goto(x, y)
t.circle(3, 360)
t.end_fill()
t.penup()
toe(-98, -120)
toe(-96, -110)
toe(-88, -105)
toe(-80, -105)
# 右肢
t.color("orange")
t.penup()
t.goto(65, -24)
t.setheading(-40)
t.begin_fill()
t.pendown()
t.circle(-100, 40)
t.setheading(0)
t.circle(-30, 40)
t.setheading(-140)
t.circle(-40, 40)
t.setheading(-30)
a = 0.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.05
t.rt(3) # 向左转3度
t.fd(a) # 向前走a的步长
elif 30 <= i < 60 or 90 <= i < 100:
a = a - 0.05
t.rt(3)
t.fd(a)
t.setheading(87)
t.circle(150, 30)
t.end_fill()
t.penup()
t.goto(85, -115)
t.setheading(150)
t.color("pink", "pink")
t.begin_fill()
t.pendown()
a = 0.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.03
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.03
t.lt(3)
t.fd(a)
t.end_fill()
t.penup()
toe(98, -120)
toe(96, -110)
toe(88, -105)
toe(80, -105)
7.在需要的位置写上我们的新年祝福
t.goto(-57, -140)
t.color("orange")
t.setheading(-20)
t.pendown()
t.circle(165, 40)
t.penup()
t.goto(0, 180)
t.write("祝大家虎年快乐,虎虎生威!",
align="center", font=("Times", 28, "bold"))
t.color("black")
t.penup()
t.goto(0, 80)
t.write("王",
align="center", font=("Times", 38, "bold"))
t.penup()
t.goto(0, -5)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -15)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
t.goto(0, -25)
t.write("一 一",
align="center", font=("Times", 18, "bold"))
8. 显示倒数3,2,1
#显示倒数3,2,1
def draw_0(i):
turtle.screensize(50, 50, bg='yellow')
turtle.speed(0)
turtle.penup()
turtle.hideturtle() # 隐藏箭头显示
turtle.goto(-50, -100)
turtle.color('red')
write = turtle.write(i, font=('宋体', 200, 'normal'))
time.sleep(1)
9.显示我们需要的文字
# 显示文字
def draw_1():
turtle.penup()
turtle.hideturtle() #隐藏箭头显示
turtle.goto(-410, 0)
turtle.color('red')
write = turtle.write('叮咚~新年礼物到啦💕', font=('宋体', 60, 'normal'))
time.sleep(2)
10.设定代码运行入口,调用目标函数
number=[3,2,1] #储存显示界面倒数数字1,2,3
if __name__ == '__main__':
turtle.setup(900, 500) #调画布的尺寸
for i in number:
turtle.screensize(50, 50, bg='yellow')
draw_0(i)
clear_screen()
turtle.screensize(50, 50, bg='yellow')
draw_1()
clear_screen()
turtle.screensize(50, 50, bg='yellow')
laohu()
time.sleep(5)
threads = []
for i in range(100): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.01)
threads[i].start()
成果展示
用Python画的小老虎
来源:https://blog.csdn.net/L010409/article/details/122862589


猜你喜欢
- 目录一、建立画布二、用plt.subplot函数建立坐标系,并分别绘制折线图和柱状图三、完整代码如下所示四、对应效果图如下所示一、建立画布i
- 1.join函数的语法及用法(1)语法:'sep'.join(sep_object)参数说明sep:分割符,可为&l
- 基本操作查看数据库<code>show databases;</code>指定字符集<code>crea
- BULK COLLECT(成批聚合类型)和数组集合type类型is table of 表%rowtype index by binary_i
- 文件拆分代码:#-*-encoding:utf-8-*-import osimport sysimport threadingdef get
- Python 操作 Excel常用工具数据处理是 Python 的一大应用场景,而 Excel 又是当前最流行的数据处理软件。因此用 Pyt
- 使用环境在cmd模式下输入 mysql --version (查看mysql安装的版本).完整的命令可以通过mysql --help来获取.
- python 二维列表转置def transpose(self, matrix): new_matrix = []
- 在进行ASP网站开发时,有时需在客户端调用MSSQL数据库的数据进行打印,若调用数据量小,可以通过在客户端运用FileSystemObjec
- 本文实例讲述了Python面向对象之类和对象属性的增删改查操作。分享给大家供大家参考,具体如下:一、类属性的操作# -*- coding:u
- 本文实例讲述了Python对列表排序的方法。分享给大家供大家参考。具体分析如下:1、sort()函数sort()函数使用固定的排序算法对列表
- 利用python3来实现TCP协议,和UDP类似。UDP应用于及时通信,而TCP协议用来传送文件、命令等操作,因为这些数据不允许丢失,否则会
- 1. requests发送文件功能Requests 使得上传多部分编码文件变得很简单url = 'http://httpbin.or
- 一、决策树原理决策树是用样本的属性作为结点,用属性的取值作为分支的树结构。 决策树的根结点是所有样本中信息量最大的属性。树的中间结点是该结点
- sql脚本是包含一到多个sql命令的sql语句,我们可以将这些sql脚本放在一个文本文件中(我们称之为“sql脚本文件”),然后通过相关的命
- 数据预处理在解决深度学习问题的过程中,往往需要花费大量的时间和精力。 数据处理的质量对训练神经网络来说十分重要,良好的数据处理不仅会加速模型
- JavaScript中的定时器大家基本在平时的开发中都遇见过吧,但是又有多少人去深入的理解其中的原理呢?下面我们就来分析一下定时器的实现原理
- 本文实例讲述了python获取Linux下文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下:区别于前文所述。本例是在lin
- 什么是异常?异常是一个事件,其中一个程序,破坏程序的指令的正常流的执行过程中而发生的。一般情况下,当一个Python脚本遇到一些情况不能处理
- PHP xpath() 函数定义和用法xpath()函数运行对 XML 文档的 XPath 查询。如果成功,该函数返回 SimpleXMLE