Smart, but not too smart, pong opponent

Get help using Construct 2

Post » Thu Mar 29, 2012 9:22 pm

Hi, I was just wondering how I can create a smart pong opponent, like in the actual game of pong. But not too smart to the point where it wins every single time! You know, one that follows the ball pretty well, but not too well. Right now, mine just sits there because I didn't put any events to it. So any help would be appreciated. Thanks!
Posts: 1
Reputation: 302

Post » Thu Mar 29, 2012 9:25 pm

without thinking about it, I would make the opponent react on a random occasion.

Lets say, X seconds after you (the player) hitted the ball back. (where X is a random number between 0.5 & 2 seconds maybe)
Posts: 133
Reputation: 6,599

Post » Thu Mar 29, 2012 9:26 pm

Well, I tried this once (not in Construct, though).

You can add a bit of delay on its movements when changing directions by a random amount, so it responds at different speeds when the ball changes direction.
Posts: 184
Reputation: 6,040

Post » Thu Mar 29, 2012 9:59 pm

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
Posts: 108
Reputation: 3,444

Post » Thu Oct 16, 2014 6:01 am

Hi, I'm making a tennis game, the computer how the ball responds. Please Help :)
Posts: 13
Reputation: 505

Return to How do I....?

Who is online

Users browsing this forum: bambo and 3 guests