Skip to content

Commit 6a1e58b

Browse files
committed
Updating turtle programs
1 parent a17f113 commit 6a1e58b

File tree

6 files changed

+180
-166
lines changed

6 files changed

+180
-166
lines changed

code/flower.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
try:
11-
# see if Swampy is installed as a package
12-
from swampy.TurtleWorld import *
13-
except ImportError:
14-
# otherwise see if the modules are on the PYTHONPATH
15-
from TurtleWorld import *
12+
from __future__ import print_function, division
13+
14+
import turtle
1615

17-
from polygon import *
16+
from polygon import arc
1817

1918

2019
def petal(t, r, angle):
@@ -26,7 +25,7 @@ def petal(t, r, angle):
2625
"""
2726
for i in range(2):
2827
arc(t, r, angle)
29-
lt(t, 180-angle)
28+
t.lt(180-angle)
3029

3130

3231
def flower(t, n, r, angle):
@@ -39,21 +38,19 @@ def flower(t, n, r, angle):
3938
"""
4039
for i in range(n):
4140
petal(t, r, angle)
42-
lt(t, 360.0/n)
41+
t.lt(360.0/n)
4342

4443

4544
def move(t, length):
4645
"""Move Turtle (t) forward (length) units without leaving a trail.
4746
Leaves the pen down.
4847
"""
49-
pu(t)
50-
fd(t, length)
51-
pd(t)
48+
t.pu()
49+
t.fd(length)
50+
t.pd()
5251

5352

54-
world = TurtleWorld()
55-
bob = Turtle()
56-
bob.delay = 0.01
53+
bob = turtle.Turtle()
5754

5855
# draw a sequence of three flowers, as shown in the book.
5956
move(bob, -100)
@@ -65,9 +62,5 @@ def move(t, length):
6562
move(bob, 100)
6663
flower(bob, 20, 140.0, 20.0)
6764

68-
die(bob)
69-
70-
# dump the contents of the campus to the file canvas.eps
71-
world.canvas.dump()
72-
73-
wait_for_user()
65+
bob.hideturtle()
66+
turtle.mainloop()

code/koch.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
try:
11-
# see if Swampy is installed as a package
12-
from swampy.TurtleWorld import *
13-
except ImportError:
14-
# otherwise see if the modules are on the PYTHONPATH
15-
from TurtleWorld import *
12+
from __future__ import print_function, division
13+
14+
import turtle
1615

1716

1817
def koch(t, n):
1918
"""Draws a koch curve with length n."""
20-
if n<3:
21-
fd(t, n)
19+
if n < 10:
20+
t.fd(n)
2221
return
23-
m = n/3.0
22+
m = n/3
2423
koch(t, m)
25-
lt(t, 60)
24+
t.lt(60)
2625
koch(t, m)
27-
rt(t, 120)
26+
t.rt(120)
2827
koch(t, m)
29-
lt(t, 60)
28+
t.lt(60)
3029
koch(t, m)
3130

3231

3332
def snowflake(t, n):
3433
"""Draws a snowflake (a triangle with a Koch curve for each side)."""
3534
for i in range(3):
3635
koch(t, n)
37-
rt(t, 120)
36+
t.rt(120)
3837

3938

40-
world = TurtleWorld()
41-
bob = Turtle()
42-
bob.delay = 0
43-
44-
bob.x = -150
45-
bob.y = 90
46-
bob.redraw()
39+
bob = turtle.Turtle()
4740

41+
bob.pu()
42+
bob.goto(-150, 90)
43+
bob.pd()
4844
snowflake(bob, 300)
4945

50-
world.mainloop()
46+
turtle.mainloop()
47+

code/letters.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
from swampy.TurtleWorld import *
12+
from __future__ import print_function, division
13+
14+
import turtle
1115

1216
from polygon import circle, arc
1317

14-
# LEVEL 0 PRIMITIVES are provided by World.py.
15-
# They include fd, bk, lt, rt, pu and pd
18+
# LEVEL 0 PRIMITIVES
19+
# fd, bk, lt, rt, pu and pd
20+
21+
def fd(t, length):
22+
t.fd(length)
23+
24+
def bk(t, length):
25+
t.bk(length)
1626

27+
def lt(t, angle=90):
28+
t.lt(angle)
29+
30+
def rt(t, angle=90):
31+
t.rt(angle)
32+
33+
def pd(t):
34+
t.pd()
35+
36+
def pu(t):
37+
t.pu()
1738

1839
# LEVEL 1 PRIMITIVES are simple combinations of Level 0 primitives.
1940
# They have no pre- or post-conditions.
@@ -35,7 +56,7 @@ def skip(t, n):
3556
pd(t)
3657

3758
def stump(t, n, angle=90):
38-
"""make a vertical line and leave the turtle at the top, facing right"""
59+
"""Makes a vertical line and leave the turtle at the top, facing right"""
3960
lt(t)
4061
fd(t, n)
4162
rt(t, angle)
@@ -53,20 +74,19 @@ def hollow(t, n):
5374
# location and direction.
5475

5576
def post(t, n):
56-
"""make a vertical line and return to the original position"""
77+
"""Makes a vertical line and return to the original position"""
5778
lt(t)
5879
fdbk(t, n)
5980
rt(t)
6081

6182
def beam(t, n, height):
62-
"""make a horizontal line at the given height and return."""
83+
"""Makes a horizontal line at the given height and return."""
6384
hollow(t, n*height)
6485
fdbk(t, n)
6586
hollow(t, -n*height)
6687

67-
6888
def hangman(t, n, height):
69-
"""make a vertical line to the given height and a horizontal line
89+
"""Makes a vertical line to the given height and a horizontal line
7090
at the given height and then return.
7191
This is efficient to implement, and turns out to be useful, but
7292
it's not so semantically clean."""
@@ -77,7 +97,7 @@ def hangman(t, n, height):
7797
rt(t)
7898

7999
def diagonal(t, x, y):
80-
"""make a diagonal line to the given x, y offsets and return"""
100+
"""Makes a diagonal line to the given x, y offsets and return"""
81101
from math import atan2, sqrt, pi
82102
angle = atan2(y, x) * 180 / pi
83103
dist = sqrt(x**2 + y**2)
@@ -90,7 +110,7 @@ def vshape(t, n, height):
90110
diagonal(t, n/2, height*n)
91111

92112
def bump(t, n, height):
93-
"""make a bump with radius n at height*n
113+
"""Makes a bump with radius n at height*n
94114
"""
95115
stump(t, n*height)
96116
arc(t, n/2.0, 180)
@@ -264,15 +284,13 @@ def draw_(t, n):
264284
skip(t, n)
265285

266286
if __name__ == '__main__':
267-
world = TurtleWorld()
268287

269288
# create and position the turtle
270289
size = 20
271-
bob = Turtle()
272-
bob.delay = 0.01
290+
bob = turtle.Turtle()
273291

274292
for f in [draw_h, draw_e, draw_l, draw_l, draw_o]:
275293
f(bob, size)
276294
skip(bob, size)
277295

278-
wait_for_user()
296+
turtle.mainloop()

code/pie.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
import math
12+
from __future__ import print_function, division
1113

12-
try:
13-
# see if Swampy is installed as a package
14-
from swampy.TurtleWorld import *
15-
except ImportError:
16-
# otherwise see if the modules are on the PYTHONPATH
17-
from TurtleWorld import *
14+
import math
15+
import turtle
1816

1917

2018
def draw_pie(t, n, r):
@@ -25,9 +23,9 @@ def draw_pie(t, n, r):
2523
r: length of the radial spokes
2624
"""
2725
polypie(t, n, r)
28-
pu(t)
29-
fd(t, r*2 + 10)
30-
pd(t)
26+
t.pu()
27+
t.fd(r*2 + 10)
28+
t.pd()
3129

3230

3331
def polypie(t, n, r):
@@ -40,7 +38,7 @@ def polypie(t, n, r):
4038
angle = 360.0 / n
4139
for i in range(n):
4240
isosceles(t, r, angle/2)
43-
lt(t, angle)
41+
t.lt(angle)
4442

4543

4644
def isosceles(t, r, angle):
@@ -54,33 +52,28 @@ def isosceles(t, r, angle):
5452
"""
5553
y = r * math.sin(angle * math.pi / 180)
5654

57-
rt(t, angle)
58-
fd(t, r)
59-
lt(t, 90+angle)
60-
fd(t, 2*y)
61-
lt(t, 90+angle)
62-
fd(t, r)
63-
lt(t, 180-angle)
55+
t.rt(angle)
56+
t.fd(r)
57+
t.lt(90+angle)
58+
t.fd(2*y)
59+
t.lt(90+angle)
60+
t.fd(r)
61+
t.lt(180-angle)
6462

6563

66-
# create the world and bob
67-
world = TurtleWorld()
68-
bob = Turtle()
69-
bob.delay = 0
70-
pu(bob)
71-
bk(bob, 130)
72-
pd(bob)
64+
bob = turtle.Turtle()
65+
66+
bob.pu()
67+
bob.bk(130)
68+
bob.pd()
7369

7470
# draw polypies with various number of sides
7571
size = 40
7672
draw_pie(bob, 5, size)
7773
draw_pie(bob, 6, size)
7874
draw_pie(bob, 7, size)
7975
draw_pie(bob, 8, size)
80-
die(bob)
81-
82-
# dump the contents of the campus to the file canvas.eps
83-
world.canvas.dump()
8476

85-
wait_for_user()
77+
bob.hideturtle()
78+
turtle.mainloop()
8679

0 commit comments

Comments
 (0)