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.
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!
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.
getch()
to get the key pressed by the user, store it in the local key variable
closegraph();
to close the graphics window
return 0;
bool
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;
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:
bGoingDown
is false
bGoingRight
is true
bGoingRight
is false
bGoingDown
is true
bGoingDown
is false
main
functionint
variables for the center of the ball, initialize the x coordinate to 100
and the y coordinate to 200
bool
variables, initialize the one for going right to true
,
and for the one for going down to false
getch()
to get the key pressed by the user, store it in the local key variable
closegraph();
to close the graphics window
return 0;