Creating an Etch A Sketch App Using Python and Turtle

-

and History of the Etch-a-Sketch Tablet

The Etch-a-Sketch tablet was one of the vital interesting toy creations of the late Nineteen Fifties. The Etch-a-Sketch is largely a tablet look-alike; the red frame has a screen embedded in it and has two knobs. These knobs control the horizontal and vertical movements of a stylus behind the screen. This product quickly became a large success, with over 1 million units sold throughout the first 12 months. It was considered one of the inventions that might keep kids busy making drawings and having fun, and at the identical time, promote cognitive development by enhancing advantageous motor skills, hand-eye coordination, and spatial awareness through knob-controlled sketching. It became so popular that it even got featured in movies like .

Understanding the Project

In this text, we’ll use the Python Turtle Module to develop our own-style, digital version of the Etch-a-Sketch. It is a beginner-to-intermediate-level tutorial, which might require a basic understanding of Python fundamentals akin to Python functions, loops, etc. Through coding this project, we’ll learn Python event handling, coordinates and movements, functions and loops, in addition to consequential visual feedback. Furthermore, we may even understand the concept of instances in object-oriented programming. That is an interesting implementation of the core concept of Python, and a fun strategy to learn programming through a visible project. Let’s start!

Python’s Turtle Module for Visual Coding

With a view to make an Etch-a-Sketch Application, we’ll need a visible representation of our code. That is where Python’s Turtle module comes into play. The turtle module is a component of the Python standard library, and allows one to attract on a 2D coordinate system through easy commands. The module alse supports keyboard input, behaving as a digital pen and thus making it ideal to simulate an Etch-a-Sketch.

The turtle module is predicated on a robotic turtle, which is given some commands that it follows and produces drawings accordingly. To make use of this functionality, we simply must import the module into our code after which use the defined functions, which will be explored from the official documentation here.

The next is just a few lines of code that import the module and use probably the most useful function to attract on the screen:

import turtle
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(45)
turtle.forward(100)
turtle.exitonclick()
Python Turtle Module Basic Functions (Image by Writer)

The forward function is used to maneuver the cursor forward and takes the gap as an argument, whereas the right function turns the turtle’s head to the suitable by the angle that’s given as an argument. More details of every function will be accessed through the official documentation.

The Turtle module also has object-oriented programming capabilities, which implies that we will create objects from a given blueprint. The Turtle and Screen class will be used to create object instances to be utilized in our code. Allow us to create these:

my_pen= Turtle()
my_pen.width(3)
my_pen.speed(0)
screen = Screen()
screen.title("Etch A Sketch")

See how we’ve got created a turtle object and called it my_pen from the Turtle Class that could be a a part of the Python Turtle Module. We’ve got also created the screen object, which can allow us to visualise the pen movement in addition to customise it in accordance with our needs. We’ve got also customised the width and speed of the pen, in addition to named the screen.

Defining the Movement Functions

Next is to define the functions for the movement of our pen. Identical to the physical Etch-a-Sketch tablet, which has 2 knobs, one for the vertical up and down movement, and the second for the horizontal left to right movement, we’ll define a complete of 4 functions:

  1. Move Forwards: It will move the pen upwards, synonymous with the up movement in the unique tablet.
  2. Move Backwards: It will move the pen downwards, synonymous with the down movement in the unique tablet.
  3. Turn Left: It will move the pen to the left by a certain angle.
  4. Turn Right: It will move the pen to the suitable by a certain angle.

Let’s code the above as functions:

def move_forwards():
    my_pen.forward(50)

def move_backwards():
    my_pen.backward(50)

def turn_left():
    new_heading = my_pen.heading() + 10
    my_pen.setheading(new_heading)

def turn_rigth():
    new_heading = my_pen.heading() - 10
    my_pen.setheading(new_heading)

In the primary two functions, we’ve got straightforwardly used the turtle functions of forward and backward. Within the horizontal movement functions, turn_left and turn_right, we’ve got defined a brand new variable new_heading which is largely the angle by which the pen will turn. The new_heading takes the pen’s heading which is the present angle and adds 10 degrees in case of turning left and subtracts 10 degrees within the case of turning right. This angle will probably be stored because the new_heading which can act as an argument to the setheading function that sets the orientation of the pen by the angle given because the argument.

We may even define a function that can clear the screen. This function uses the turtle’s clear function which deletes the turtle’s drawing from the screen without affecting the state and position of the turtle. It’ll also return the pen’s position back to home, by utilizing the penup, home and pendown functions:

def clear_screen():
    my_pen.clear()
    my_pen.penup()
    my_pen.home()
    my_pen.pendown()

Screen Listening

One among the capabilities of the turtle module is that it accommodates screen listening events. In programming, event listening is an idea that detects and responds to user actions. In our case, the user motion will probably be via keyboard, using the WASD keys for the pen’s movement. We are going to use this functionality in our code. This will be completed using the listen and onkey method for the screen object. The listen method is used to gather key events, and the onkey method defines the function to be called in accordance with the actual key that has been pressed.

screen.listen()
screen.onkey(move_forwards, "w")
screen.onkey(move_backwards, "s")
screen.onkey(turn_left, "a")
screen.onkey(turn_rigth, "d")
screen.onkey(clear_screen, "c")

Lastly, since we wish to retain the screen, we’ll use the exitonclick screen method that might keep the screen there until we click on it.

screen.exitonclick()

Etching & Sketching!

Now that our code is complete, we’ll run this system. A screen will appear before us and can remain so until we click anywhere on it.

We are going to use the “W”, “A”, “S” and “D” keys to create drawing and “C” to clear screen. Allow us to draw a circle through the keyboard!

Sketching a Circle throuh keyboard inputs (Image by Writer)

You too can draw a circle just by moving forward with “W” after which turning the left key “A” two times, and continuing to achieve this until the pen reaches its starting position. We may practice drawing shapes and understand geometry all of the while fiddling with this program.

Enhancing the Project

Now that our basic program is made, we will add many other features that might further customise and enhance our creation, akin to:

  • Adding keys and corresponding functions for diagonal movements
  • Changing the colour of the pen
  • Saving the drawings and exporting the canvas as a picture

Conclusion

We’ve got successfully used our basic knowledge of Python and the Turtle module to create an Etch-a-Sketch digital program. It is a fun strategy to learn programming in addition to to grasp the coordinates, as every part is visually displayed. It also makes it easy to indicate any mistakes one makes within the code and debug in a timely manner. Although easy in its code, this sort of program forms the premise of complex graphical programs and software ahead, so a basic understanding of the graphical interface is crucial to understand the foundations of digital graphics.

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x