温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-02-11 10:27:28 阅读:335 作者:小新 栏目:开发技术
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关Python如何利用Turtle库绘制一个小老虎,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

导语

哈喽铁汁们好久不见吖~小编已经复工了于是马不停蹄赶来给大家准备新年礼物算开工礼物吧!

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

海龟来作图

虎年就是要画老虎

2022不用纸和笔~

今晚画老虎~

一二三四五

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

老虎宝宝示意图

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

虎年怎么能少得了老虎?画只虎头虎脑的可爱老虎,点燃除夕夜。不用纸和笔,就靠Python海龟作图,小朋友赶紧代码敲起来吧!

1.定义库以及初始化界面

def laohu():
    import turtle as t
    # 设置幕布大小及颜色
    t.screensize(5050, 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(-10597)
    t.setheading(160)
    t.begin_fill()
    t.pendown()
    t.circle(-30230)
    t.setheading(180)
    t.circle(3790)
    t.end_fill()
    # 右耳
    t.penup()
    t.goto(10597)
    t.setheading(20)
    t.begin_fill()
    t.pendown()
    t.circle(30230)
    t.setheading(0)
    t.circle(-3790)
    t.end_fill()

3.画出小老虎头部轮廓

# 头部轮廓
    t.penup()
    t.goto(-67140)
    t.setheading(30)
    t.pendown()
    t.circle(-13460)
 
    t.penup()
    t.goto(-50, -25)
    t.setheading(180)
    t.pendown()
    t.circle(-10030)
    t.circle(-3090)
    t.setheading(100)
    t.circle(-20020)
 
    t.penup()
    t.goto(50, -25)
    t.setheading(0)
    t.pendown()
    t.circle(10030)
    t.circle(3090)
    t.setheading(80)
    t.circle(20020)

4. 画出老虎的两只眼睛

# 两虎眼
    # 左眼
    t.penup()
    t.goto(-9025)
    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(-5343)
    t.setheading(0)
    t.begin_fill()
    t.pendown()
    t.circle(19360)
    t.end_fill()
 
    t.penup()
    t.pensize(4)
    t.goto(-6057)
    t.setheading(30)
    t.pendown()
    t.circle(-1260)
    # 右眼
    t.penup()
    t.goto(9025)
    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(5343)
    t.setheading(0)
    t.begin_fill()
    t.pendown()
    t.circle(13360)
    t.end_fill()
 
    t.penup()
    t.pensize(4)
    t.goto(6057)
    t.setheading(150)
    t.pendown()
    t.circle(1260)

5.画出老虎的鼻子和嘴巴

# 鼻子和嘴吧
    t.penup()
    t.goto(-1620)
    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(-240)
    t.setheading(-60)
    t.pendown()
    t.circle(28120)

6.画出小老虎的左右肢体和脚趾

# 小老虎肢体
    # 左肢
    t.color("orange")
    t.penup()
    t.goto(-65-24)
    t.setheading(-140)
    t.begin_fill()
    t.pendown()
    t.circle(10040)
    t.setheading(180)
    t.circle(3040)
    t.setheading(-40)
    t.circle(4040)
    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(-15030)
    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(3360)
        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(-10040)
    t.setheading(0)
    t.circle(-3040)
    t.setheading(-140)
    t.circle(-4040)
    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(15030)
    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(16540)
    t.penup()
    t.goto(0180)
    t.write("祝大家虎年快乐,虎虎生威!",
            align="center", font=("Times"28"bold"))
 
    t.color("black")
    t.penup()
    t.goto(080)
    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(5050, 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(-4100)
    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如何利用Turtle库绘制一个小老虎

关于“Python如何利用Turtle库绘制一个小老虎”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×