Uh oh!
There was an error while loading. Please reload this page.
forked from AllenDowney/ThinkPython
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathThreader.py
More file actions
Latest commit
executable file
·69 lines (50 loc) · 1.63 KB
/
Copy pathThreader.py
File metadata and controls
executable file
·69 lines (50 loc) · 1.63 KB
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
"""This code is for an exercise from
Think Python: An Introduction to Software Design
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
This program requires Gui.py, which is part of
Swampy; you can download it from thinkpython.com/swampy.
"""
fromswampy.GuiimportCallable
fromswampy.TurtleWorldimportTurtle, TurtleWorld
classThreader(Turtle):
def__init__(self, world):
Turtle.__init__(self, world)
self.delay=0.005
self.set_color('purple')
defstep():
"""Threaders don't need no stinkin' step method.
See http://en.wikipedia.org/wiki/Stinking_badges
"""
defmoveto(self, x, y):
"""Teleports to the given coordinates and redraws."""
self.x=x
self.y=y
self.redraw()
defkoch(self, n):
"""Draws a Koch curve with length n.
See http://en.wikipedia.org/wiki/Koch_snowflake
"""
ifn<8:
self.fd(n)
return
foranglein [-60, 120, -60, 0]:
self.koch(n/3.0)
self.rt(angle)
defsnowflake(self):
"""Draws a Koch snowflake."""
foriinrange(3):
self.koch(300)
self.rt(120)
self.undraw()
defmake_threader(world):
"""Creates a Threader and makes it draw a snowflake."""
t=Threader(world)
t.moveto(-150, 90)
# TODO: modify this so it runs in a new thread
t.snowflake()
world=TurtleWorld()
world.setup_interactive()
# add a button that calls make_threader
world.bu(text='Make Threader', command=Callable(make_threader, world))
world.mainloop()