Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson15-diy1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
014b6244
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
783d936d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
22 deletions
my_tree.py
my_tree.py
View file @
014b6244
import
turtle
# 导入turtle模块
import
random
# 导入random模块
# 分形树
def
tree
(
n
):
if
pen
=
turtle
.
Turtle
()
# 创建画笔
pen
.
color
(
'sienna'
)
# 画笔颜色
pen
.
speed
(
0
)
...
...
@@ -14,27 +18,6 @@ pen.up() # 朝向正上方
pen
.
backward
(
150
)
# 后退150
pen
.
down
()
# 朝向正下方
# 树枝
def
tree
(
n
):
# 画树枝
if
n
>=
0
:
# 设置终止条件,if条件判断,n的值大于等于0,才执行以下代码
if
n
<=
12
:
# if条件判读,n的值小于等于12,才执行以下代码
color_list
=
[
'snow'
,
'lightcoral'
]
color
=
random
.
choice
(
color_list
)
pen
.
color
(
color
)
else
:
pen
.
color
(
'sienna'
)
# 赭(zhe)色
pen
.
pensize
(
n
/
10
)
# 树枝长度除以10设置为画笔大小
pen
.
forward
(
n
)
# 向前移动
angle
=
random
.
random
()
# 角度angle
pen
.
right
(
30
*
angle
)
# 角度angle乘30作为向右旋转度数
length
=
1.5
*
random
.
random
()
# 长度length
tree
(
n
-
10
*
length
)
# 树枝长度减10乘长度lenght
pen
.
left
(
60
*
angle
)
# 角度angle乘60作为向左旋转度数
tree
(
n
-
10
*
lenght
)
pen
.
right
(
30
*
angle
)
# 角度angle乘30作为向右旋转度数
pen
.
up
()
# 画笔抬起
pen
.
backward
(
n
)
# 原路返回
pen
.
down
()
# 画笔落下
tree
(
60
)
# 设置树枝长度60
turtle
.
done
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment