Education STEM

A Very Simple Etch A Sketch Type Program (Python)

Here’s a fun, little application using Python. In this post, I demonstrate drawing lines using just the arrow keys on the keyboard (up/down/left/right). The drawing direction is determined by the arrow key pressed as shown in the clip below:

sketch animation

With each keypress a line is drawn in the direction of the arrow head, which is determined by the arrow key pressed by the user on the keyboard. This is accomplished by using heading() to get current heading and setheading() to set a new heading in turtle library of Python. The key presses are bound to my custom functions up(), down(), right(), left() as shown in the snippet below:

Where t is the instance of Turtle: t=Turtle()

The custom up() function for example is:

The fd method moves the turtle pointer forward by that many pixels. We ensure before moving it forward however that it’s heading up! If it’s not (e.g. was drawing right or down or left previously), then we turn it to face north and then move forward thereby drawing a vertical line from current position to upward.

One of the coolest features of Turtle is Undo. We can control its drawing by telling to backtrack and remove previously drawn shapes. The End button in keyboard is bound to t.undo()

NOTE: For simplicity of this example, I removed any dependencies on additional buttons so it’s just mapped to End key like the rest of the commands. The following clip shows Undo feature in action where you can see the lines getting erased each time I press End key to Undo which is cumulative, so 1 press will remove 1 previous line, 2 presses will remove 2 previous lines, and so on:


For more thorough explanation and full code, please see below.



This post is not meant to be a formal tutorial, instead it is  to offer key concepts, and approaches to problem-solving. Basic->Intermediate technical/coding knowledge is assumed. If you like more info or source file to learn/use, or need more basic assistance, you may contact me at tanman1129 at hotmail dot com. To support this voluntary effort, you can donate via Paypal from the button in my Home page, or become a Patron via my Patreon site. A supporter/patron gets direct answers on any of my blogs including code/data/source files whenever possible. At the very least, it’ll encourage me to share more posts/tips/knowledge in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top