pong game needed + source

Started by
9 comments, last by chad_420 18 years, 8 months ago
hey, im gonna try to make a pong game but i need to see some sourc code on collision detection and ball speed and if the ball is out of the room the other player gets a point etc. So anyone knows a good pong + source? thnx
Advertisement
i just threw up my pong clone with source code on the GD showcase

written with C++ and DirectX 9.0


Ponged
The collision detection is fairly simple for a pong game. There are three different things you need to test for:

1) A collision with the paddle. Fairly easy if you do simple bounding boxes. Should look fine.
2) Collision with the top/bottom walls. Just test the y coordinates of the ball. If it is colliding, then just set the direction of the ball to 90 degrees off what it was.
3) Collision with the right/left walls. Just test the x coordinates. If it is a hit, add a point to the other player and reset the ball.

First set it up so the ball will bounce off the top/bottom wals. Then add the paddle collision code. Then add the code for winning/losing. Just take it slowly and do everything one at a time.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
oh i forgot to say i only want it pure win32 no directx or so.
Quote:Original post by jordi_0071
oh i forgot to say i only want it pure win32 no directx or so.


Well the collison detection is the same no matter how you render it. The only difference is how you will be drawing the various objects (paddles and ball).
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Not necesarily, in directx you can use the convenient helper funtions in D3DX like D3DXSphereBoundProbe(), that make it really easy to do collision on primitives.
Try this:
#define SCREEN_HEIGHT  GetSystemMetrics(SM_CYSCREEN)#define SCREEN_WIDTH   GetSystemMetrics(SM_CXSCREEN)// RECT goes in order: top,left,right,bottom (I think)// square ballRECT ball = {ball_y,             ball_x,             ball_x+ball_size,             ball_y+ballsize};// player 1 paddleRECT paddle1 = {paddle_y,                paddle_x,                paddle_x+paddle_width,                paddle_Y+Paddle_height}; // here you would also have player2's paddleint BallVX = [velocity];int BallVY = [Velocity];int p1score = 0; // player 1 score (left)int p2score = 0; // player 2 score (right)// check for collision with top and bottom wallsif ((ball.top <= 0) || (ball.bottom >= SCREEN_HEIGHT)){    BallVY = -BallVY;  // reverse ball velocity}// check for collision with side walls increment player// score accordinglyif (ball.left <= 0){    ++p2score;  // increase player2 score    BallVX = -BallVX;}else if (ball.right >= SCREEN_WIDTH){    ++p1score;    BallVX = -BallVX;}// check for collision with paddle (does not check for collision with// sides of paddle but it's pretty much the same)if ((ball.left >= paddle1.left) && (ball.right <= paddle1.right) &&    (ball.bottom >= paddle.top)){    BallVX = -BallVX;}


I wrote this on the fly so there might be some (or a lot) of errors
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
You'll first need to choose a language and an API to work with. SDL or Allegro are great choices for beginners to graphics programming, while OpenGL and DirectX are good choices if you plan to do things a little more complex. As for the language, C++ is the standard language in the industry. But I personally would recommend going with either Python (via PyGame, a Python SDL interpretation) or C# (via SDL.NET).

My pong compilation, rPong, was done using C++ and SDL. If I were to do it again, I would use C# and SDL.NET or Managed DirectX.
Rob Loach [Website] [Projects] [Contact]
i use C++
But you need an API to display the graphics and for getting input from the keyboard. So you should probably choose between either SDL or Allegro as mentioned above. I don't know anything about Allegro, but SDL is quite easy to use IMO. Here's a very nice tutorial on starting out with it: here

This topic is closed to new replies.

Advertisement