# PONG as my first game

This is a discussion on PONG as my first game within the Game Programming forums, part of the General Programming Boards category; In the key checking, to see what keys are pressed(ex. a=left for player 1 and <- arrow = left for ...

1. ## I haven't tried this but...

In the key checking, to see what keys are pressed(ex. a=left for player 1 and <- arrow = left for player 2) It think if it's like that, you can use and && statement:

if(a && left) // quick example
{
move();
}

That's what I think will work. I never tried this, though.

2. Noone has mentioned the angles of the ball. If the ball hits a wall it is not just going to bounce straight back, it is based on its current angle.

Code:
```struct tagball
{
double x;
double y;
double lastx;
double lasty;
double xinc;
double yinc;
}ball;

double diffx=ball.x-ball.lastx;
double diffy=ball.y-ball.lasty;
diffx*=diffx;
diffy*=diffy;
double tdist=sqrt(diffx+diffy);

ball.xinc=diffx/tdist;
ball.yinc=diffy/tdist;

ball.x+=ball.xinc;
ball.y+=ball.yinc;

For starting angle:
#define PI 3.14159
#define DEGTORAD(x) (double) ((x)*(PI)/(180.0f))

Now make sure that you adjust for the four quadrants (--,-+,+-,++) Make sure that the starting angle is correct so that the ball does not just move up and down. (0 or 180;90 or 270 - depends on alignment of your axes). The xinc and yinc should never reach a point where the ball is untouchable or does not move left and right so trap for those values as well.

Can't find my source, so hope this is correct. If it's not accurate it is very close. It does use the so slow sqrt() and there is a faster way, but just an example.

3. I've already taken care of the angle the ball bounces off at. While I'm sure your method works great, my way was much easier:
totalx=20;
totaly=200;
ballx=.16;
bally=.07;
for (x=0;x!=1;x=x)
{
//user input for paddle
//draw new paddle(s). 2 for 2-play
if ( ball is at a certain x and between 2 of the paddle coordinates)
ballx=(-.16); changes ball direction
//same for other side except ballx would become .16
if (ball hits bottom of screen)
bally=(-.07);
if (ball hits top of screen)
bally=.07;
totalx=totalx+ballx; //update ball's x coordinate
totaly=totaly+bally; //update ball's y coordinate
//delete old ball
//draw new ball
}

notice that the slope remains the same the whole time except it becomes either positive or negative. The angles the ball hits the wall are the same as the angle it bounces off. Any ideas for computer paddle A.I.?

4. ## keyboards

... are annoying in dos games.

I believe what you are after is a keyboard interrupt. If the other post in reply to this works then forgive this useless addition. Your game probably needs a keyboard interrupt function in it so that rather than getting the key and then checking it, a function is called whenever a key is pressed which then does the checking.
For the rest of the time you just use your looping to move the ball around.

Just don't ask me how to write it 'cause I can't remember - you'd have to scourge the net

Almosthere

ps. for the computer's AI you could either

1) have a good look at what Bubba posted (geeez) and get the pc to use the ball's speed and path to figure out where it's gonna land - that way it could know exactly where it was based on the path it could figure it would follow (difficult) OR

2) use your code and simulate it all with a similar loop to your movement one (same effect but probably easier... and messier) OR

3) get it to make an educated guess based on where the ball is and where it is going (the angle and position) to roughly figure it out before fine-tuning by moving with the ball (probably what i'd make it do - more like a person that way) OR

4) just get the computer's paddle to move in line with the ball so it's vertical (or horizontal - whichever way you're moving it) position would be tracking it (quick and dirty).

5. ## thanks

I already have tried the way you mentioned where the computer paddle goes exactly where the ball is and it slows the ball down and makes the computer unbeatble obviously, so I'll maybe try guessing at it or Bubba's method. I also need to use that interrupting function you mentioned. The problem with my original way of 2-play input was that it would check for the guy on the right first and so if they both pressed a key, the guy on the right would always get his paddle to move and not the other guy.

6. try creating another thread, then have one thread control player 1's paddle and another control player 2's

also on the computers ai try making the exact calculations then rounding it, then subtracting the equivilent of a few cm or mm off the calculation, the computer will eventualy miss the ball

Page 2 of 2 First 12
Popular pages Recent additions