Wednesday, January 28, 2009

Chapter 13 -- Etch

Well here it is, the first bug report I've received from a reader. Thanks to Gregor Lingl (The same guy who wrote the excellent xturtle module that we based cTurtle on).

Listing 13.4 is wrong. Listing 13.4 provides a class that inherits from Turtle and provides simple onKey callbacks for turning left, right, forward and backward. These callbacks should turn a default amount or go back or forward a default number of spaces.

To avoid a crazy recursion the callbacks were named left5 and right5 in the working version of the code, but somehow the 5 got left off in the published listing.

Here's a complete, tested and working version of listing 13.4


import sys
import os
from cTurtle import Turtle, mainloop

class Etch(Turtle):
defaultDist = 5
def __init__(self):
super(Etch, self).__init__()
self.color('blue')
self.pensize(2)
self.speed(0)
self.distance = 5
self.turn = 5
self.onKey(self.forward,"Up")
self.onKey(self.bkwd,"Down")
self.onKey(self.left5,"Left")
self.onKey(self.right5,"Right")
self.onKey(self.quit,"q")
self.listen()
self.main()

def forward(self,distance=defaultDist):
super(Etch,self).forward(distance)

def bkwd(self):
self.backward(self.distance)

def left5(self):
self.left(self.turn)

def right5(self):
self.right(self.turn)

def quit(self):
sys.exit()

def main(self):
mainloop()

if __name__ == '__main__':
etch = Etch()

1 comment:

  1. Professor Miller,

    While frustratedly trying to install Python on my MacBook so as to take our first quiz before midnight (and failing miserably), I noticed a typo on page 453 of the textbook. The link to the installation tutorial should be as follows:

    http://wiki.python.org/moin/BeginnersGuide/Download

    The textbook references "main" rather than "moin" in the web address.

    I hope I can get this figured out before the quiz closes... see you tomorrow.

    ReplyDelete