Classwork 5: Animated ASCII Art (optional)
To play with printing to the terminal screen.
We can use printf statements to produce some "animated" ASCII art, like this one of an inch worm making its way across the screen: inch.avi.
To accomplish this, we are really printing these two strings over and over:
_____ (5 underscore characters)
_/\_(Note: to specify a backslash character in printf use 2 backslashes "\\".)
For this to work, each time we print, we want to start over at the top left corner of the window. We also want to erase what we printed before. Fortunately, most terminal emulators understand VT100 escape sequences. For example, if we print the string "\033[2J" to the screen, the screen is "cleared" (all characters in the window are erased). Also, the escape sequence "\033[H" moves the cursor to the "home" position --- the top left corner of the screen.
- The complete set of VT100 escape sequences can be found here.
- Wikipedia has an article on the DEC VT100.
Thus, we just have to include these two statements each time we print the worm to the screen:
printf("\033[2J"); // clear screen printf("\033[H"); // go home
Finally, we need one more tool. Our program will be too fast if we simply printed the two inch worm strings to the screen. (We will only see the inchworm's last position.) We need a way to pause our program between steps. This can be accomplished by the usleep function. The following statement will pause the program for 350,000 microseconds (about 1/3 of a second).
Be creative and use printf to create some animated ASCII art to the screen. For example, you could have two inch worms "racing" each other. Here's an example of an inch worm "eating" something it encountered: eat.avi.
When you are done submit your program by:
submit cs104_chang cw05 myart.c
Be sure to logout completely when you have finished!