网络编程
位置:首页>> 网络编程>> Python编程>> Python利用Turtle库绘制一个小老虎

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

作者:车厘子@  发布时间:2023-06-13 11:35:02 

标签:Python,Turtle,老虎

导语

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

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

海龟来作图

虎年就是要画老虎

2022不用纸和笔~

今晚画老虎~

一二三四五

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

老虎宝宝示意图

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

虎年怎么能少得了老虎?画只虎头虎脑的可爱老虎,点燃除夕夜。不用纸和笔,就靠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利用Turtle库绘制一个小老虎

用Python画的小老虎

来源:https://blog.csdn.net/L010409/article/details/122862589

0
投稿

猜你喜欢

  • Data Points Archive 有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整。啊,但关键在于确定如
  • 前言:看本教程,必须先仔细看前言的内容,否则会进入误区!最近在做个性休闲服装内网站的设计课程,过程中发现,个性元素的应用成为最难的问题,第一
  • 一、原因:今天在尝试初始化一个WEB应用的时候,发现其连接不上MySQL,从Traceback看到使用的默认密码为‘YES’。没辙,居然尝试
  • 使用ASP生成图片彩色校验码49行代码,三个文件        Asp文件:Co
  • 网页设计遇到最大的麻烦之一莫过于网页对不同浏览器的兼容性问题了,因为IE 6.0 / IE 7.0 / firefox 2 / Opera
  • 1. 程序背景之前做文件批量移动的时候不小心多加了一个pdf后缀,但问题不大,几行代码就可以搞定~2. 程序要求将以下目录中文件夹中的有问题
  • python学生成绩管理系统创建,供大家参考,具体内容如下要求编写学生类,班级类,并在电脑运行生成表单,输入一个数字,得到对应的结果。输出样
  • CSDN免积分下载 你懂的。1、输入资源地址如:http://download.csdn.net/download/gengqkun/412
  • 一、问题说明首先,运行下述代码,复现问题:# -*-coding:utf-8-*-import reimport requestsfrom
  • PHP mysqli_rollback() 函数关闭自动提交,做一些查询,提交查询,然后回滚当前事务:<?php// 假定数据库用户名
  • 今天交流会上,分享前端的开发经验,有一条虽然很快带过,但是我倒是印象蛮深刻的,就写点小结来分享一下吧。不知道是标准害了大家还是大家害了标准,
  • 前言:在appium中adb命令的使用必不可少,做android测试嘛,adb命令肯定肯定是每天都要用的啦,所以今天给特地写个博客吧!这里就
  • 一、内置函数下面简单介绍几个:1.abs() 求绝对值2.all() 如果 iterable 的所有元素都为真(或者如果可迭代为空),则返回
  • 测试环境Python 3.6.2Win 10 内存 8G,CPU I5 1.6 GHz背景描述这个作品来源于一个日志解析工具的开发,这个开发
  • 这系列文章将介绍一下Oracle的基础知识,将会使用docker下运行的oracle11g的精简版进行说明。这篇文章介绍一下oracle的版
  • python输入错误怎么删除?python常用的输入函数raw_input()在输入的过程中如果输错了,不能像在命令行下那样backspac
  •       在实际信息系统开发中,经常会用到各种各样的协议,网络协议常用的有http,tcp,udp等,传输数
  • 本文实例为大家分享了JSP使用commons-fileupload实现文件上传代码,供大家参考,具体内容如下1、准备:将commons-fi
  • 【摘 要】 我只是提供我几个我认为有助于提高写高性能的asp.net应用程序的技巧,本文提到的提高asp.net性能的技巧只是一个起步,更多
  • python的基础练习案例——名片管理系统,一个控制台程序的案例练习,平台为pycharm2017。代码是看着python视频学的时候写的,
手机版 网络编程 asp之家 www.aspxhome.com