I'd suggest starting out with nearly "perfect" paddle control and then adding in "mistakes" until you've reached a difficulty level you're happy with. Fortunately, near-perfect paddle control is pretty simple logically in pong.
The following assumes a normal pong layout with a paddle on each side of the screen. If you have a different layout you'll need to adjust as appropriate.
First, the logic for our "perfect" control is as follows:
- IF the ball is moving away from the paddle, try to move the paddle so it is centred vertically.
- IF the ball is moving towards the paddle, try to match the paddle's y coordinate (assuming the origin is the centre of the paddle) to the ball's y coordinate.
If you wanted to include different difficulties this could possibly be the AI for your "hard" mode, although you should only make a player play against this AI if your game is set up in such a way that it can become impossible for the paddle to hit the ball. The only possible improvement on this would be to predict the ball's path and move towards the final location rather than matching the y-coordinate, but as we want a beatable opponent it would be silly to bother with that extra logic.
Next, just add some mistakes, selecting one or more from the following:
- Over-shoot: rather than attempting to match the y-coordinate exactly, just give the paddle regular "up" or "down" updates, so that it might move further than actually necessary and have to adjust. This will often result in "jittery" paddle movement, but if you watch a real player you'll find they are often jittery too!
- Delay: slow down the paddle's reaction times when starting it's movement (or when stopping it's movement, to again achieve an over-shoot!). I'd suggest a random delay of little more than 1 second (perhaps 0.2 to 1.2 seconds?), as too long will be obvious and make your AI look intentionally stupid. Have a play with the numbers to see what feels natural.
- "Jitter" when waiting for the ball to come back. Real players often move towards the centre (as described above) when the ball is moving away... but then they get bored, and move the paddle up or down a bit. This can result in it being slightly harder to reach the ball.
- "Wrong button!": you might occasionally just outright move the paddle in the wrong direction. This should be used very
sparingly (1 out of 20 paddle movements or less), and you should never move it very far, otherwise it will become obvious you're trying to lose the game. If thrown in very occasionally this one can help with the illusion of human-like input however.
Does that help? jbadams2012-03-29 22:10:42