C++ Assignment 10
Etch-A-Sketch

Background:

The Etch-A-Sketch is a plastic tablet that lets you make designs by turning knobs that control a point (called a stylus). The left and right knobs control horizontal and vertical rods, moving the stylus where the two rods meet. When the stylus moves, it scrapes the screen, which is coated with a mixture of aluminum powder and plastic beads, leaving the line you see. The first Etch-A-Sketch was sold in 1960. Through television advertising it went on to become one of the most popular toys ever made.

In this assignment you will create an electronic version of the Etch-A-Sketch. This program will give you practice with switch statements, if statements and graphics.

Selection in C++

Being able to choose between choices is an important part of programming. In C++ there are two different ways of making a selection: if-else and switch. The if-else is the most commonly used of the two. It is used to select conditionally one of two possible outcomes. The syntax for the if-else statement follows:

if (Condition)
  Statement1
else
  Statement2

Here's a fragment of code that displays "Positive":


int nNum = 3;
if (nNum > 0)
     cout<<"Positive"<<endl;
else if (nNum < 0)
     cout<<"Negative"<<endl;
else
     cout<<"Zero"<<endl;

Notice that there is no condition with an else. Just as with loops, if you want more than one line of code to be controlled by an if or an else, make a block of code surrounded by curly braces {} .

If you have only a single statement that you want to execute conditionally, you can leave off the else part, as shown in the following example:


if (nNum == 4)
  cout<<"Four"<<endl;

Similar to the if-else, the switch statement is specifically designed to work with a list of many choices. Here's a fragment of code that displays "Average":


char cGrade = 'C';
switch (cGrade) 
{
  case 'A':
    cout<<"Excellent"<<endl;
    break;
  case 'B':
    cout<<"Good"<<endl;
    break;
  case 'C':
    cout<<"Average"<<endl;
    break;
  case 'D':
    cout<<"Needs Improvement"<<endl;
    break;
  default:
    cout<<"Needs Lots Of Improvement"<<endl;

}

Note that switch only works with variables that can be represented by integers. You can switch on ints, chars and colors, but not doubles, strings or conditions. Each case ends in a colon. Also note the break; at the end of each case.

Since this is a larger program than the previous assignments, you may want to complete it in steps.

Step One:

It is a good idea to start by writing the code that allows you to "draw". We can use the putpixel function from winbgim.h to draw one dot at a time on the screen. The function takes three integer arguments, the first two give the coordinates where the point is drawn and the third the color that is used: putpixel(nX, nY, nColor);

Here is some pseudocode for the main function:

  1. Open a graphics window.
  2. Declare three local variables: two integers for the X & Y coordinates of the stylus and a character variable to hold the Key pressed by the user
  3. Do while the key pressed is not 'q'
    1. Use getch() to store the key pressed in the local character variable
    2. Switch on the key pressed.
      1. The key pressed means left
        1. decrease the X coordinate by one
      2. The key pressed means right
        1. increase the X coordinate by one
      3. The key pressed means up
        1. increase the Y coordinate by one
      4. The key pressed means down
        1. decrease the Y coordinate by one
      5. The key pressed means clear the screen
        1. Call the cleardevice() function to erase the screen
    3. draw a point at the current coordinates.
  4. use closegraph(); to close the graphics window
  5. return 0

Step Two:

Once you have the basic program working you can add features and graphics to make your program more interesting. Have fun and be creative! Your etch-a-sketch doesn't have to look like any other. Here are some ideas.

To make a red border that looks like a traditional etch-a-sketch you can use the bar function from winbgim.h. The bar function takes four arguments that are the coordinates for the four corners of a rectangle in the order left, top, right, bottom. To make a solid red bar acrss the top of the screen try:


	setfillstyle(1,RED);
   	bar(0,0,640,50);

You can use three more calls to the bar function to finish your border.

To make the white knobs at the two bottom corners of the screen the fillellipse function from winbgim.h works well. To make a knob in the lower left of the screen try:


	setfillstyle(1,WHITE);
     	fillellipse(25, 455, 15, 15);
Regular text doesn't work well with graphics. A better way to place words on the screen with instructions for the user is to use outtextxy from winbgim.h.

	settextstyle(5, 0, 2);
	//takes three arguments, the first is font style, last is size
	settextjustify(1, 1);
	setcolor(WHITE);
	outtextxy(320, 20, "Etch-A-Sketch");
	//takes three arguments, the first two are coordinates and the third a string

It's a good idea to place all the code that clears the screen, draws the border and displays the instructions in the same function.

Extensions:

If you have extra time you can add an option that allow the user to change the current color. Another idea might be to add "mirrors" that draw a symmetrical design at different regions on the screen. You could also change the thickness of the line by making several calls to putpixel. If you are really good you may be able to get the knobs to appear to turn as you draw!

Thanks to Jarrett Liang for his help with this assignment