Python之turtle库画各种有趣的图及源码(更新中)

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

1、安卓小人

  1 #!/usr/bin/env python
  2 import turtle
  3 aj=turtle.Pen()
  4 y=0
  5 aj.speed(5)
  6 #turtle.screensize(200,800)
  7 turtle.bgcolor("black")
  8 #aj.shape("turtle")
  9 def head():
 10     aj.color("green")
 11     aj.fd(160)
 12     x=aj.xcor()
 13     aj.seth(90)
 14     aj.begin_fill()
 15     #aj.color("green")
 16     aj.circle(x/2,180)
 17     aj.end_fill()
 18     aj.penup()
 19     aj.goto(33,37)
 20     aj.pendown()
 21     aj.dot(13,"black")
 22     aj.penup()
 23     aj.goto(126,37)
 24     aj.pendown()
 25     aj.dot(13,"black")
 26     aj.penup()
 27     aj.home()
 28     aj.pendown()
 29     aj.hideturtle()
 30     aj.fd(160)
 31     aj.seth(90)
 32     aj.circle(x/2,60)
 33     aj.right(90)
 34     aj.pensize(5)
 35     aj.fd(30)
 36 
 37     aj.penup()
 38     aj.home()
 39     #aj.pendown()
 40     aj.hideturtle()
 41     aj.fd(160)
 42     aj.seth(90)
 43     aj.circle(x/2,120)
 44     aj.right(90)
 45     aj.pensize(5)
 46     aj.pendown()
 47     aj.fd(30)
 48     aj.penup()
 49     aj.home()
 50     aj.penup()
 51 
 52 def body():
 53     aj.pensize(0)
 54 
 55     aj.home()
 56     aj.showturtle()
 57     aj.goto(0,-7)
 58     aj.pendown()
 59     aj.begin_fill()
 60     aj.fd(160)
 61     aj.right(90)
 62     aj.fd(120)
 63     aj.right(90)
 64     aj.fd(160)
 65     y=aj.ycor()
 66     aj.right(90)
 67     aj.fd(120)
 68     aj.end_fill()
 69 
 70 def legs():
 71     aj.penup()
 72     #turtle.color("red")
 73     aj.goto(33,-169)
 74     aj.pendown()
 75     aj.pensize(32)
 76     aj.fd(43)
 77     aj.penup()
 78     aj.goto(130,-169)
 79     aj.pendown()
 80     aj.fd(43)
 81     aj.penup()
 82 
 83 def hands():
 84     aj.home()
 85     aj.pensize(30)
 86     aj.goto(-18,-77)
 87     aj.pendown()
 88     aj.left(90)
 89     aj.fd(65)
 90     aj.penup()
 91     aj.goto(179,-77)
 92     aj.pendown()
 93     aj.fd(65)
 94     aj.penup()
 95     aj.hideturtle
 96     aj.fd(100)
 97     aj.hideturtle()
 98     aj.circle(100)
 99     aj.circle(100,360,59)
100     aj.reset()
101     turtle.bgcolor("black")
102     turtle.pencolor("green")
103     turtle.hideturtle()
104     turtle.goto(-300,0)
105     aj.hideturtle
106     turtle.write("Thank you for watching....", font = ("Bodoni MT Black", 28, "bold"))
107     turtle.penup()
108     turtle.goto(-40,-170)
109     turtle.pendown()
110     turtle.pencolor("yellow")
111     turtle.write("Developed by 一个超会写Bug的安太狼", font = ("Palatino Linotype", 22, "bold"))
112 
113 head()
114 body()
115 legs()
116 hands()
117 turtle.done()

效果图:
在这里插入图片描述

2、龙形曲线(Dragon Curve)

又叫分形龙,是一种自相似碎形曲线的统称,因形似龙的蜿蜒盘曲而得名。

 1 # -*- coding: utf-8 -*-
 2 
 3 from turtle import *
 4 length = 5
 5 angle  = 90
 6 setup(1280,720)
 7 up()
 8 goto(300,-100)
 9 
10 down()
11 def draw_path(path):
12     for symbol in path:
13         if symbol == 'f':
14             import random
15             colormode(255)
16             color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
17             fd(length)
18         elif symbol == '-':
19             lt(angle)
20         elif symbol == '+':
21             rt(angle)
22 
23 def apply_path(rules,path):
24     lit = [x for x in path]
25     for i in range(len(lit)):
26         symbol = lit[i]
27         if symbol == 'x':
28             lit[i] = rules[symbol]
29         elif symbol == 'y':
30             lit[i] = rules[symbol]
31     path = ''.join(lit)
32     return path
33 
34 rules = {
35     'x':'x+yf+',
36     'y':'-fx-y'
37 }
38 path = 'fx'
39 speed(0)
40 for i in range(13):
41     path = apply_path(rules,path)
42 draw_path(path)
43 done()

效果图:
在这里插入图片描述

3、樱桃树

 1 # -*- coding: utf-8 -*-
 2 
 3 import turtle
 4 
 5 toplevel = 8  # 一共递归6层
 6 angle = 30
 7 rangle = 15
 8 
 9 
10 def drawTree(length, level):
11     turtle.left(angle)  # 绘制左枝
12     turtle.color("black")
13     turtle.forward(length)
14 
15     if level == toplevel:  # 叶子
16         turtle.color("pink")
17         turtle.circle(2, 360)
18 
19     if level < toplevel:  # 在左枝退回去之前递归
20         drawTree(length - 10, level + 1)
21     turtle.back(length)
22 
23     turtle.right(angle + rangle)  # 绘制右枝
24     turtle.color("black")
25     turtle.forward(length)
26 
27     if level == toplevel:  # 叶子
28         turtle.color("pink")
29         turtle.circle(2, 360)
30 
31     if level < toplevel:  # 在右枝退回去之前递归
32         drawTree(length - 10, level + 1)
33         turtle.color("black")
34     turtle.back(length)
35     turtle.left(rangle)
36 
37 
38 turtle.left(90)
39 turtle.penup()
40 turtle.back(300)
41 turtle.pendown()
42 turtle.forward(100)
43 turtle.speed('fastest')
44 drawTree(80, 1)
45 
46 turtle.done()

效果图:
在这里插入图片描述

4、科赫雪花

 1 import turtle as t
 2 from turtle import *
 3 import random
 4 
 5 def draw_path(path):
 6     t.colormode(255)
 7     t.color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
 8     for symbol in path:
 9         if symbol == 'F':
10             forward(length)
11         elif symbol == '-':
12             right(angle)
13         elif symbol == '+':
14             left(angle)
15 
16 def apply_rule(path):
17     rule = 'F+F--F+F'
18     return path.replace('F',rule)
19 
20 length = .5
21 angle  = 60
22 setup(1280,720)
23 bgcolor('black')
24 up()
25 color("#0fe6ca")
26 goto(0,0)
27 down()
28 path = 'F--F--F'
29 speed(0)
30 up()
31 goto(-440,-250)
32 down()
33 for i in range(5):
34     path = apply_rule(path)
35 draw_path(path)
36 draw_path(path)
37 draw_path(path)
38 a,b = pos()
39 for i in range(3):
40     up()
41     a += 250
42     goto(a,b)
43     down()
44     draw_path(path)
45     draw_path(path)
46     draw_path(path)
47 b += 220
48 for i in range(2):
49     up()
50     a -= 250
51     goto(a,b)
52     down()
53     draw_path(path)
54     draw_path(path)
55     draw_path(path)
56 b += 220
57 for i in range(2):
58 
59     draw_path(path)
60     draw_path(path)
61     draw_path(path)
62     up()
63     a += 130
64     goto(a,b)
65     down()

 

效果图:
在这里插入图片描述

5、视觉冲击1

 1 import turtle as t
 2 from turtle import *
 3 
 4 
 5 angle = 60 #通过改变角度,绘制出各种多边形
 6 t.setup(1280,720)
 7 t.bgcolor('black')
 8 t.pensize(2)
 9 randomColor = ['red','blue','green','purple','gold','pink']
10 t.speed(0)
11 for i in range(600):
12       t.color(randomColor[i%6])
13       t.fd(i)
14       t.rt(angle+1)
15 up()
16 color("#0fe6ca")
17 goto(0,0)
18 down()
19 t.done()

效果图:
在这里插入图片描述

6、视觉冲击2

 1 # -*- coding: utf-8 -*-
 2 
 3 import turtle as t
 4 from turtle import *
 5 
 6 angle = 60 #通过改变角度,绘制出各种多边形
 7 t.bgcolor('black')
 8 t.pensize(2)
 9 randomColor = ['red','blue','green','purple','gold','pink']
10 t.speed(0)
11 for i in range(200):
12       t.color(randomColor[i%6])
13       t.circle(i)
14       t.rt(angle+1)
15 up()
16 color("#0fe6ca")
17 goto(0,0)
18 down()

效果图:
在这里插入图片描述

7、视觉冲击3

 1 from turtle import *
 2 import time
 3 
 4 speed(0)
 5 colormode(255)
 6 clrs = ["MidnightBlue", "Navy", "DarkBlue", "MediumBlue", "RoyalBlue", "MediumSlateBlue", "CornflowerBlue",
 7         "DodgerBlue", "DeepskyBlue", "LightSkyBlue", "SkyBlue", "LightBlue"]
 8 
 9 time.sleep(2)
10 
11 for j in range(120):
12 
13     cn = 0
14     c = 30
15     f = 70
16 
17     for i in range(12):
18         pencolor(clrs[cn])
19         circle(c)
20         left(90)
21         penup()
22         forward(f)
23         right(90)
24         pendown()
25         c = c * 0.8
26         f = f * 0.8
27         circle(c)
28         cn = cn + 1
29 
30     penup()
31     goto(0, 0)
32     forward(5)
33     right(3)
34     pendown()

效果图:
在这里插入图片描述

8、希尔伯特曲线:

 1 # -*- coding: utf-8 -*-
 2 
 3 from turtle import *
 4 import random
 5 length = 10
 6 angle  = 90
 7 setup(1280,720)
 8 up()
 9 
10 goto(-640,-360)
11 down()
12 def draw_path(path):
13     for symbol in path:
14         if symbol == 'f':
15             colormode(255)
16             color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
17             fd(length)
18         elif symbol == '+':
19             lt(angle)
20         elif symbol == '-':
21             rt(angle)
22 
23 def apply_path(rules,path):
24     lit = [x for x in path]
25     for i in range(len(lit)):
26         symbol = lit[i]
27         if symbol == 'x':
28             lit[i] = rules[symbol]
29         elif symbol == 'y':
30             lit[i] = rules[symbol]
31     path = ''.join(lit)
32     return path
33 
34 rules = {
35     'x':'+yf-xfx-fy+',
36     'y':'-xf+yfy+fx-'
37 }
38 path = 'x'
39 speed(0)
40 for i in range(7):
41     path = apply_path(rules,path)
42 draw_path(path)
43 done()

效果图:
在这里插入图片描述

9、Sierpiński箭头曲线

 1 # -*- coding: utf-8 -*-
 2 
 3 
 4 from turtle import *
 5 length = 5
 6 angle = -60
 7 setup(1280,720)
 8 up()
 9 
10 goto(-640,-350)
11 down()
12 def draw_path(path):
13     for symbol in path:
14         if symbol == 'A' or symbol == 'B':
15             import random
16             colormode(255)
17             color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
18             forward(length)
19         elif symbol == '-':
20             right(angle)
21         elif symbol == '+':
22             left(angle)
23     ht()
24 
25 def apply_rules(path,rules):
26     lit = [_ for _ in path]
27     for i in range(len(lit)):
28         symbol = lit[i]
29         if symbol in rules:
30             lit[i] = rules[symbol]
31     path = ''.join(lit)
32     return path
33 
34 
35 rules = {
36     'A':'B-A-B',
37     'B':'A+B+A'
38 }
39 path = 'A'
40 speed(0)
41 for i in range(7):
42     path = apply_rules(path,rules)
43 draw_path(path)
44 up()
45 goto(0,-340)
46 angle = 60
47 down()
48 draw_path(path)
49 up()
50 goto(0,-340)
51 angle = -60
52 down()
53 draw_path(path)

效果图:
在这里插入图片描述

10、Koch曲线

 1 # -*- coding: utf-8 -*-
 2 
 3 from turtle import *
 4 import random
 5 length = 2
 6 angle  = 90
 7 setup(1280,720)
 8 up()
 9 goto(-600,-350)
10 down()
11 def draw_path(path):
12     for symbol in path:
13         if symbol == 'F':
14             colormode(255)
15             color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
16             forward(length)
17         elif symbol == '-':
18             right(angle)
19         elif symbol == '+':
20             left(angle)
21     ht()
22 
23 def apply_rule(path):
24     rule = 'F+F-F-F+F'
25     return path.replace('F',rule)
26 
27 path = 'F'
28 speed(0)
29 for i in range(5):
30     path = apply_rule(path)
31 for i in range(5):
32     draw_path(path)
33 up()
34 goto(-478,-228)
35 down()
36 for i in range(4):
37     draw_path(path)
38 up()
39 goto(-356,-106)
40 down()
41 for i in range(3):
42     draw_path(path)
43 up()
44 goto(-235,16)
45 down()
46 for i in range(2):
47     draw_path(path)
48 up()
49 goto(-115,137)
50 down()
51 draw_path(path)

效果图:
在这里插入图片描述

11、月亮代表我的心

  1 # -*- coding: utf-8 -*-
  2 
  3 from turtle import *
  4 import time
  5 import turtle as t
  6 
  7 
  8 def gotopos(x, y):
  9     up()
 10     goto(x, y)
 11     down()
 12     ht()
 13 
 14 
 15 def author():
 16     pensize(2)
 17     gotopos(610, -315)
 18     lt(-90)
 19     fd(80)
 20     pensize(1)
 21     lt(-270)
 22  
 23 
 24 def apply_rules(path, rules):
 25     L = [_ for _ in path]
 26     for i in range(len(L)):
 27         symbol = L[i]
 28         if symbol == 'F':
 29             L[i] = rules[symbol]
 30         if symbol == 'X':
 31             L[i] = rules[symbol]
 32     path = ''.join(L)
 33     return path
 34 
 35 
 36 def draw_path(path):
 37     posList, angleList = [], []
 38     for symbol in path:
 39         if symbol == 'F':
 40             t.forward(length)
 41         elif symbol == '+':
 42             t.left(angle)
 43         elif symbol == '-':
 44             t.rt(angle)
 45         elif symbol == '[':
 46             posList.append(t.pos())
 47             angleList.append(t.heading())
 48         elif symbol == 'a':
 49             t.pensize(3)
 50             t.color("#867b68")
 51         elif symbol == 'b':
 52             t.pensize(2)
 53             t.color("#867b68")
 54         elif symbol == 'c':
 55             t.pensize(2)
 56             t.color("#867b68")
 57         elif symbol == ']':
 58             t.up()
 59             t.home()
 60             t.goto(posList.pop())
 61             t.left(angleList.pop())
 62             t.down()
 63 
 64 
 65 def writez(x, y, str_, size=56, font="华文行楷"):
 66     gotopos(x, y)
 67     write(str_, font=(font, size))
 68 
 69 
 70 setup(1280, 800)
 71 speed(5)
 72 bgcolor("#9c917f")
 73 color("#afa697")
 74 begin_fill()
 75 gotopos(0, -400)
 76 circle(400)
 77 end_fill()
 78 author()
 79 color("#7d776d")
 80 s = "愿天化作比翼鸟"
 81 s2 = "在地愿为连理枝"
 82 for i in range(len(s)):
 83     writez(560, 350 - i * 50, s[i], 36)
 84 for i in range(len(s2)):
 85     writez(460, 350 - i * 50, s2[i], 36)
 86 color("#888475")
 87 writez(-50, 100, "")
 88 writez(-50, 40, "")
 89 writez(-160, 0, "", 96)
 90 writez(-50, 0, "", 176)
 91 writez(33, -30, "", 62)
 92 writez(-18, -95, "", 78)
 93 writez(-213, -210, "", 196)
 94 
 95 gotopos(249, -26)
 96 color("#867b68")
 97 speed(0)
 98 gotopos(-650, -100)
 99 length = 6
100 path = 'F'
101 angle = 27
102 rules = {
103     'F': 'aFF[b-F++F][c+F--F]c++F--F',
104     'X': 'aFF+[b+F]+[c-F]'
105 }
106 
107 for _ in range(4):
108     path = apply_rules(path, rules)
109 draw_path(path)
110 gotopos(570, -330)
111 done()

效果图:
在这里插入图片描述

12、生则同寝 死则同穴

 1 # -*- coding: utf-8 -*-
 2 
 3 
 4 from turtle import *
 5 import random
 6 import time
 7 
 8 str_ = """
 9 守一段情 念一个人。
10 时光不老 我们不散。
11 厮守终生 不离不弃。
12 天暗下来 你就是光。
13 亡魂溺海 止于终老。
14 生死挈阔 与子成说。
15 柔情似水 佳期如梦。
16 我中有你 你中有我。
17 青山不老 为雪白头。
18 心若向阳 无畏悲伤。
19 一人一心 白首不离。
20 心如荒岛 囚我终老。
21 我的世界 只有你懂。
22 你若安好 便是晴天。
23 心有灵犀 一点就通。
24 厮守海角 非你不娶。
25 执子的手 漫漫的走。
26 执子之手 与子偕老。
27 山河拱手 为君一笑。
28 红尘初妆 山河无疆。
29 千秋功名 一世葬你。
30 既不回头 何必不忘。
31 既然无缘 何须誓言。
32 今日种种 似水无痕。
33 明夕何夕 君已陌路。
34 才会相思 便害相思。
35 人来人往 繁华似锦。
36 回首万年 情衷伊人。
37 生能尽欢 死亦无憾。
38 执手若无 泪溅花上。
39 花开花落 人世无常。
40 入我心者 待以君王。
41 为醉而醉 似醉非醉。
42 伤心鸿影 爱已惘然。
43 只要你要 只要我有。
44 日久生情 日久情疏。
45 忧佳相随 风雨无悔。
46 有生之年 誓死娇宠
47 引喻山河 指日可诚。
48 水上鸳鸯 云中翡翠。
49 天荒地老 海誓山盟。
50 生则同寝 死则同穴。
51 生有此女 夫复何求""".split("")
52 setup(1280,720)  # 设置窗口大小
53 colormode(255)  # 使用的颜色模式, 整数还是小数
54 up()
55 a, b = -500, 280
56 goto(a,b)
57 bgcolor("black")
58 
59 
60 down()
61 def w(str_,b):
62     bgcolor( random.randint(0,255),random.randint(0,255),random.randint(0,255))  # 随机生成RGB值, 每次调用函数改变背景颜色
63     for i in range(len(str_)):
64         up()
65         goto(a+100*i,b)
66         down()
67         size =  random.randint(12,68)  # 随机字体大小
68         color( random.randint(0,255),random.randint(0,255),random.randint(0,255))  # 随机字体颜色
69         write(str_[i], align="center",font=("楷体",size))
70 
71         
72 for k in range(4):
73     for i in range(7):
74         w(str_[i+7*k],b-100*i)
75     reset()  # 清屏
76 
77     
78 for i in range(7):
79     w(str_[i+7*4],b-100*i)
80 up()
81 color("#262626;")
82 goto(-600,300)
83 write('Author:Mifen',font=("微软雅黑", 18))
84 goto(-600,250)
85 write('E-mail :8*******7346@qq.com',font=("微软雅黑", 18))
86 goto(-600, 200)
87 write('Code :https://github.com/Amd794/Python123', font=("微软雅黑", 18))
88 goto(-600,-350)
89 down()
90 ht()

 

部分效果图:
在这里插入图片描述

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

未完待续

posted @ 2021-02-09 03:35  BugMiaowu2021  阅读(1986)  评论(0编辑  收藏  举报