Skip to content

Commit 6ee2a2e

Browse files
committed
Python Turtle examples
1 parent ecdc3ea commit 6ee2a2e

12 files changed

+795
-0
lines changed

2canvases.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from turtle import TurtleScreen, RawTurtle, TK
2+
3+
4+
def main():
5+
root = TK.Tk()
6+
cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff")
7+
cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee")
8+
cv1.pack()
9+
cv2.pack()
10+
11+
s1 = TurtleScreen(cv1)
12+
s1.bgcolor(0.85, 0.85, 1)
13+
s2 = TurtleScreen(cv2)
14+
s2.bgcolor(1, 0.85, 0.85)
15+
16+
p = RawTurtle(s1)
17+
q = RawTurtle(s2)
18+
19+
p.color("red", (1, 0.85, 0.85))
20+
p.width(3)
21+
q.color("blue", (0.85, 0.85, 1))
22+
q.width(3)
23+
24+
for t in p, q:
25+
t.shape("turtle")
26+
t.lt(36)
27+
28+
q.lt(180)
29+
30+
for t in p, q:
31+
t.begin_fill()
32+
for i in range(5):
33+
for t in p, q:
34+
t.fd(50)
35+
t.lt(72)
36+
for t in p, q:
37+
t.end_fill()
38+
t.lt(54)
39+
t.pu()
40+
t.bk(50)
41+
42+
return "EVENTLOOP"
43+
44+
45+
if __name__ == '__main__':
46+
main()
47+
TK.mainloop() # keep window open until user closes it

fractals.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
from turtle import *
2+
from time import sleep, perf_counter as clock
3+
4+
5+
class CurvesTurtle(Pen):
6+
# example derived from
7+
# Turtle Geometry: The Computer as a Medium for Exploring Mathematics
8+
# by Harold Abelson and Andrea diSessa
9+
# p. 96-98
10+
def hilbert(self, size, level, parity):
11+
if level == 0:
12+
return
13+
# rotate and draw first subcurve with opposite parity to big curve
14+
self.left(parity * 90)
15+
self.hilbert(size, level - 1, -parity)
16+
# interface to and draw second subcurve with same parity as big curve
17+
self.forward(size)
18+
self.right(parity * 90)
19+
self.hilbert(size, level - 1, parity)
20+
# third subcurve
21+
self.forward(size)
22+
self.hilbert(size, level - 1, parity)
23+
# fourth subcurve
24+
self.right(parity * 90)
25+
self.forward(size)
26+
self.hilbert(size, level - 1, -parity)
27+
# a final turn is needed to make the turtle
28+
# end up facing outward from the large square
29+
self.left(parity * 90)
30+
31+
# Visual Modeling with Logo: A Structural Approach to Seeing
32+
# by James Clayson
33+
# Koch curve, after Helge von Koch who introduced this geometric figure in 1904
34+
# p. 146
35+
def fractalgon(self, n, rad, lev, dir):
36+
import math
37+
38+
# if dir = 1 turn outward
39+
# if dir = -1 turn inward
40+
edge = 2 * rad * math.sin(math.pi / n)
41+
self.pu()
42+
self.fd(rad)
43+
self.pd()
44+
self.rt(180 - (90 * (n - 2) / n))
45+
for i in range(n):
46+
self.fractal(edge, lev, dir)
47+
self.rt(360 / n)
48+
self.lt(180 - (90 * (n - 2) / n))
49+
self.pu()
50+
self.bk(rad)
51+
self.pd()
52+
53+
# p. 146
54+
def fractal(self, dist, depth, dir):
55+
if depth < 1:
56+
self.fd(dist)
57+
return
58+
self.fractal(dist / 3, depth - 1, dir)
59+
self.lt(60 * dir)
60+
self.fractal(dist / 3, depth - 1, dir)
61+
self.rt(120 * dir)
62+
self.fractal(dist / 3, depth - 1, dir)
63+
self.lt(60 * dir)
64+
self.fractal(dist / 3, depth - 1, dir)
65+
66+
67+
def main():
68+
ft = CurvesTurtle()
69+
70+
ft.reset()
71+
ft.speed(0)
72+
ft.ht()
73+
ft.getscreen().tracer(1, 0)
74+
ft.pu()
75+
76+
size = 6
77+
ft.setpos(-33*size, -32*size)
78+
ft.pd()
79+
80+
ta = clock()
81+
ft.fillcolor("red")
82+
ft.begin_fill()
83+
ft.fd(size)
84+
85+
ft.hilbert(size, 6, 1)
86+
87+
# frame
88+
ft.fd(size)
89+
for i in range(3):
90+
ft.lt(90)
91+
ft.fd(size*(64+i % 2))
92+
ft.pu()
93+
for i in range(2):
94+
ft.fd(size)
95+
ft.rt(90)
96+
ft.pd()
97+
for i in range(4):
98+
ft.fd(size*(66+i % 2))
99+
ft.rt(90)
100+
ft.end_fill()
101+
tb = clock()
102+
res = "Hilbert: %.2fsec. " % (tb-ta)
103+
104+
sleep(3)
105+
106+
ft.reset()
107+
ft.speed(0)
108+
ft.ht()
109+
ft.getscreen().tracer(1, 0)
110+
111+
ta = clock()
112+
ft.color("black", "blue")
113+
ft.begin_fill()
114+
ft.fractalgon(3, 250, 4, 1)
115+
ft.end_fill()
116+
ft.begin_fill()
117+
ft.color("red")
118+
ft.fractalgon(3, 200, 4, -1)
119+
ft.end_fill()
120+
tb = clock()
121+
res += "Koch: %.2fsec." % (tb-ta)
122+
return res
123+
124+
125+
if __name__ == '__main__':
126+
msg = main()
127+
print(msg)
128+
mainloop()

hanoi.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from turtle import *
2+
3+
4+
class Disc(Turtle):
5+
def __init__(self, n):
6+
Turtle.__init__(self, shape="square", visible=False)
7+
self.pu()
8+
self.shapesize(1.5, n*1.5, 2) # square-->rectangle
9+
self.fillcolor(n/6., 0, 1-n/6.)
10+
self.st()
11+
12+
13+
class Tower(list):
14+
"Hanoi tower, a subclass of built-in type list"
15+
16+
def __init__(self, x):
17+
"create an empty tower. x is x-position of peg"
18+
self.x = x
19+
20+
def push(self, d):
21+
d.setx(self.x)
22+
d.sety(-150+34*len(self))
23+
self.append(d)
24+
25+
def pop(self):
26+
d = list.pop(self)
27+
d.sety(150)
28+
return d
29+
30+
31+
def hanoi(n, from_, with_, to_):
32+
if n > 0:
33+
hanoi(n-1, from_, to_, with_)
34+
to_.push(from_.pop())
35+
hanoi(n-1, with_, from_, to_)
36+
37+
38+
def play():
39+
onkey(None, "space")
40+
clear()
41+
try:
42+
hanoi(6, t1, t2, t3)
43+
write("press STOP button to exit",
44+
align="center", font=("Courier", 16, "bold"))
45+
except Terminator:
46+
pass # turtledemo user pressed STOP
47+
48+
49+
def main():
50+
global t1, t2, t3
51+
ht()
52+
penup()
53+
goto(0, -225) # writer turtle
54+
t1 = Tower(-250)
55+
t2 = Tower(0)
56+
t3 = Tower(250)
57+
# make tower of 6 discs
58+
for i in range(6, 0, -1):
59+
t1.push(Disc(i))
60+
# prepare spartanic user interface ;-)
61+
write("press spacebar to start game",
62+
align="center", font=("Courier", 16, "bold"))
63+
onkey(play, "space")
64+
listen()
65+
return "EVENTLOOP"
66+
67+
68+
if __name__ == "__main__":
69+
msg = main()
70+
print(msg)
71+
mainloop()

main.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from turtle import *
2+
color('red', 'yellow')
3+
begin_fill()
4+
while True:
5+
forward(200)
6+
left(170)
7+
if abs(pos()) < 1:
8+
break
9+
end_fill()
10+
done()

peace.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from turtle import *
2+
3+
4+
def main():
5+
peacecolors = ("red3", "orange", "yellow",
6+
"seagreen4", "orchid4",
7+
"royalblue1", "dodgerblue4")
8+
9+
reset()
10+
Screen()
11+
up()
12+
goto(-320, -195)
13+
width(70)
14+
15+
for pcolor in peacecolors:
16+
color(pcolor)
17+
down()
18+
forward(640)
19+
up()
20+
backward(640)
21+
left(90)
22+
forward(66)
23+
right(90)
24+
25+
width(25)
26+
color("white")
27+
goto(0, -170)
28+
down()
29+
30+
circle(170)
31+
left(90)
32+
forward(340)
33+
up()
34+
left(180)
35+
forward(170)
36+
right(45)
37+
down()
38+
forward(170)
39+
up()
40+
backward(170)
41+
left(90)
42+
down()
43+
forward(170)
44+
up()
45+
46+
goto(0, 300) # vanish if hideturtle() is not available ;-)
47+
return "Done!"
48+
49+
50+
if __name__ == "__main__":
51+
main()
52+
mainloop()

0 commit comments

Comments
 (0)