Except, if you move the paddle as Philip suggests, the ball won't ever be above or below the paddle......it will just follow it. That's probably the most basic pong AI to start off with.
Once you've managed that, the next step, I would suggest, is to make the paddle return to the middle each time it has hit the ball, and once it gets to the middle you would then implement a 'move up if the ball is above/move down if the ball is below' type routine. The paddle obviously has a maximum speed, so sometimes it will make it, sometimes it won't.
As the levels get harder, you just increase the AI's paddle speed (and the ball speed too probably)
You probably want to figure it out for yourself, but you can use this code for reference if you like - it is a version of pong I wrote a couple of months ago which uses exactly this type of AI.
Rem Project: pong
Rem Created: 29/07/2004 16:10:54
Rem ***** Main Source File *****
sync on
sync rate 50
hide mouse
backdrop on
`make objects
`player paddle
make object box 1,20,60,20
set object collision on 1
color object 1,rgb(255,0,0)
`computer paddle
make object box 2,20,60,20
set object collision on 2
color object 2,rgb(0,255,0)
`ball
make object sphere 3,10
set object collision on 3
color object 3,rgb(255,0,0)
`court
`top
make object box 4,500,20,300
set object collision on 4
color object 4,rgb(255,0,255)
`bottom
make object box 5,500,20,300
set object collision on 5
color object 4,rgb(100,100,0)
`back
make object box 6,500,300,20
color object 6,rgb(0,0,255)
`set up court
position object 4,0,-150,0
position object 5,0,150,0
position object 6,0,0,300
`position paddles
position object 1,-180,0,100
position object 2,180,0,100
`position ball
position object 3,0,0,150
`set light
set ambient light 30
make light 1
set point light 1,0,0,0
set light range 1,200
`set camera
make camera 1
position camera 1,0,0,-500
color backdrop 1,rgb(0,0,0)
`restart new game
newgame:
`set initial variables
level=1
pscore=0
cscore=0
pv#=8 `player paddle velocity
cv#=1 `computer paddle velocity
bv#=6 `ball velocity
`restart new serve or new level
restart:
randomize timer()
wait 1000
py#=-10+rnd(20) `player y
cy#=0 `computer y
bx#=0 `ball x
by#=0 `ball y
bd#=0 `ball direction (in degrees:- right=0)
`main loop
do
set text size 10
print screen fps()
`player input
if upkey()>0 and object collision (1,5)=0
py#=py#+pv#
endif
if downkey()>0 and object collision (1,4)=0
py#=py#-pv#
endif
`move player paddle
position object 1,-180,py#,100
`ball motion
bx#=bx#+bv#*cos(bd#)
by#=by#+bv#*sin(bd#)
`collision with court
if by#>140
by#=by#-10
bd#=bd#*-1
endif
if by#<-140
by#=by#+10
bd#=bd#*-1
endif
`collision with paddles
if object collision (3,0)=1
bv#=bv#*-1 `reverse velocity
bx#=bx#+20+(level*3) `prevent embedding
`measure offset between player paddle and ball and adjust direction
po#=by#-py#
bd#=po#*1.5
endif
if object collision (3,0)=2
bv#=bv#*-1
bx#=bx#-20-(level*3)
co#=by#-cy#
bd#=co#*1.5
endif
`new ball position
position object 3,bx#,by#,100
`computer paddle A.I.
`return to middle
if bv#<0
if cy#>2 then a#=-cv#
if cy#<2 then a#=cv#
if cy#<=2 and cy#>=-2 then a#=0
cy#=cy#+a#
endif
`move towards ball
if bv#>0
if by#>cy# then a#=cv#
if by#<cy# then a#=-cv#
cy#=cy#+a#
endif
position object 2,180,cy#,100
`win or lose
if object position x(3) < -250
cscore=cscore+1
vb#=vb#*-1
goto restart
endif
if object position x(3) > 250
pscore = pscore +1
goto restart
endif
`print score
ink rgb(255,255,255),0
set text size 15
set cursor 300,230
print pscore," : ",cscore
`goto next level, increase difficulty and reset scores
if pscore>2
bv#=bv#+1
pv#=pv#+0.3
cv#=cv#+0.5
level=level+1
pscore=0
cscore=0
goto restart
endif
`display level
set cursor 275,10
set text size 10
ink rgb(0,255,0),0
print "Level ",level
`game over
if cscore>2
ink rgb(205,205,255),0
set cursor 240,250
set text size 30
print "Game Over"
set text size 20
set cursor 220,280
Print "Press any key to restart"
sync
wait key
goto newgame
endif
point camera 1,0,0,0
position light 1,bx#,by#,0
point light 1,bx#,by#,200
if inkey$()="q" then goto restart
sync
loop
wait key