Jump to content
  • Advertisement
Sign in to follow this  
MrSpiceGirl

C++ - Creating Pong

This topic is 4632 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

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 this post


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


Link to post
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.

Anyone who knows where to find info about this?

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...

Appreciate answers

Share this post


Link to post
Share on other sites
Quote:
Original post by Tradone
how 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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..

while (paddleposition < ball position)
PC press up;

while (paddleposition > ball position)
PC press down;

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

Hmm time to draw some images for the game :D

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!