CS1101C Lab 4 (Even Week)

Ball Animation

The deadline for this lab question is Sunday 8 April 2007, 23:59:59 hours.

The name of your C program file must be called ball.c, files with any other name will not be marked.

Background

The purpose of this lab is to familiarize you with the notion of 2d arrays. There are two main objectives:

  1. Declaration of 2D arrays
  2. Simple processing on 2D arrays

Ball Animation

To show moving pictures (animation) on a computer is relatively simple process. Each stage of an animation is actually a standstill picture, also known as frame. If we can show each of these frames fast enough, human eyes will be tricked into seeing motion.

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:

  1. Show the current screen.
  2. Ask user for command (move ball in one of the four directions or exit)
  3. Move the ball accordingly if the user is not exiting
  4. Go back to step 1 if the command is not “exit”.
  5. Print the last screen into output file “ball.txt”.

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].

Sample Runs

Assuming that the executable is ball, one sample execution is shown below. Note that comments preceding with “//” are there to explain the output.

$ 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.
.....................
.....................
.....................
.....................
.....................
.....................
.....................
.....................
.*.*.................
..*..................
.*.*.................
.....................
.....................
.....................
.....................
.....................
.....................
.....................
.....................
.....................
.....................
$

Hints and Tips

Here's some suggestions on the possible functions you can write. You are free to come up with your own design. However, you must modularize your program.

There some crucial decisions that will make your program easier/harder:

Ponders


This document, index.html, has been accessed 19 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

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.