The name of your C program file must be called ball.c, files with any other name will not be marked.
In this lab, we will try something similar. Imagine we have a “screen” of 21 rows and 21 columns. Each “point” on screen can be identified by [row][column], where the top left corner is [0][0] and the top right corner is [0][20]. We are going to “animate” an image of ball on this screen. As mentioned, to animate something, we need multiple “frames” of the image. In this case, the ball has two simple frames, as shown below:
Frame One:
* *** *
Frame Two:
* * * * *
Since each “pixel” of the ball is just a character “*”, we can represent both the screen and the ball frames as a two dimensional character array in C.
Now that we have a way to represent the screen and ball, let's us write a program to allow the user moves the ball on screen in four directions (up, down, left, right). Every time the we move the ball image, the frame used will be changed to the next frame. Loop back to frame one after frame two. The screen is initially filled with “.”.
So, the basic flow of your program is as follows:
You can assume that at the start of the program, the ball is shown using frame one. The center point of the ball is at the position [10][10].
$ ball ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..........*.......... .........***......... // ball center at [10][10]. ..........*.......... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 1 // move to left. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ........*.*.......... .........*........... // ball center at [10][9]. frame two. ........*.*.......... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 1 // move to left again. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ........*............ .......***........... // ball center at [10][8]. frame one. ........*............ ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 1 // multiple moves omitted. // keep moving left. output omitted. // .... // eventually. 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 1 // move to left. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... *.*.................. .*................... // ball center at [10][1]. frame two. *.*.................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 1 // try to move left again. Cannot move ball in that direction! // already at the edge! ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... *.*.................. .*................... // ball not moved. frame two. *.*.................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 2 //move to right ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..*.................. .***................. // ball center at [10][2]. frame one. ..*.................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 3 //move upward ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... .*.*................. ..*.................. // ball center at [9][2]. frame two. .*.*................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 3 //move upward ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..*.................. .***................. // ball center at [8][2]. frame one. ..*.................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 4 //move downward ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... .*.*................. ..*.................. // ball center at [9][2]. frame two. .*.*................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... 1.Left 2.Right 3.Up 4.Down 5.Exit ==> 5 Final screen written into output file. $ less -e ball.txt // check the content of output file. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... .*.*................. ..*.................. .*.*................. ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... ..................... $
There some crucial decisions that will make your program easier/harder:
A total of 13 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.