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繪制小豬佩奇內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found