Skip to content

Commit dcfcf7b

Browse files
committed
add turtle sample
1 parent b2d7068 commit dcfcf7b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

samples/gui/turtle/rect.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 导入turtle包的所有内容:
2+
from turtle import *
3+
4+
# 设置笔刷宽度:
5+
width(4)
6+
7+
# 前进:
8+
forward(200)
9+
# 右转90度:
10+
right(90)
11+
12+
# 笔刷颜色:
13+
pencolor('red')
14+
forward(100)
15+
right(90)
16+
17+
pencolor('green')
18+
forward(200)
19+
right(90)
20+
21+
pencolor('blue')
22+
forward(100)
23+
right(90)
24+
25+
# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
26+
done()

samples/gui/turtle/stars.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from turtle import *
2+
3+
def drawStar(x, y):
4+
pu()
5+
goto(x, y)
6+
pd()
7+
# set heading: 0
8+
seth(0)
9+
for i in range(5):
10+
fd(40)
11+
rt(144)
12+
13+
for x in range(0, 250, 50):
14+
drawStar(x, 0)
15+
16+
done()

samples/gui/turtle/tree.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from turtle import *
2+
3+
colormode(255)
4+
5+
lt(90)
6+
7+
lv = 14
8+
l = 120
9+
s = 45
10+
11+
width(lv)
12+
13+
r = 0
14+
g = 0
15+
b = 0
16+
pencolor(r, g, b)
17+
18+
penup()
19+
bk(l)
20+
pendown()
21+
fd(l)
22+
23+
def draw_tree(l, level):
24+
global r, g, b
25+
# save the current pen width
26+
w = width()
27+
28+
# narrow the pen width
29+
width(w * 3.0 / 4.0)
30+
# set color:
31+
r = r + 1
32+
g = g + 2
33+
b = b + 3
34+
pencolor(r % 200, g % 200, b % 200)
35+
36+
l = 3.0 / 4.0 * l
37+
38+
lt(s)
39+
fd(l)
40+
41+
if level < lv:
42+
draw_tree(l, level + 1)
43+
bk(l)
44+
rt(2 * s)
45+
fd(l)
46+
47+
if level < lv:
48+
draw_tree(l, level + 1)
49+
bk(l)
50+
lt(s)
51+
52+
# restore the previous pen width
53+
width(w)
54+
55+
speed("fastest")
56+
57+
draw_tree(l, 4)
58+
59+
done()

0 commit comments

Comments
 (0)