Jump to content
  • Advertisement
Sign in to follow this  

pong game needed + source

This topic is 4634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ball
RECT ball = {ball_y,
ball_x,
ball_x+ball_size,
ball_y+ballsize};
// player 1 paddle
RECT paddle1 = {paddle_y,
paddle_x,
paddle_x+paddle_width,
paddle_Y+Paddle_height};

// here you would also have player2's paddle

int 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 walls
if ((ball.top <= 0) || (ball.bottom >= SCREEN_HEIGHT))
{
BallVY = -BallVY; // reverse ball velocity
}

// check for collision with side walls increment player
// score accordingly
if (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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!