# C++ - Creating Pong

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

## Recommended Posts

Howdy, I was wondering if there is any tutorial on the internet which explains how to create a pong clone? Really feel like doing it, though I haven´t figured out how...

##### Share on other sites
There isn't much to it.. You need:
* Graphics to display the paddles, ball and score
* Collision detection to detect if the ball has hit a paddle or gone off the screen
* Logic to tell how the ball will move
* A simple AI for single-player games
* A bit of code to wrap it up, start new games, etc.

Start with graphics, then add ball movement, then collision detection, then AI. Then you're done. So pick a graphics API and get reading.

##### Share on other sites
http://alleg.sourceforge.net/docs/how_to_make_a_pong_game.en.html

##### Share on other sites
how long should it take for a novice programmer?

##### Share on other sites
So, I´ve been studying some SDL for the last 3 hours...

I learnt how to blit images. So I will be able to display background, paddle, ball and score.

I also learnt a easy way to detect collisions, though, I am not sure if this apply to the walls....

I still need to know how I will get the ball moving, and how to make it´s speed increase.
I also need to know how to create an AI.

About the collision detection.... Will this do?

if (balls position == (wallposition-1))
make it bounce;
if (balls position == (otherwallsposition-1))
make it bounce;

if (balls position == Leftside of the screen)
++Player 1 score;
Re-place ball to the middle;

if (balls position == Rightside of the screen)
++Player 2 score;
Re-place ball to the middle;

Hope you understood that what I wrote were with commonlanguage combined with the construction of code.

So I still need to know, where can I find information about getting the ball to move automaticly and how to create an AI.

baker >> Ill check that out, though im working with SDL...

##### Share on other sites
Quote:
 Original post by Tradonehow long should it take for a novice programmer?

Depends on how you define "novice". I wouldn't imagine too long, but if you've only made a few programs it may be longer. It may take you a week, it may take you two. It could take you an hour if you know what you are doing.

Good luck!

##### Share on other sites
the ball's speed should be stored as an x and y component.
to make the ball speed up, you can just do:

velocity.x *= CONSTANT;velocity.y *= CONSTANT;

update the ball's position with:
position.x += velocity.x * time_since_last_frame;position.y += velocity.y * time_since_last_frame;

your collision detection looks about right, except it doesn't work if the ball travels more than one pixel in a frame, so you could do:

if (position.y + velocity.y * time_since_last_frame == (wallposition-1))make it bounce;

etc.

good luck!

##### Share on other sites
Alrighty, thanks :D

Right now I am studying the source code of a pong already created to get some hints. I feel pretty comfortable with the code. Even though almost everything is new I feel that I understand it.

Though, this pong is a 2-player pong. Which means that I can´t lean anything about AI from it.

So I am still looking for tutorials about AI :P

##### Share on other sites
As for the AI, when I made a pong game, I just made it so the AI player could basically choose to press up or down, just like the player. If the ball is above the AI player it 'presses' up, and vice versa. This method kind of sucked, because the AI would be perfect at slow ball speeds, and worthless when the ball moved fast.

You could have the AI paddle follow the Y position of the ball, but not perfectly by using random() creatively.

##### Share on other sites
Hmm I think that I will start out by using the first method you told about so I can get the game to run before I try anything more advanced.

Is it created using while loops?
was thinking something like..

PC press up;

PC press down;

Or is it more advanced than that? Any other ideas?

Hmm time to draw some images for the game :D

##### Share on other sites
For a novice at game programming or to the graphics API they are using, but not to their language, I would say making a pong clone could take somewhere between 3 and 15 hours.

For a novice at programming their language, but not programming in general(eg: programmed before, but not in the language used to make the pong cone) it could take from 5 to 15 hours.

For a novice at programming of any sorts, it could take upward of 10 hours to really any time, depending on the language and how lucky they get.

These are all estimates. For just a simple pong clone(no multiplayer, except on same computer, no highscores list, especially not online, no funky graphics, no 3d, ect.) these should be roughly accurate numbers.

##### Share on other sites
For the AI in my pong clone I had the computer guess where the ball would end up rather than try to follow it. I felt this was more realistic to how a human plays. When you see the ball is going to bounce off a wall you dont follow it to the wall you guess where you think the ball will bounce to after hitting the wall and wait for the ball there, adjusting as necessary as it gets closer.

This is basically what my AI does. After the player hits the ball and it is heading toward the AI, I calculate the exact position the ball will end up on the AI side. I then applied a random guess factor for the AI so that he is not exact on the location. He could be off by many pixels depending on the ball speed and distance when the guess is made. The distance factors in because the AI gets to guess as soon as the ball is hit in his direction and then again when the ball is about a paddles length away. This way he has a chance to correct a bad guess but this mostly only helps if the guess is just slightly off so he doesn't have silly misses that are one pixel away. Actually I have 3 difficulty levels, the easy difficulty still has some silly misses but the normal and hard difficuly are more accurate.

You are welcome to take a look at the source if you want. I used allegro though so some things may be different but the basic AI logic should make sense regardless. This is the first AI I've really done so I'm sure it could be done better ways but it works and you are welcome to use or modify it to your liking :)

##### Share on other sites
Thanks for all the help :D

Thoguh, Im stuck now, when I execute the game the game just flashes and I get an error message:

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

So far I havn´t been able to find the error since I don´t really understand what the error message means. Anyone got any advice?

Edit: Problem solved, off to tetris :D

[Edited by - MrSpiceGirl on April 9, 2006 1:50:39 PM]