C++ Assignment 16: Single Player Pong

Pong was the first video game to achieve commercial success. It was invented in 1972. It's a little known fact that the programmer who worked with Nolan Bushnell at Atari on Pong is a Lowell alumnus: Allan Alcorn. At first Pong was only found in commercial arcades, but later in 1974 it achieved wide success as a home computer game. The Pong game is important historically as it was the first computer many people brought into their homes. It was the first human computer interface many people experienced. Computer Programmers use the term "killer app" to mean an important (killer) application (program) that has wide appeal. Many people will buy their first computer to run a particular "killer app" . Pong was the "killer app" that helped to make home video games successful.

Starting the Assignment: The Paddle

It is probably easiest to complete this assignment in steps. First work on writing the code that lets you control a paddle. Then, once the paddle is working, add the code for the ball.

The Paddle Functions

To get the paddle working you will need two functions plus main:
void DrawPaddle (int nX, int nY) //just 3 to 5 lines of code!
void ErasePaddle (int nX, int nY) //just 3 to 5 lines of code!

Drawing and Erasing the paddle

Notice that the draw and erase paddle functions take two arguments:
void DrawPaddle (int nX, int nY)

nX and nY are the coordinates of one corner of the paddle. You can make your paddle any shape, size and color you would like. To start, you will probably want to use the setcolor function to set the color of the paddle. To draw the padde, you could call the line function 4 times to make the outline of a rectangular paddle, or you could use the bar function to draw a filled rectangle. (If you use bar don't forget to use a call like setfillstyle(1,WHITE) to set the fill pattern and color.)

The ErasePaddle function looks just like DrawPaddle except you use setcolor(BLACK) (and setfillstyle(1,BLACK) if you are using bar) to cover the paddle with black.

The main function

The main function is where you will get the input for the position of the paddle. Here's some pseudocode:
  1. Open a graphics window.
  2. Declare two local integers for the position of the left corner of the paddle, initialize the x coordinate to 100 and the y to 450
  3. Declare a local character variable to hold the Key pressed by the user
  4. Do while the key pressed is not 'q'
    1. While there is no key pressed
      1. Draw the paddle
      2. delay by 10 milliseconds
    2. use getch() to get the key pressed by the user, store it in the local key variable
    3. Erase the paddle
    4. If the key pressed means left, subtract 10 from the x coordinate
    5. If the new x position is off the screen, reset the x position to 0
    6. If the key pressed means move the paddle to the right, add 10 to the x coordinate
    7. If the new x position is off the screen, reset the x position to the maximum
    8. Draw the paddle
  5. use closegraph(); to close the graphics window
  6. return 0;

Adding the Ball

Now that your paddle is working you will add the ball to your Pong game. You will need to add four functions and some additional code in the main function.

bool

Remember that a bool is a type of "mailbox" that can hold true or false. You can declare and initialize a bool with code like bool bGoingDown = true;

The Four New Functions

You will need to add the following functions:

void DrawBall(int nX,int nY);		//draws a circle with center at x,y
void EraseBall(int nX,int nY);		//draws a black circle with center x,y
void Move(int &nX,int &nY,bool bGoingRight,bool bGoingDown); //moves the ball in the current direction of travel
void Bounce(int nX,int nY,int nPaddleX,bool &bGoingRight,bool &bGoingDown); //controls "bounce" of ball

The first two functions are short: two lines each. You should use the circle function to draw the circle. You can make your ball any size you want, though you might want to start with a ball of radius 10.

The Move function is almost as easy. In move we check to see the the ball is going right. If it is, we increase the x coordinate by 3, otherwise we decrease the x coordinate by 3. Then we check to see if the ball is going down. If it is we increase the y coordinate by 3, otherwise we decrease the y coordinate by 3.

The Bounce function is the most complicated. It takes five arguments: nX and nY are the coordinates of the center of the ball, nPaddleX is the x coordinate of the left of the paddle and bGoingRight and bGoingDown keep track of the direction that the ball is traveling. This function checks to see if the ball should "bounce" off of the edge of the screen or the paddle. To make this function easier, we will limit the ball to a 45 degree angle. Here's some pseudocode:

  1. If the y coordinate is at or below the top edge of the paddle and the x coordinate is between the left and right sides of the paddle.
    1. bGoingDown is false
  2. If the x coordinate is at the left side of the screen.
    1. bGoingRight is true
  3. If the x coordinate is at the right side of the screen.
    1. bGoingRight is false
  4. If the y coordinate is at the top of the screen
    1. bGoingDown is true
  5. If the y coordinate is at the bottom of the screen
    1. bGoingDown is false

The main function

You will need to add some new code to your main function. Here's some pseudocode, the new code is shown in bold:
  1. Open a graphics window.
  2. Declare two local integers for the position of the left corner of the paddle, initialize the x coordinate to 100 and the y to 450
  3. Declare two more local int variables for the center of the ball, initialize the x coordinate to 100 and the y coordinate to 200
  4. Declare two local bool variables, initialize the one for going right to true, and for the one for going down to false
  5. Declare a local character variable to hold the Key pressed by the user
  6. Do while the key pressed is not 'q'
    1. While there is no key pressed
      1. Erase the ball
      2. Move the ball
      3. Bounce the ball
      4. Draw the Ball
      5. Draw the paddle
      6. delay by 10 milliseconds
    2. use getch() to get the key pressed by the user, store it in the local key variable
    3. Erase the paddle
    4. If the key pressed means left, subtract 10 from the x coordinate
    5. If the new x position is off the screen, reset the x position to 0
    6. If the key pressed means move the paddle to the right, add 10 to the x coordinate
    7. If the new x position is off the screen, reset the x position to the maximum
    8. Draw the paddle
  7. use closegraph(); to close the graphics window
  8. return 0;

Thanks to Ivan Ulianov and Dmitry Gomerman for their help with this assignment.