-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWobbler.py
71 lines (53 loc) · 1.69 KB
/
Wobbler.py
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
69
70
71
"""
This module is part of an exercise for
Think Python: an Introduction to Software Design
Allen B. Downey
"""
from swampy.TurtleWorld import *
from random import randint
class Wobbler(Turtle):
"""a Wobbler is a kind of Turtle with attributes for speed and
clumsiness."""
def __init__(self, world, speed=1, clumsiness=60, color='red'):
Turtle.__init__(self, world)
self.delay = 0
self.speed = speed
self.clumsiness = clumsiness
self.color = color
# move to the starting position
self.pu()
self.rt(randint(0, 360))
self.bk(150)
def step(self):
"""step is invoked by TurtleWorld on every Wobbler, once
per time step."""
self.steer()
self.wobble()
self.move()
def move(self):
"""move forward in proportion to self.speed"""
self.fd(self.speed)
def wobble(self):
"""make a random turn in proportion to self.clumsiness"""
dir = randint(0, self.clumsiness) - randint(0, self.clumsiness)
self.rt(dir)
def steer(self):
"""steer the Wobbler in the general direction it should go.
Postcondition: the Wobbler's heading may be changed, but
its position may not."""
self.rt(10)
def make_world(constructor):
# create TurtleWorld
world = TurtleWorld()
world.delay = .01
world.setup_run()
# make three Wobblers with different speed and clumsiness attributes
colors = ['orange', 'green', 'purple']
i = 1.0
for color in colors:
t = constructor(world, i, i * 30, color)
i += 0.5
return world
if __name__ == '__main__':
world = make_world(Wobbler)
world.mainloop()