Pong is one of the earliest arcade video games and a two-dimensional sports game which simulates table tennis and was the first game developed by Atari Inc in June 1972. The player controls an in-game paddle by moving it vertically across the left side of the screen, and can compete against either a computer controlled opponent or another player controlling a second paddle on the opposing side. Players use the paddles to hit a ball back and forth. The aim is for a player to earn more points than the opponent; points are earned when one fails to return the ball to the other.
Pong is a simple game to code. The game involves a ball and two pads. The ball moves around on its own in the game world, and the player moves the pad. If the ball hits the wall or lands on the pad, change the ball's general direction. But if the ball misses the pad, then display the message - you missed - to the player.
The controls are:
|UP||move pad up|
|DOWN||move pad down|
|TAB||toggle Cheat on/off|
Writing code for the core logic of a Pong game is as follows. First update the position of the pong ball:
Then test or check the ball's y location in the game's world positions using a series of "
if" statements. If the ball hits the top or bottom wall, then change the ball's general direction.
if( (ball.y<pong_screen_top) />PONG_SCREEN_BOTTOM-2) ) ball.headingY=-ball.headingY;
Now check if the ball has landed on the pad, if it has then make the ball bounce back.
if ( (ball.y>= PlayersPad.LEFT) &&
(ball.y<= PlayersPad.RIGHT) && (ball.x==PlayersPad.x))
But if the ball misses the pad, then display the message - you missed - to the player.
if ( ball.x < PONG_SCREEN_LEFT) displayYouMissed();
That's it. Download the source and check it out. Thanks for reading.
- 15th July, 2010: Initial post