Public Group

# Help with Pong AI in C# w/ XNA

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

## Recommended Posts

I've been working on Pong in C# w/ XNA (a rewrite from my last attempt), and I'm now at the point where I want to give the second paddle AI.

I'm not sure how to implement it. All previous attempts resulted in the paddle stuttering or not moving correctly.

##### Share on other sites
You haven't provided much information in your post. How have you been approaching the problem so far?

##### Share on other sites
Well in my attempts, I tried to move the paddle when the ball moved in a certain direction.

If it's Y value was += a number, then it meant the ball was moving upwards, and so the paddle would move upwards to stop it.

The problem here is that it'd simply move upward, without any real attempt at following the ball, so it looked really bad.

##### Share on other sites
I have seen this be done:
Say you are playing from left to right, you could compare the balls y position with the paddle's y position.

If the ball is higher, then move the paddle higher (the amount it would move could be stored in a vairable that you could tweak to adjust speed and judder and difficulty)

If the ball is lower, then move the paddle lower.

On the game I saw, the above would only occur when the ball was heading towards the computer controlled pad, not when heading away.

This seemed natural enough in that I only realised what it was doing when I purposely decided to think what it was doing. Then it took about 3 volleys.

This is very basic but it's a start and I think it may help you. Let me know if it doesn't.

##### Share on other sites
I got some time to try out your method and it worked.

It checks to see if the ball is incoming (based on whether it goes to the right of the screen's midpoint) and if the Ball's Y position is greater than or less than the Paddle's Y position, it moves accordingly.

My next step with the AI is to change the paddle's speed or something to reflect greater difficulty.

##### Share on other sites
This is how i aprroched the same problem

All pseudo code ofc
//Some Variablesfloat maxMovmentValue = 5f; //The Maximum amount the paddle can move at any one timevoid MoveAiPaddle(float Amount){//We simpally move the Ai's paddle along the Y axis by 'Amount'Ai.Paddle.Position += MathHelper.Clamp(Amount, -maxMovmentValue, maxMovmentValue);}void CheckRelPos(){//Check the paddles Y Position Relative the the Balls positionfloat ballYPos = pongBall.Center.YPosition; // we get the CENTER of the Texturefloat paddleYPos = Ai.Paddle.Center.Y;float difference = paddleYPos - ballYPos;//Here we attempt to move the paddle by the difference in y values//The Method should clamp the value so it can only move a certain amount.MoveAiPaddle(difference);}

This works if your ball speeds up and if you have the ball bounding off at an angle from your paddle.

The AI paddle is always moving to keep its center inline with the center of the ball. if the ball is moving tofast the paddle may not reach it as it can only move by '' of course just tweak the value to your liking or something.

P.S. this may not be the best way but at least its an idea

Best of Luck ~Andy^

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013187
×