CS 200 - Lab 22

Animation

In this lab, you will implement a simple animation using the console window.

Getting Started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download lab22.zip.  Import it into Visual Studio (File->Import...).

Add your code to the source file called Animate.c.

Your task

Here is the main function of the program, which you will not need to change:

struct Scene scene;

scene = create_scene();

while (!animation_is_done(scene)) {
/* clear the off-screen display buffer */
clear_screen();

/* render the scene into the display buffer */
render_scene(scene);

/* copy the display buffer to the display */
cons_update();

/* pause */
sleep_ms(ANIMATION_DELAY);

/* update the scene */
scene = update_scene(scene);
}

move_cursor(22, 0);

return 0;

The idea is simple:

the update_scene function creates an initial Scene and returns it

the animation_is_done function checks the current Scene and returns 1 if the animation has finished, 0 otherwise

the render_scene function takes the current Scene and, using the move_cursor, change_color, and printw functions, renders the current scene into an off-screen display buffer.  (Note: printw works just like printf, except that it prints text into an off-screen display buffer, rather than printing directly into the window.  This eliminates display flickering.)

the update_scene function takes the current Scene and returns an updated Scene in which one or more elements of the Scene move to different locations

Your task is to implement these four functions so that the program shows a simple animation.  One idea is to implement a "bouncing ball" animation, where a text character moves diagonally and bounces off the sides of the screen:

The general idea is that the Scene type should contain fields that record the position (and if appropriate, the direction) of the animated element.  For example, in my bouncing ball implementation, my Scene type is defined this way:

struct Scene {
int x, y;
int dx, dy;
};

The x and y fields record the current column and row of the ball, and the dx and dy fields record the current direction (1 for forward, -1 for backward) for the x and y directions.

You don't have to implement a bouncing ball.  An easier animation is to move the character from the left side of the screen (column 0) to the right (column 79).