用python画出你的童年回忆
时间:2022-07-22
本文章向大家介绍用python画出你的童年回忆,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
今天是一年一度的国际儿童节,作为一个程序猿,怎么可以放过这个学习技能的机会呢?
于是,今天我们来学习python的turtle库绘制童年的卡通人物,皮卡丘,小猪佩奇,小黄人,一起做回年轻的那个少年。
一、Turtle图形库简介
Turtle库,又称海龟库,是Python语言中一个很流行的绘制图像的函数库。前面我们已经介绍过很多Turtle相关的知识。
大家可以想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
Turtle库一般python环境会自带。
二、常用函数
- 画笔控制函数
-
penup()
:抬起画笔; -
pendown()
:落下画笔; -
pensize(width)
:画笔宽度; -
pencolor(color)
:画笔颜色;color为颜色字符串或者rgb值
-
- 运动控制函数
-
forward(d)/fd(d)
:直行d个像素; -
circle(r, extent = None)
:绘制半径为r,角度为extent的弧形,圆心默认在海龟左侧距离r的位置;
-
- 方向控制函数
-
setheading(angle)/seth(angle)
:改变前进方向; -
left(angle)
:海龟左转; -
right(angle)
:海龟右转;
-
三、代码演示
下面用海龟库完成小猪佩奇的绘制
import turtle as t
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255,155,192),"pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840,500) # 设置主窗口的大小为840*500
t.speed(10) # 设置画笔速度为10
#鼻子
t.pu() # 提笔
t.goto(-100,100) # 画笔前往坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-30°
t.begin_fill() # 外形填充的开始标志
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3) #向左转3度
t.fd(a) #向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill() # 依据轮廓填充
t.pu() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255,155,192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为5的圆
t.color(160,82,45) # 设置画笔和填充颜色
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()
#头
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300,-30) # 顺时针画一个半径为300,圆心角为30°的园
t.circle(100,-60)
t.circle(80,-100)
t.circle(150,-20)
t.circle(60,-95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3) #向左转3度
t.fd(a) #向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
#耳朵
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
t.end_fill()
#眼睛
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#腮
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
#嘴
t.color(239,69,19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30,40)
t.circle(40,80)
#身体
t.color("red",(255,99,71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()
#手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)
#脚
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
#尾巴
t.pensize(4)
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70,20)
t.circle(10,330)
接下来我们来画皮卡丘
from turtle import *
'''
绘制皮卡丘头部
'''
def face(x, y):
"""画脸"""
begin_fill()
penup()
# 将海龟移动到指定的坐标
goto(x, y)
pendown()
# 设置海龟的方向
setheading(40)
circle(-150, 69)
fillcolor("#FBD624")
# 将海龟移动到指定的坐标
penup()
goto(53.14, 113.29)
pendown()
setheading(300)
circle(-150, 30)
setheading(295)
circle(-140, 20)
print(position())
forward(5)
setheading(260)
circle(-80, 70)
print(position())
penup()
goto(-74.43, -79.09)
pendown()
penup()
# 将海龟移动到指定的坐标
goto(-144, 103)
pendown()
setheading(242)
circle(110, 35)
right(10)
forward(10)
setheading(250)
circle(80, 115)
print(position())
penup()
goto(-74.43, -79.09)
pendown()
setheading(10)
penup()
goto(-144, 103)
pendown()
penup()
goto(x, y)
pendown()
end_fill()
# 下巴
penup()
goto(-50, -82.09)
pendown()
pencolor("#DDA120")
fillcolor("#DDA120")
begin_fill()
setheading(-12)
circle(120, 25)
setheading(-145)
forward(30)
setheading(180)
circle(-20, 20)
setheading(143)
forward(30)
end_fill()
# penup()
# # 将海龟移动到指定的坐标
# goto(0, 0)
# pendown()
def eye():
"""画眼睛"""
# 左眼
color("black", "black")
penup()
goto(-110, 27)
pendown()
begin_fill()
setheading(0)
circle(24)
end_fill()
# 左眼仁
color("white", "white")
penup()
goto(-105, 51)
pendown()
begin_fill()
setheading(0)
circle(10)
end_fill()
# 右眼
color("black", "black")
penup()
goto(25, 40)
pendown()
begin_fill()
setheading(0)
circle(24)
end_fill()
# 右眼仁
color("white", "white")
penup()
goto(17, 62)
pendown()
begin_fill()
setheading(0)
circle(10)
end_fill()
def cheek():
"""画脸颊"""
# 右边
color("#9E4406", "#FE2C21")
penup()
goto(-130, -50)
pendown()
begin_fill()
setheading(0)
circle(27)
end_fill()
# 左边
color("#9E4406", "#FE2C21")
penup()
goto(53, -20)
pendown()
begin_fill()
setheading(0)
circle(27)
end_fill()
def nose():
"""画鼻子"""
color("black", "black")
penup()
goto(-40, 38)
pendown()
begin_fill()
circle(7, steps=3)
end_fill()
def mouth():
"""画嘴"""
color("black", "#F35590")
# 嘴唇
penup()
goto(-10, 22)
pendown()
begin_fill()
setheading(260)
forward(60)
circle(-11, 150)
forward(55)
print(position())
penup()
goto(-38.46, 21.97)
pendown()
end_fill()
# 舌头
color("#6A070D", "#6A070D")
begin_fill()
penup()
goto(-10.00, 22.00)
pendown()
penup()
goto(-14.29, -1.7)
pendown()
penup()
goto(-52, -5)
pendown()
penup()
goto(-60.40, 12.74)
pendown()
penup()
goto(-38.46, 21.97)
pendown()
penup()
goto(-10.00, 22.00)
pendown()
end_fill()
color("black", "#FFD624")
penup()
goto(-78, 15)
pendown()
begin_fill()
setheading(-25)
for i in range(2):
setheading(-25)
circle(35, 70)
end_fill()
color("#AB1945", "#AB1945")
penup()
goto(-52, -5)
pendown()
begin_fill()
setheading(40)
circle(-33, 70)
goto(-16, -1.7)
penup()
goto(-18, -17)
pendown()
setheading(155)
circle(25, 70)
end_fill()
def ear():
"""画耳朵"""
# 左耳
color("black", "#FFD624")
penup()
goto(-145, 93)
pendown()
begin_fill()
setheading(165)
circle(-248, 50)
right(120)
circle(-248, 50)
end_fill()
color("black", "black")
penup()
goto(-240, 143)
pendown()
begin_fill()
setheading(107)
circle(-170, 25)
left(80)
circle(229, 15)
left(120)
circle(300, 15)
end_fill()
# 右耳
color("black", "#FFD624")
penup()
goto(30, 136)
pendown()
begin_fill()
setheading(64)
circle(-248, 50)
right(120)
circle(-248, 50)
end_fill()
color("black", "black")
penup()
goto(160, 200)
pendown()
begin_fill()
setheading(52)
circle(170, 25)
left(116)
circle(229, 15)
left(71)
circle(-300, 15)
end_fill()
def setting():
pensize(2)
# 隐藏海龟
hideturtle()
speed(10)
def main():
"""主函数"""
setting()
face(-132, 115)
eye()
cheek()
nose()
mouth()
ear()
done()
if __name__ == '__main__':
main()
最后我们来画小黄人
import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(2)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()
#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()
#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()
#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(70,30)
t.dot()
#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()
t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()
#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()
#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)
#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()
#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)
t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.end_fill()
turtle.mainloop()
- 马尔可夫链文本生成的简单应用:不足20行的Python代码生成鸡汤文
- 最长递增子序列
- dedecms批量删除文档关键词可以吗
- 【学术】在C ++中使用TensorFlow训练深度神经网络
- 一个canonical标签解决site不在首页的问题
- 由一道面试题来了解进程间的通信
- 【教程】简单教程:用Python解决简单的水果分类问题
- 通过html<map>标签给图片加链接
- Windows下安装Scikit-Learn
- 解决后台无法进入提示DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value
- 一文教你实现skip-gram模型,训练并可视化词向量
- 如何将文章列表用<li>分两列显示
- 面试题之走楼梯问题
- 最长公共子序列与最长公共子串
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法