forums

TNT Basic Forums > Programming Q&A
best way to to AI for pong?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

02-02-2007 02:01

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

i was thinking about making the pong game single player as well. I was wondering what the best way to make a pong Ai would be? Obivosuly i could make an Ai that is unbeatable really easy (player2y=bally) to make it easier to win should i just do something like player2y=bally-50 or something like that? or is there a better way? TIA
- Hendo!

02-02-2007 08:08

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

It's not too difficult...

The trick is to make it Look smart. There are basically two areas here, when the ball is moving away from the AI paddle, and when it's moving towards it.

Moving away
When the ball hits the paddle, generate a random number between 0 - 100. if this value is below, say, 30 then set a paddle_target value to 1, if it's above 80 then set it to 2, otherwise it's 0. When the ball is moving away from the paddle, check the target value. If it's 1 then move the paddle slowly towards the ball, if it's 2 move it slowly towards the middle of the play area and if it's 0 then don't move the paddle at all. You can add variations here, moving in a random way, or only moving a few steps etc. and of course you can tweek the values and change the speed.

Moving towards
When the ball hits the player paddle or a wall generate two values, speed and offset. If paddle_y>ball_y+offset then take speed off of paddle_y else if paddle_y<ball_y-offset then add speed to paddle_y. If neither of these apply then leave it alone. You could also generate a Delay value and pause the paddle for that number of loops (if delay>0 then dec delay, else move paddle etc).

Playing around with these should get you somewhere near where you want to be...

Danny (nods)

02-02-2007 08:14

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

(by the way)

If you really want to add the element of 'Human Error' you could give the paddle momentum. This means it would take longer to get up to speed, and longer to change direction or slow down...

I'll leave you to play with that though, don't want it to be too easy eh? ;)
HINT: Check the directory...

Danny (nods)

02-02-2007 12:23

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

i tried this for when it hits the AI bat:
global int paddle_target=0
int hitbat2=sprite col (2,0)
if hitbat2>-1
if random (0,100)<30
paddle_target=1
else if random (0,100)>80
paddle_target=2
else
paddle_target=0
end if
if paddle_target=0
player2y=player2y
else if paddle_target=1
player2y=bally-50
else if paddle_target=0
repeat
if player2y>240
player2y=player2y-5
else if player2y<240
player2y=player2y+5
end if
until player2y=240
end if
end if

it doesn't work though...nothing happens.

02-02-2007 12:25

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

wait never mind i fixed it.

02-02-2007 15:54

Posted by:
Hendo

Location:
Barrie, Ontario

Click Here to Email Hendo   Find more posts by Hendo

i tried this for my AI

int hitbat=sprite col (2,0 to 1)
int hitbat1=sprite col (1,2)
int hitbat2=sprite col (2,0)
if hitbat2>-1
if random (0,100)<30
paddle_target=1
else if random (0,100)>80
paddle_target=2
else
paddle_target=0
end if
if paddle_target=0
player2y=player2y
if paddle_target=1
player2y=bally-50
else if paddle_target=2
repeat
if player2y>240
player2y=player2y-5
else if player2y<240
player2y=player2y+5
end if
until player2y=240
end if
end if
end if
if hitbat1>-1
if player2y>bally+15
player2y=bally-40
else if player2y<bally-10
player2y=bally+40
end if
end if

but it didn't work at all... I'm not a great coder so I have trouble spotting my errors =^(

02-03-2007 18:39

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

OK, a few problems there...

Where to start? First off, in both sets of code, lose the repeat...until sections, you only want to move a step at a time.

The first bit of code is getting there, though I think the paddle should move rather than jump to a position, but the second bit still has me sort of stumped.
I don't get the collision checks (though it probably makes more sense if you can see the whole code or know the sprite numbering), and you set a target again... Why? If the ball is heading towards the paddle the only logical target must surely be the ball... Re-read my first post, second part (Moving Towards), and have a look at what it's doing, then check the logic in your 'AI' section, I think you'll agree they're pretty different.

Well, have another play with it and get back to me if you can't figure it out. If you get really stuck you can mail me the project and I'll take a look for you.

Danny (nods)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.