Python利用Turtle库绘制一个小老虎

 更新时间:2022年02月26日 15:40:54   作者:车厘子@  
虎年就是要画老虎!本文将通过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画的小老虎

以上就是Python利用Turtle库绘制一个小老虎的详细内容,更多关于Python Turtle绘制老虎的资料请关注脚本之家其它相关文章!

相关文章

  • Python基础入门之魔法方法与异常处理

    Python基础入门之魔法方法与异常处理

    在python中,所有以“__"双下划线包起来的方法,都统称为魔法方法,下面这篇文章主要给大家介绍了关于Python基础入门之魔法方法与异常处理的相关资料,需要的朋友可以参考下
    2021-11-11
  • 一文教你Python引入其他文件夹下的.py文件

    一文教你Python引入其他文件夹下的.py文件

    这篇文章主要为大家详细介绍了如何在Python中引入其他文件夹里的.py文件,并探讨几种常见的实现方式,有需要的小伙伴可以根据需求进行选择
    2025-03-03
  • Python OpenCV识别行人入口进出人数统计

    Python OpenCV识别行人入口进出人数统计

    本文主要介绍了Python OpenCV识别行人入口进出人数统计,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
    2023-01-01
  • Python Scala中使用def语句定义方法的详细过程

    Python Scala中使用def语句定义方法的详细过程

    这篇文章主要介绍了Python Scala中使用def语句定义方法,Scala的方法是类的一部分,而函数是一个对象可以赋值给一个变量,下面来讲解Scala的方法,需要的朋友可以参考下
    2022-09-09
  • django启动uwsgi报错的解决方法

    django启动uwsgi报错的解决方法

    这篇文章主要给大家介绍了关于django启动uwsgi报错的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • Python微信操控itchat的方法

    Python微信操控itchat的方法

    itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。这篇文章主要介绍了Python微信操控itchat的方法,需要的朋友可以参考下
    2019-05-05
  • python音频处理的示例详解

    python音频处理的示例详解

    这篇文章主要介绍了python音频处理的示例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 详解Python中常用的图片处理函数的使用

    详解Python中常用的图片处理函数的使用

    这篇文章主要为大家介绍了一些在Python中常用的图片处理函数的使用,例如split()、merge()、threshold()、applyColorMap()等,需要的可以参考一下
    2022-01-01
  • Python实现抓取百度搜索结果页的网站标题信息

    Python实现抓取百度搜索结果页的网站标题信息

    这篇文章主要介绍了Python实现抓取百度搜索结果页的网站标题信息,本文使用BeautifulSoup来解析HTML,需要的朋友可以参考下
    2015-01-01
  • Python装饰器语法糖

    Python装饰器语法糖

    今天小编就为大家分享一篇关于Python装饰器语法糖,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01

最新评论