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.
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:
- kill some time by creating a loop with an empty body (again, here you can adjust your values when you see the ball bouncing)
- now erase the ball;
- then draw the green window again. (Observe that every time something is erased from the green rectangle, the rectangle must be refreshed by redrawing it.
- Now, where is the ball? Get the position of the ball! In order to read the coordinates, an accessor method GetPosition(float XBallCoord, float YBallCoord) is available in the RectangleShape class. The method, when called with the ball object, reads the coordinates of the ball (which are private variables).
- Compute now the value of the step that has to be added to the current position to redraw the ball to the right or to the left. Remember that the value depends on the direction the ball should go during the next step. The ball will go to the right until it touches the right hand side of the green rectangle. If the XBallCoord is larger than the green window length (which means that the ball has reached the right hand side of the green window) we have to set the step to the opposite direction. Remember that, initially, to let the ball go to the right, we have set the Xstep to 1% of the length of the green window. To let the ball go to the left, we set the Xstep to (-1%) of the length of the green window. Use a conditional statement to express this situation.
- Update the coordinates of the ball so that we can draw the ball in the new position. The ball should add the step computed in the previous step to the X coordinate of the ball to simulate its horizontal movement. In order to update the coordinates of the ball, use the method called SetPosition((float NewXBallCoord, float NewYBallCoord) that is available in the class RectangleShape. The method, when called, updates the value of the coordinates to those passed by value in the parameters.
- Finally, we redraw the ball. The ball will be drawn in the new position.
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)
Congratulations! You have completed another Lab of CS I !! :-)