Activity 12-3 - (Optional) Animation - Creating a Ping Pong Game


This part of the program is for Extra Credit and it may add up to 3 additional points to your current grade (up to 2 point for Ex 12.4 and up to 1 additional extra point if you take the extra challenge)

The program that you will write can be considered the beginning of a program for the video game PONG. The program has to perform the animation of a bouncing ball using the EZwindows library. While at a first the program seems challenging if it is your first program with animation, I encourage you to follow one by one the following steps.

Exercise 12.5

1.      Create a project called “yournamePong”

2.      Add to the project all the *.h files and the Ezwindows library as you have done in the previous project. Set the properties to be sure that the*.h files and the library are located by the compiler.

3.      Add to the project a new .cpp file. Call it,  yournamePong.cpp. This file will contain the ApiMain function that starts the animation. See the Lawn.cpp file to see how the ApiMain is created and prepare the skeleton of the ApiMain function. Now you are ready to fill the function.

4.      The first thing you will do in the main function is to add the statement srand(time(0));    This instruction initializes a random number generator based on the current time. The function srand is in the cstdlib library and the function time is in the ctime library. So be sure you include it in your file.

5.      Create two constants, one for the length and one for the width of the window and set them to 15.0f and 9.0f respectively.

6.     Create a window with a title yournamePong, with the defined width and length and open the window. (Check here if your program is working appropriately so far. Before doing so, you must implement step 12 and 13 to close the program.). If the program is working, the continue with the next steps.

7.     Create the green rectangle of the game in the window. Observe the constructor of the rectangle that you have to use and find the appropriate coordinates and other parameters that are required.

     RectangleShape(SimpleWindow &w, float XCoord, float YCoord,const color &c = Red, float Length = 1.0f, float Width = 2.0f);

8.      Draw the rectangle. Again, check if your program is running so far and that you see the green rectangle in the window.

9.      Now it is time to create the ball. To minimize the number of details required in this program,  the ball will be rectangular. It will be easy to replace a rectangle with a circle in the future. We desire that the ball starts from a different position on the left hand side of the green rectangle, every time we start the program. Therefore, we have compute the coordinates of the position of the ball before creating the ball. The X coordinate should be on the left hand side of the green rectangle, the Y coordinate can be computed as the height of the green windows multiplied by (rand()%100))/100.0; Create the ball.

10.  Now draw the ball. It is time to check again that your program is running and that you see the green rectangle and a ball on the left hand side of the rectangle.

11.  Now the ball begins moving to the right. Before writing this part of the code, let us understand how the animation is performed. For simplicity, the ball moves only horizontally (only the X coordinate of the ball changes) and by steps. Each step must be small. Animation is done by drawing the ball and erasing the ball in sequence.  Note that the class RectangleShape contains the method Draw( ) to draw the rectangle, and a method Erase( ) to erase a rectangle. When the ball is erased from the green rectangle, the green rectangle must be redrawn. You have to draw and erase the ball very many times before the game is over. Since the computer is very fast in doing this, you must ask the computer to kill some time to get a better visual result. When the ball reaches the right hand side of the rectangle, the ball changes direction. This means that is necessary to continue drawing and erasing the ball, but this time the ball is redrawn to the left of the previous position, instead of its right. As you understand this requires to work with conditionals and loops. Now that you have read this part let’s go in detail to implement this section:

a.       First declare a variable that contain the size of a step on the X axis (Xstep). To let the ball go to the right, set the step to 1% of the length of the green window.

b.      In order to simulate the ball bouncing horizontally, create a loop that repeats the process of moving the ball as many times you want. (You can adjust the values to your preferred ones when you see the ball bouncing). Inside the loop perform the following steps:

c.       The loop is finally terminated.

12.  Conclude the program by adding the usual code required to maintain the window open.

13.  Finally close the window.

14.  Run and test your program. Play with the values of the loop and see what happens when you modify them. Can you make the ball go faster or slower? Can you keep the game running longer?

15. When you have completed your work and satisfied with your results, Zip the folder "yournamePong" and attach it to the e-mail that you will send to the instructor to submit your Lab.

For extra challenge (one additional extra credit point): In the real game of PONG, the ball has vertical as well as horizontal motion.  Just as Xstep controls the horizontal motion, you will need a value Ystep to control the vertical motion. You will also need to change Ystep whenever the ball hits the top or bottom wall.  Update your program to include these changes. (Choose a value of Ystep that gives your program a nice "bouncing" behavior).  You might want to change the counter in the outer loop to run your program for a shorter time (or longer time) when testing. 
 
 

Instructions to return your lab to the teacher: (5 points penalty if instructions are not followed appropriately)

  1. Be sure you return all the printed files where you have written all your answers. These are the answers to Ex. 12.1, 12.24, and the yournamePong zipped folder.
  2. Transfer ONLY the source files you have written from loki to your local Windows machine and collect them in a  folder called YourLastNameLab12 (ex. AGuercioLab12). Be sure you did not copy any executable file in this folder. If you did, please remove it from the folder.
    The files to be zipped are:

    Exercise 12-1: 
    ex121.cpp
    Exercise 12.2:
    ex124.cpp
    Exercise 12-3:
    yournamePong zipped folder
  3. Now exit from the folder, right click on the folder and choose WinZip--> Zip and e-mail. This action will pack your folder and attach it to an e-mail. You can use a Rar Program if you have that program instead of the Zip program.
  4. Send the e-mail to kschaffe@kent.edu with the subject CS23021 Lab 12 Submission.
  5. Send your e-mail before the deadline to receive full credit.

Congratulations! You have completed another Lab of CS I !! :-)