-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path小树.py
91 lines (83 loc) · 1.97 KB
/
小树.py
1
2
3
4
5
6
7
8
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
# Time : 2019/4/5 22:27
# Author : Mifen
# Email : [email protected]
# Github : https://github.com/Amd794
import time
import turtle as t
from turtle import *
t.setup(1280,720)
t.speed(0)
t.pensize(1)
length = 3
path = 'X'
angle = 25
up()
color("#262626;")
goto(-600, 300)
write('Author:Mifen', font=("微软雅黑", 18))
goto(-600, 250)
write('E-mail :[email protected]', font=("微软雅黑", 18))
goto(-600, 200)
write('Code :https://github.com/Amd794/Python123', font=("微软雅黑", 18))
goto(-600, -350)
down()
expalnation = {
'F': '画线',
'x': '-',
'+': '逆时针旋转',
'-': '顺时针旋转',
'[': '记录当前位置',
']': '恢复上一个位置',
'a': '上色',
'b': '上色',
'c': '上色'
}
rules = {
'F': 'FF',
'X': 'aF-[b[X]+cX]+aF[c+FX]-X'
}
def draw_path(path, expalnation):
posList, angleList = [], []
t.up()
t.goto(0, -350)
t.down()
t.lt(90)
for symbol in path:
if symbol == 'F':
t.forward(length)
elif symbol == '+':
t.left(angle)
elif symbol == '-':
t.rt(angle)
elif symbol == '[':
posList.append(t.pos())
angleList.append(t.heading())
elif symbol == 'a':
t.pensize(3)
t.color("#8c503c")
elif symbol == 'b':
t.pensize(2)
t.color("#4ab441")
elif symbol == 'c':
t.pensize(2)
t.color("#18b418")
elif symbol == ']':
t.up()
t.home()
t.goto(posList.pop())
t.left(angleList.pop())
t.down()
def apply_rules(path, rules):
L = [_ for _ in path]
for i in range(len(L)):
symbol = L[i]
if symbol == 'F':
L[i] = rules[symbol]
if symbol == 'X':
L[i] = rules[symbol]
path = ''.join(L)
return path
for _ in range(6):
path = apply_rules(path, rules)
draw_path(path, expalnation)