基于Python-turtle库绘制皮卡丘、多边形、星空

源码:

  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()
 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)
 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)
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 
169     def colorLeftEar(self, x, y):
170         t = self.t
171         self.noTrace_goto(x, y)
172         t.fillcolor('#000000')
173         t.begin_fill()
174         t.seth(330)
175         t.circle(100, 35)
176         t.seth(219)
177         t.circle(-300, 19)
178         t.seth(110)
179         t.circle(-30, 50)
180         t.circle(-300, 10)
181         t.end_fill()
182         
183 
184     def colorRightEar(self, x, y):
185         t = self.t
186         self.noTrace_goto(x, y)
187         t.fillcolor('#000000')
188         t.begin_fill()
189         t.seth(300)
190         t.circle(-100, 30)
191         t.seth(35)
192         t.circle(300, 15)
193         t.circle(30, 50)
194         t.seth(190)
195         t.circle(300, 17)
196         t.end_fill()
197     
198 
199     def body(self):
200         t = self.t
201 
202         t.fillcolor('#F6D02F')
203         t.begin_fill()
204         # 右脸轮廓
205         t.penup()
206         t.circle(130, 40)
207         t.pendown()
208         t.circle(100, 105)
209         t.left(180)
210         t.circle(-100, 5)
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)
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)
295         t.circle(-100, 10)
296         t.left(180)
297         t.circle(100, 10)
298         t.seth(5)
299         t.circle(100, 10)
300         t.circle(-100, 40)
301         t.circle(100, 35)
302         t.left(180)
303         t.circle(-100, 10)
304 
305         # 右脚
306         t.seth(290)
307         t.circle(100, 55)
308         t.circle(10, 50)
309 
310         t.seth(120)
311         t.circle(100, 20)
312         t.left(180)
313         t.circle(-100, 20)
314 
315         t.seth(0)
316         t.circle(10, 50)
317 
318         t.seth(110)
319         t.circle(100, 20)
320         t.left(180)
321         t.circle(-100, 20)
322 
323         t.seth(30)
324         t.circle(20, 50)
325 
326         t.seth(100)
327         t.circle(100, 40)
328 
329         # 右侧身体轮廓
330         t.seth(200)
331         t.circle(-100, 5)
332         t.left(180)
333         t.circle(100, 5)
334         t.left(30)
335         t.circle(100, 75)
336         t.right(15)
337         t.circle(-300, 21)
338         t.left(180)
339         t.circle(300, 3)
340 
341         # 右手
342         t.seth(43)
343         t.circle(200, 60)
344         
345 
346         t.right(10)
347         t.fd(10)
348 
349         t.circle(5, 160)
350         t.seth(90)
351         t.circle(5, 160)
352         t.seth(90)
353 
354         
355         t.fd(10)
356         t.seth(90)
357         t.circle(5, 180)
358         t.fd(10)
359 
360         t.left(180)
361         t.left(20)
362         t.fd(10)
363         t.circle(5, 170)
364         t.fd(10)
365         t.seth(240)
366         t.circle(50, 30)
367 
368         
369         t.end_fill()
370         self.noTrace_goto(130, 125)
371         t.seth(-20)
372         t.fd(5)
373         t.circle(-5, 160)
374         t.fd(5)
375 
376         # 手指纹
377         self.noTrace_goto(166, 130)
378         t.seth(-90)
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)
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)
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)
505         t.circle(8, 100)
506         t.end_fill()
507         self.noTrace_goto(-168.47, 185.52)
508         t.seth(36)
509         t.circle(-270, 54)
510         t.left(180)
511         t.circle(270, 27)
512         t.circle(-80, 98)
513 
514         t.fillcolor('#444444')
515         t.begin_fill()
516         t.left(180)
517         t.circle(80, 197)
518         t.left(58)
519         t.circle(200, 45)
520         t.end_fill()
521 
522         self.noTrace_goto(-58, 270)
523         t.pencolor('#228B22')
524         t.dot(35)
525 
526         self.noTrace_goto(-30, 280)
527         t.fillcolor('#228B22')
528         t.begin_fill()
529         t.seth(100)
530         t.circle(30, 180)
531         t.seth(190)
532         t.fd(15)
533         t.seth(100)
534         t.circle(-45, 180)
535         t.right(90)
536         t.fd(15)
537         t.end_fill()
538         t.pencolor('#000000')
539 
540 
541 
542 
543     def start(self):
544         self.body()
545 
546 
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()

 

效果图:

在这里插入图片描述

源码:

 1 #SquareSpiral1.py
 2 
 3 import turtle as t
 4 
 5 # t = turtle.Pen()
 6 t.bgcolor("black")
 7 sides=eval(input("输入要绘制的边的数目,请输入2-6的数字!"))
 8 colors=["red","yellow","green","blue","orange","purple"]
 9 for x in range(150):
10     t.pencolor(colors[x%sides])
11     t.forward(x*3/sides+x)
12     t.left(360/sides+1)
13     t.width(x*sides/200)
14 t.exitonclick()
15 print("####结束####")

 

效果图:

在这里插入图片描述
在这里插入图片描述

源码:

 1 from turtle import *
 2 from random import random,randint
 3 screen = Screen()
 4 width ,height = 800,600
 5 screen.setup(width,height)
 6 screen.bgcolor("black")
 7 screen.mode("logo")
 8 screen.delay(0)#这里要设为0,否则很卡
 9 t = Turtle(visible = False,shape='circle')
10 t.pencolor("white")
11 t.fillcolor("white")
12 t.penup()
13 t.setheading(-90)
14 t.goto(width/2,randint(-height/2,height/2))
15 stars = []
16 for i in range(200):
17     star = t.clone()
18     s =random() /3
19     star.shapesize(s,s)
20     star.speed(int(s*10))
21     star.setx(width/2 + randint(1,width))
22     star.sety( randint(-height/2,height/2))
23     star.showturtle()
24     stars.append(star)
25 while True:
26     for star in stars:
27         star.setx(star.xcor() - 3 * star.speed())
28         if star.xcor()<-width/2:
29             star.hideturtle()
30             star.setx(width/2 + randint(1,width))
31             star.sety( randint(-height/2,height/2))
32             star.showturtle()

 

效果图:

在这里插入图片描述

在这里插入图片描述

posted @ 2021-02-15 19:58  BugMiaowu2021  阅读(653)  评论(0)    收藏  举报