Monday, August 24, 2009

Chapter 12 -- Listing 12.8

There is a typo in Listing 12.8. In the init method the instance variable is visibleObjects. The b is missing in the text. In addition, the calls to up() and tracer should be added to the drawAll() method as they are in listing 12.4

Here is a complete, corrected version of Listing 12.8


class Canvas:

def __init__(self,w,h):
self.width = w
self.height = h
self.visibleObjects = []
self.turtle = cTurtle.Turtle()
self.turtle.setup(width=self.width,height=self.height)
self.turtle.hideturtle()

def drawAll(self):
self.turtle.reset()
self.turtle.tracer(0)
for shape in self.visibleObjects:
shape._draw(self.turtle)
self.turtle.tracer(1)
self.turtle.hideturtle()

def addShape(self,shape):
self.visibleObjects.append(shape)

def draw(self,gObject):
gObject.setCanvas(self)
gObject.setVisible(True)
self.turtle.up()
self.turtle.tracer(0)
gObject._draw(self.turtle)
self.turtle.tracer(1)
self.addShape(gObject)


No comments:

Post a Comment