关于python:趣味python-一步一步绘制小猪佩奇

45次阅读

共计 6552 个字符,预计需要花费 17 分钟才能阅读完成。

微信公众号:Python 集中营
简略的事件反复做,反复的事件保持做,保持的事件用心做;
你的必定是我保持的能源,如果这篇文章对你有帮忙,点个关注吧!

定义全局属性
 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 技术栈,材料获取、交换社区、干货分享,期待你的退出~

正文完
 0