[Solved] 'Pong' AI player movement (player vs Computer)

Hello Community,

I’m trying to remake pong (as an educational exercise), so far I’ve found tutorials more for 2 (keyboard) players using WASD + CURSOR… but I’m trying to make a 1 player vs Computer version.

I tried this - LOL


But it roams around, chasing the ball.

I also lowered the speed also, as I don’t want to have the perfect AI player…

How best to do this please?
Thanks,
Roberto

1 Like

Hi, it seems there are different ways to do this. For some background about the original pong game:

And here a solution that is suggested often and seems to work (if ball is above paddle, move up; if ball is below paddle, move down)
https://www.gamedev.net/forums/topic.asp?topic_id=439576

Most simple solution is to for example create global variable called RNG
Now you make event
Condition
Ball distance to Player2 is below 50 pixels (adjust distance to your needs i would go with going length of half of whole field)
Trigger once

Action
Change global variable RNG set to RandomInRange(0,5)

Now you copy your event from your screenshot above and paste it over in 3 events

in 1st event
Condition
Global Variable RNG is higher or equal to 0
Global Variable RNG is lower or equal to 3

Action
Here you add your action from screenshot BUT
Option A
You go for Player2.AngleToObject(Ball.Y)+RandomInRange(-15,+15)
Again numbers here need to be adjusted

Option B
Add action from your screenshot and in length in pixels go RandomInRange(200,400) instead of just 400

Now
You do same exact thing for 2nd event with condition
Global Variable RNG is equal to 4

And in action you do same exact thing where you give it lower random in range values
So if you for example had in above event
Player2.AngleToObject(Ball.Y)+RandomInRange(-15,+15)
You would go with
Player2.AngleToObject(Ball.Y)+RandomInRange(-5,+5)

3rd event
Condition
Global Variable RNG is equal to 5

Action
Here you simply paste what you have on screenshot

Idea is
RNG variable will pick random number from 0 to 5
So you have bigger chances to get number from 0 to 3
Which in action will make AI make dumb move

And lower chance if RNG will be 4 where enemy make less dumb move

And same chance as above if RNG will be 5 for enemy to make spot on move

I hope you get the idea

Thanks @Drona & @ZeroX4. I got it working with your help.

2 Likes