Etch SolutionΒΆ

Here is the first attempt at a solution to make a simple etch a sketch program using events and the turtle. It has the same global variable issues as our first mouse click program yesterday, but it will work.

Lets start by looking at a general outline of the program, as that is a good example of the way that many event driven programs operate.

  1. Initialize the turtle and the window objects
  2. Set up colors and speeds and initialize a couple of constants
  3. Write the callback functions to handle the events we want to handle.
  4. Bind our callback functions to the events.
  5. Enter the event loop

The solution above is done to very closely approximate the real etch-a-sketch. Your left hand moves the point left and right while your right hand moves the point up and down. Drawing a diagonal line is quite tedious! Of course with a keyboard to work with we can really improve our drawing experience. You could modify the program above to use the arrow keys on your keyboard. “Up” and “Down” could allow the turtle to go forward and backward, while “Left” and “Right” could turn the turtle by a few degrees. You may also want to improve the program by adding some key events to allow your users to select a color for the turtle. You could also add a reset key that would erase the current picture.

Just for the sake of completeness for you object oriented programmers following along, here is a version that encapsulates our Etch-a-Sketch inside a class.

The above solution is actually a great example of using inheritance. In the previous post we had a simple class that made a turtle and stored it away as an instance variable. In this case we don’t need an instance variable because we are Making our Etch object a subclass of Turtle. This means that an Etch object can do everything a regular turtle can do but we are giving it some specialized behavior. In particular we are redefining the behavior of up, down, left, and right.

Next Section - Rolling the Dice