python实战之一步一步教你绘制小猪佩奇
程序员文章站
2022-04-23 18:34:06
一、定义全局属性1# 导入turtle绘图库 2 3import turtle as tle 4 5class peppa_turtle: 6 7 # 初始化全局属性 8 9 def __...
一、定义全局属性
1# 导入turtle绘图库 2 3import turtle as tle 4 5class peppa_turtle: 6 7 # 初始化全局属性 8 9 def __init__(self): 10 11 self.tle = tle 12 13 # #全局画笔属性设置 14 15 # 设置画笔的大小 16 17 self.tle.pensize(4) 18 19 # 设置gbk颜色范围为0-255 20 21 self.tle.colormode(255) 22 23 # 设置画笔颜色和填充颜色 24 25 self.tle.color((255, 155, 192), "pink") 26 27 # 设置主窗口的大小为800*500 28 29 self.tle.setup(800, 600) 30 31 # 设置画笔速度为1 32 33 self.tle.speed(1)
二、定义鼻子绘制函数
1# 绘制小猪佩奇的鼻子 2 3 def draw_nose(self): 4 5 # 提笔 6 7 self.tle.pu() 8 9 # 画笔前往坐标(-100,100) 10 11 self.tle.goto(-100, 100) 12 13 # 下笔 14 15 self.tle.pd() 16 17 # 笔的角度为-30° 18 19 self.tle.seth(-30) 20 21 # 外形填充的开始标志 22 23 self.tle.begin_fill() 24 25 a = 0.4 26 27 for i in range(120): 28 29 if 0 <= i < 30 or 60 <= i < 90: 30 31 a = a + 0.08 32 33 # 向左转3度 34 35 self.tle.lt(3) 36 37 # 向前走a的步长 38 39 self.tle.fd(a) 40 41 else: 42 43 a = a - 0.08 44 45 self.tle.lt(3) 46 47 self.tle.fd(a) 48 49 # 依据轮廓填充 50 51 self.tle.end_fill() 52 53 # 提笔 54 55 self.tle.pu() 56 57 # 笔的角度为90度 58 59 self.tle.seth(90) 60 61 # 向前移动25 62 63 self.tle.fd(25) 64 65 # 转换画笔的角度为0 66 67 self.tle.seth(0) 68 69 self.tle.fd(10) 70 71 self.tle.pd() 72 73 # 设置画笔颜色 74 75 self.tle.pencolor(255, 155, 192) 76 77 self.tle.seth(10) 78 79 self.tle.begin_fill() 80 81 # 画一个半径为5的圆 82 83 self.tle.circle(5) 84 85 # 设置画笔和填充颜色 86 87 self.tle.color(160, 82, 45) 88 89 self.tle.end_fill() 90 91 self.tle.pu() 92 93 self.tle.seth(0) 94 95 self.tle.fd(20) 96 97 self.tle.pd() 98 99 self.tle.pencolor(255, 155, 192) 100 101 self.tle.seth(10) 102 103 self.tle.begin_fill() 104 105 self.tle.circle(5) 106 107 self.tle.color(160, 82, 45) 108 109 self.tle.end_fill()
三、定义头部绘制函数
1# 绘制小猪佩奇的头 2 3 def draw_head(self): 4 5 self.tle.color((255, 155, 192), "pink") 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(41) 12 13 self.tle.seth(0) 14 15 self.tle.fd(0) 16 17 self.tle.pd() 18 19 self.tle.begin_fill() 20 21 self.tle.seth(180) 22 23 # 顺时针画一个半径为300,圆心角为30°的园 24 25 self.tle.circle(300, -30) 26 27 self.tle.circle(100, -60) 28 29 self.tle.circle(80, -100) 30 31 self.tle.circle(150, -20) 32 33 self.tle.circle(60, -95) 34 35 self.tle.seth(161) 36 37 self.tle.circle(-300, 15) 38 39 self.tle.pu() 40 41 self.tle.goto(-100, 100) 42 43 self.tle.pd() 44 45 self.tle.seth(-30) 46 47 a = 0.4 48 49 for i in range(60): 50 51 if 0 <= i < 30 or 60 <= i < 90: 52 53 a = a + 0.08 54 55 # 向左转3度 56 57 self.tle.lt(3) 58 59 # 向前走a的步长 60 61 self.tle.fd(a) 62 63 else: 64 65 a = a - 0.08 66 67 self.tle.lt(3) 68 69 self.tle.fd(a) 70 71 self.tle.end_fill()
四、定义耳朵绘制函数
1# 绘制小猪佩奇的耳朵 2 3 def draw_ear(self): 4 5 self.tle.color((255, 155, 192), "pink") 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(-7) 12 13 self.tle.seth(0) 14 15 self.tle.fd(70) 16 17 self.tle.pd() 18 19 self.tle.begin_fill() 20 21 self.tle.seth(100) 22 23 self.tle.circle(-50, 50) 24 25 self.tle.circle(-10, 120) 26 27 self.tle.circle(-50, 54) 28 29 self.tle.end_fill() 30 31 self.tle.pu() 32 33 self.tle.seth(90) 34 35 self.tle.fd(-12) 36 37 self.tle.seth(0) 38 39 self.tle.fd(30) 40 41 self.tle.pd() 42 43 self.tle.begin_fill() 44 45 self.tle.seth(100) 46 47 self.tle.circle(-50, 50) 48 49 self.tle.circle(-10, 120) 50 51 self.tle.circle(-50, 56) 52 53 self.tle.end_fill()
五、定义眼睛绘制函数
1# 绘制小猪佩奇的眼睛 2 3 def draw_eye(self): 4 5 self.tle.color((255, 155, 192), "white") 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(-20) 12 13 self.tle.seth(0) 14 15 self.tle.fd(-95) 16 17 self.tle.pd() 18 19 self.tle.begin_fill() 20 21 self.tle.circle(15) 22 23 self.tle.end_fill() 24 25 self.tle.color("black") 26 27 self.tle.pu() 28 29 self.tle.seth(90) 30 31 self.tle.fd(12) 32 33 self.tle.seth(0) 34 35 self.tle.fd(-3) 36 37 self.tle.pd() 38 39 self.tle.begin_fill() 40 41 self.tle.circle(3) 42 43 self.tle.end_fill() 44 45 self.tle.color((255, 155, 192), "white") 46 47 self.tle.pu() 48 49 self.tle.seth(90) 50 51 self.tle.fd(-25) 52 53 self.tle.seth(0) 54 55 self.tle.fd(40) 56 57 self.tle.pd() 58 59 self.tle.begin_fill() 60 61 self.tle.circle(15) 62 63 self.tle.end_fill() 64 65 self.tle.color("black") 66 67 self.tle.pu() 68 69 self.tle.seth(90) 70 71 self.tle.fd(12) 72 73 self.tle.seth(0) 74 75 self.tle.fd(-3) 76 77 self.tle.pd() 78 79 self.tle.begin_fill() 80 81 self.tle.circle(3) 82 83 self.tle.end_fill()
六、定义腮绘制函数
1# 绘制小猪佩奇的腮 2 3 def draw_cheek(self): 4 5 self.tle.color((255, 155, 192)) 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(-95) 12 13 self.tle.seth(0) 14 15 self.tle.fd(65) 16 17 self.tle.pd() 18 19 self.tle.begin_fill() 20 21 self.tle.circle(30) 22 23 self.tle.end_fill()
定义嘴巴绘制函数
1# 绘制小猪佩奇的嘴 2 3 def draw_mouth(self): 4 5 self.tle.color(239, 69, 19) 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(15) 12 13 self.tle.seth(0) 14 15 self.tle.fd(-100) 16 17 self.tle.pd() 18 19 self.tle.seth(-80) 20 21 self.tle.circle(30, 40) 22 23 self.tle.circle(40, 80)
七、定义身体绘制函数
1# 绘制小猪佩奇的身体 2 3 def draw_body(self): 4 5 self.tle.color("red", (255, 99, 71)) 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(-20) 12 13 self.tle.seth(0) 14 15 self.tle.fd(-78) 16 17 self.tle.pd() 18 19 self.tle.begin_fill() 20 21 self.tle.seth(-130) 22 23 self.tle.circle(100, 10) 24 25 self.tle.circle(300, 30) 26 27 self.tle.seth(0) 28 29 self.tle.fd(230) 30 31 self.tle.seth(90) 32 33 self.tle.circle(300, 30) 34 35 self.tle.circle(100, 3) 36 37 self.tle.color((255, 155, 192), (255, 100, 100)) 38 39 self.tle.seth(-135) 40 41 self.tle.circle(-80, 63) 42 43 self.tle.circle(-150, 24) 44 45 self.tle.end_fill()
八、定义手绘制函数
1# 绘制小猪佩奇的手 2 3 def draw_hand(self): 4 5 self.tle.color((255, 155, 192)) 6 7 self.tle.pu() 8 9 self.tle.seth(90) 10 11 self.tle.fd(-40) 12 13 self.tle.seth(0) 14 15 self.tle.fd(-27) 16 17 self.tle.pd() 18 19 self.tle.seth(-160) 20 21 self.tle.circle(300, 15) 22 23 self.tle.pu() 24 25 self.tle.seth(90) 26 27 self.tle.fd(15) 28 29 self.tle.seth(0) 30 31 self.tle.fd(0) 32 33 self.tle.pd() 34 35 self.tle.seth(-10) 36 37 self.tle.circle(-20, 90) 38 39 self.tle.pu() 40 41 self.tle.seth(90) 42 43 self.tle.fd(30) 44 45 self.tle.seth(0) 46 47 self.tle.fd(237) 48 49 self.tle.pd() 50 51 self.tle.seth(-20) 52 53 self.tle.circle(-300, 15) 54 55 self.tle.pu() 56 57 self.tle.seth(90) 58 59 self.tle.fd(20) 60 61 self.tle.seth(0) 62 63 self.tle.fd(0) 64 65 self.tle.pd() 66 67 self.tle.seth(-170) 68 69 self.tle.circle(20, 90)
九、定义脚绘制函数
1# 绘制小猪佩奇的脚 2 3 def draw_foot(self): 4 5 self.tle.pensize(10) 6 7 self.tle.color((240, 128, 128)) 8 9 self.tle.pu() 10 11 self.tle.seth(90) 12 13 self.tle.fd(-75) 14 15 self.tle.seth(0) 16 17 self.tle.fd(-180) 18 19 self.tle.pd() 20 21 self.tle.seth(-90) 22 23 self.tle.fd(40) 24 25 self.tle.seth(-180) 26 27 self.tle.color("black") 28 29 self.tle.pensize(15) 30 31 self.tle.fd(20) 32 33 self.tle.pensize(10) 34 35 self.tle.color((240, 128, 128)) 36 37 self.tle.pu() 38 39 self.tle.seth(90) 40 41 self.tle.fd(40) 42 43 self.tle.seth(0) 44 45 self.tle.fd(90) 46 47 self.tle.pd() 48 49 self.tle.seth(-90) 50 51 self.tle.fd(40) 52 53 self.tle.seth(-180) 54 55 self.tle.color("black") 56 57 self.tle.pensize(15) 58 59 self.tle.fd(20)
十、定义尾巴绘制函数
1# 绘制小猪佩奇的尾巴 2 3 def draw_tail(self): 4 5 self.tle.pensize(4) 6 7 self.tle.color((255, 155, 192)) 8 9 self.tle.pu() 10 11 self.tle.seth(90) 12 13 self.tle.fd(70) 14 15 self.tle.seth(0) 16 17 self.tle.fd(95) 18 19 self.tle.pd() 20 21 self.tle.seth(0) 22 23 self.tle.circle(70, 20) 24 25 self.tle.circle(10, 330) 26 27 self.tle.circle(70, 30)
十一、定义手动结束函数
1# 手动结束绘制 2 def exec_end(self): 3 while true: 4 # 手动结束程序运行 5 str = input("输入exit()结束:") 6 if 'exit()' == str: 7 print('绘制完毕') 8 break
到此这篇关于python实战之一步一步教你绘制小猪佩奇的文章就介绍到这了,更多相关python绘制小猪佩奇内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: python如何读取.mtx文件