PIKACHU TURTLE CODE
PIKACHU TURTLE CODE
py 21-12-2024 14:34
1 import turtle
2
3
4 def getPosition(x, y):
5 turtle.setx(x)
6 turtle.sety(y)
7 print(x, y)
8
9 class Pikachu:
10
11 def __init__(self):
12 self.t = turtle.Turtle()
13 t = self.t
14 t.pensize(3)
15 t.speed(9)
16 t.ondrag(getPosition)
17
18
19
20 def noTrace_goto(self, x, y):
21 self.t.penup()
22 self.t.goto(x, y)
23 self.t.pendown()
24
25 def leftEye(self, x, y):
26 self.noTrace_goto(x, y)
27 t = self.t
28 t.seth(0)
29 t.fillcolor('#333333')
30 t.begin_fill()
31 t.circle(22)
32 t.end_fill()
33
34 self.noTrace_goto(x, y+10)
35 t.fillcolor('#000000')
36 t.begin_fill()
37 t.circle(10)
38 t.end_fill()
39
40 self.noTrace_goto(x+6, y + 22)
41 t.fillcolor('#ffffff')
42 t.begin_fill()
Page 1 of 14
Untitled1.py 21-12-2024 14:34
43 t.circle(10)
44 t.end_fill()
45
46 def rightEye(self, x, y):
47 self.noTrace_goto(x, y)
48 t = self.t
49 t.seth(0)
50 t.fillcolor('#333333')
51 t.begin_fill()
52 t.circle(22)
53 t.end_fill()
54
55 self.noTrace_goto(x, y+10)
56 t.fillcolor('#000000')
57 t.begin_fill()
58 t.circle(10)
59 t.end_fill()
60
61 self.noTrace_goto(x-6, y + 22)
62 t.fillcolor('#ffffff')
63 t.begin_fill()
64 t.circle(10)
65 t.end_fill()
66
67 def mouth(self, x, y):
68 self.noTrace_goto(x, y)
69 t = self.t
70
71 t.fillcolor('#88141D')
72 t.begin_fill()
73 # 下嘴唇
74 l1 = []
75 l2 = []
76 t.seth(190)
77 a = 0.7
78 for i in range(28):
79 a += 0.1
80 t.right(3)
81 t.fd(a)
82 l1.append(t.position())
83
84 self.noTrace_goto(x, y)
Page 2 of 14
Untitled1.py 21-12-2024 14:34
85
86 t.seth(10)
87 a = 0.7
88 for i in range(28):
89 a += 0.1
90 t.left(3)
91 t.fd(a)
92 l2.append(t.position())
93
94 # 上嘴唇
95
96 t.seth(10)
97 t.circle(50, 15)
98 t.left(180)
99 t.circle(-50, 15)
100
101 t.circle(-50, 40)
102 t.seth(233)
103 t.circle(-50, 55)
104 t.left(180)
105 t.circle(50, 12.1)
106 t.end_fill()
107
108 # 舌头
109 self.noTrace_goto(17, 54)
110 t.fillcolor('#DD716F')
111 t.begin_fill()
112 t.seth(145)
113 t.circle(40, 86)
114 t.penup()
115 for pos in reversed(l1[:20]):
116 t.goto(pos[0], pos[1]+1.5)
117 for pos in l2[:20]:
118 t.goto(pos[0], pos[1]+1.5)
119 t.pendown()
120 t.end_fill()
121
122 # 鼻子
123 self.noTrace_goto(-17, 94)
124 t.seth(8)
125 t.fd(4)
126 t.back(8)
Page 3 of 14
Untitled1.py 21-12-2024 14:34
127
128 # 红脸颊
129 def leftCheek(self, x, y):
130 turtle.tracer(False)
131 t = self.t
132 self.noTrace_goto(x, y)
133 t.seth(300)
134 t.fillcolor('#DD4D28')
135 t.begin_fill()
136 a = 2.3
137 for i in range(120):
138 if 0 <= i < 30 or 60 <= i < 90:
139 a -= 0.05
140 t.lt(3)
141 t.fd(a)
142 else:
143 a += 0.05
144 t.lt(3)
145 t.fd(a)
146 t.end_fill()
147 turtle.tracer(True)
148
149 def rightCheek(self, x, y):
150 t = self.t
151 turtle.tracer(False)
152 self.noTrace_goto(x, y)
153 t.seth(60)
154 t.fillcolor('#DD4D28')
155 t.begin_fill()
156 a = 2.3
157 for i in range(120):
158 if 0 <= i < 30 or 60 <= i < 90:
159 a -= 0.05
160 t.lt(3)
161 t.fd(a)
162 else:
163 a += 0.05
164 t.lt(3)
165 t.fd(a)
166 t.end_fill()
167 turtle.tracer(True)
168
Page 4 of 14
Untitled1.py 21-12-2024 14:34
Page 5 of 14
Untitled1.py 21-12-2024 14:34
211
212 # 右耳朵
213 t.seth(20)
214 t.circle(300, 30)
215 t.circle(30, 50)
216 t.seth(190)
217 t.circle(300, 36)
218
219 # 上轮廓
220 t.seth(150)
221 t.circle(150, 70)
222
223 # 左耳朵
224 t.seth(200)
225 t.circle(300, 40)
226 t.circle(30, 50)
227 t.seth(20)
228 t.circle(300, 35)
229 #print(t.pos())
230
231 # 左脸轮廓
232 t.seth(240)
233 t.circle(105, 95)
234 t.left(180)
235 t.circle(-105, 5)
236
237 # 左手
238 t.seth(210)
239 t.circle(500, 18)
240 t.seth(200)
241 t.fd(10)
242 t.seth(280)
243 t.fd(7)
244 t.seth(210)
245 t.fd(10)
246 t.seth(300)
247 t.circle(10, 80)
248 t.seth(220)
249 t.fd(10)
250 t.seth(300)
251 t.circle(10, 80)
252 t.seth(240)
Page 6 of 14
Untitled1.py 21-12-2024 14:34
253 t.fd(12)
254 t.seth(0)
255 t.fd(13)
256 t.seth(240)
257 t.circle(10, 70)
258 t.seth(10)
259 t.circle(10, 70)
260 t.seth(10)
261 t.circle(300, 18)
262
263 t.seth(75)
264 t.circle(500, 8)
265 t.left(180)
266 t.circle(-500, 15)
267 t.seth(250)
268 t.circle(100, 65)
269
270 # 左脚
271 t.seth(320)
272 t.circle(100, 5)
273 t.left(180)
274 t.circle(-100, 5)
275 t.seth(220)
276 t.circle(200, 20)
277 t.circle(20, 70)
278
279 t.seth(60)
280 t.circle(-100, 20)
281 t.left(180)
282 t.circle(100, 20)
283 t.seth(300)
284 t.circle(10, 70)
285
286 t.seth(60)
287 t.circle(-100, 20)
288 t.left(180)
289 t.circle(100, 20)
290 t.seth(10)
291 t.circle(100, 60)
292
293 # 横向
294 t.seth(180)
Page 7 of 14
Untitled1.py 21-12-2024 14:34
Page 8 of 14
Untitled1.py 21-12-2024 14:34
Page 9 of 14
Untitled1.py 21-12-2024 14:34
379 t.fd(3)
380 t.circle(-4, 180)
381 t.fd(3)
382 t.seth(-90)
383 t.fd(3)
384 t.circle(-4, 180)
385 t.fd(3)
386
387 # 尾巴
388 self.noTrace_goto(168, 134)
389 t.fillcolor('#F6D02F')
390 t.begin_fill()
391 t.seth(40)
392 t.fd(200)
393 t.seth(-80)
394 t.fd(150)
395 t.seth(210)
396 t.fd(150)
397 t.left(90)
398 t.fd(100)
399 t.right(95)
400 t.fd(100)
401 t.left(110)
402 t.fd(70)
403 t.right(110)
404 t.fd(80)
405 t.left(110)
406 t.fd(30)
407 t.right(110)
408 t.fd(32)
409
410 t.right(106)
411 t.circle(100, 25)
412 t.right(15)
413 t.circle(-300, 2)
414 ##############
415 #print(t.pos())
416 t.seth(30)
417 t.fd(40)
418 t.left(100)
419 t.fd(70)
420 t.right(100)
Page 10 of 14
Untitled1.py 21-12-2024 14:34
421 t.fd(80)
422 t.left(100)
423 t.fd(46)
424 t.seth(66)
425 t.circle(200, 38)
426 t.right(10)
427 t.fd(10)
428 t.end_fill()
429
430
431
432 # 尾巴花纹
433 t.fillcolor('#923E24')
434 self.noTrace_goto(126.82, -156.84)
435 t.begin_fill()
436
437 t.seth(30)
438 t.fd(40)
439 t.left(100)
440 t.fd(40)
441 t.pencolor('#923e24')
442 t.seth(-30)
443 t.fd(30)
444 t.left(140)
445 t.fd(20)
446 t.right(150)
447 t.fd(20)
448 t.left(150)
449 t.fd(20)
450 t.right(150)
451 t.fd(20)
452 t.left(130)
453 t.fd(18)
454 t.pencolor('#000000')
455 t.seth(-45)
456 t.fd(67)
457 t.right(110)
458 t.fd(80)
459 t.left(110)
460 t.fd(30)
461 t.right(110)
462 t.fd(32)
Page 11 of 14
Untitled1.py 21-12-2024 14:34
463 t.right(106)
464 t.circle(100, 25)
465 t.right(15)
466 t.circle(-300, 2)
467 t.end_fill()
468
469
470 # 帽子、眼睛、嘴巴、脸颊
471 self.cap(-134.07, 147.81)
472 self.mouth(-5, 25)
473 self.leftCheek(-126, 32)
474 self.rightCheek(107, 63)
475 self.colorLeftEar(-250, 100)
476 self.colorRightEar(140, 270)
477 self.leftEye(-85, 90)
478 self.rightEye(50, 110)
479 t.hideturtle()
480
481
482
483 def cap(self, x, y):
484 self.noTrace_goto(x, y)
485 t = self.t
486 t.fillcolor('#CD0000')
487 t.begin_fill()
488 t.seth(200)
489 t.circle(400, 7)
490 t.left(180)
491 t.circle(-400, 30)
492 t.circle(30, 60)
493 t.fd(50)
494 t.circle(30, 45)
495 t.fd(60)
496 t.left(5)
497 t.circle(30, 70)
498 t.right(20)
499 t.circle(200, 70)
500 t.circle(30, 60)
501 t.fd(70)
502 # print(t.pos())
503 t.right(35)
504 t.fd(50)
Page 12 of 14
Untitled1.py 21-12-2024 14:34
Page 13 of 14
Untitled1.py 21-12-2024 14:34
547
548 def main():
549 print('Painting the Pikachu... ')
550 turtle.screensize(800, 600)
551 turtle.title('Pikachu')
552 pikachu = Pikachu()
553 pikachu.start()
554 turtle.mainloop()
555
556
557 if __name__ == '__main__':
558 main()
Page 14 of 14