I have just seen this thread and written a small but very hard-to-beat AI for it.
`change these to make things more interesting
goalsize = 20
ballangle = 360 / 32
REM Change this values to en/Disable AI!
Player1AI as boolean = 1
Player2AI as boolean = 1
`setup
sync on : sync rate 60
backdrop on : color backdrop rgb(0,155,0)
fog color rgb(28, 73, 140)
set camera range 10, 10000
fog distance 10000
set text font "arial bold" : set text size 20 : set text transparent : ink 0,0
global Looptime : Global lastTime : UpdateDelta()
autocam off
xrotate camera 90
move camera -100
`start coding
global ball as integer
ball = makesphere(7)
ballspeed as float
`ballhit as float
`ballhit = 3
player1 = makesphere(10)
position object player1, -70, 0, 0
color object player1, rgb(255,0,0)
player2 = makesphere(10)
position object player2, 70, 0, 0
color object player2, rgb(0,0,255)
global speed as float
speed = 2
goalsize = 20
goal1 = makecube(goalsize)
position object goal1, -80, 0, 0
scale object goal1, 50, 50, 100
goal2 = makecube(goalsize)
position object goal2, 80, 0, 0
scale object goal2, 50, 50, 100
do
UpdateDelta()
if Player1AI=0
if keystate(17)
position object player1, object position x(player1), object position y(player1), object position z(player1) + delta(speed)
endif
if keystate(31)
position object player1, object position x(player1), object position y(player1), object position z(player1) - delta(speed)
endif
if keystate(30)
position object player1, object position x(player1) - delta(speed), object position y(player1), object position z(player1)
endif
if keystate(32)
position object player1, object position x(player1)+ delta(speed), object position y(player1), object position z(player1)
endif
else
HandleAI(player1,goal2)
endif
if Player2AI=0
if upkey()
position object player2, object position x(player2), object position y(player2), object position z(player2) + delta(speed)
endif
if downkey()
position object player2, object position x(player2), object position y(player2), object position z(player2) - delta(speed)
endif
if leftkey()
position object player2, object position x(player2) - delta(speed), object position y(player2), object position z(player2)
endif
if rightkey()
position object player2, object position x(player2)+ delta(speed), object position y(player2), object position z(player2)
endif
else
HandleAI(player2,goal1)
endif
if object collision(ball, player1)
point object ball, object position x(player1), object position y(player1), object position z(player1)
yrotate object ball, object angle y(ball)+180
a = object angle y(ball) / ballangle
yrotate object ball, a*ballangle
ballspeed = speed
endif
if object collision(ball, player2)
point object ball, object position x(player2), object position y(player2), object position z(player2)
yrotate object ball, wrapvalue(object angle y(ball)+180)
a = object angle y(ball) / ballangle
yrotate object ball, a*ballangle
ballspeed = speed
endif
if ballspeed > 0
dec ballspeed, 0.05
move object ball, delta(ballspeed)
endif
if object in screen(ball) = 0
position object ball, 0, 0, 0
endif
if object collision(ball, goal1)
inc player2score
position object ball, 0, 0, 0
endif
if object collision(ball, goal2)
inc player1score
position object ball, 0, 0, 0
endif
text 10, 10, "RED: " + str$(player1score)
text screen width()-text width("BLUE: " + str$(player2score)) - 10, 10, "BLUE: " + str$(player2score)
sync
loop
end
function HandleAI(ID,Goal)
rem Player
x# = object position x(ID)
z# = object position z(ID)
rem Ball
xb# = object position x(Ball)
zb# = object position z(Ball)
rem Target(Goal)
xt# = object position x(Goal)
zt# = object position z(Goal)
rem Angle between ball and goal
BGA# = atanfull(xt#-xb#,zt#-zb#)
rem Angle between player and ball
BPA# = atanfull(xb#- x#,zb#- z#)
rem Calculate angle-difference
ADif# = BPA#-BGA#
if ADif# > 180 then dec ADif#, 360
if ADif# < -180 then inc ADif#, 360
rem Ball in right direction -> run to ball
if abs(ADif#)<10
XAdd# = speed*cos(90-BPA#)+1-rnd(2)
ZAdd# = speed*sin(90-BPA#)+1-rnd(2)
position object ID, object position x(ID)+Delta(XAdd#), object position y(ID), object position z(ID)+Delta(ZAdd#)
else `Ball not in right direction -> run behind ball
TX# = xt#+(xb#-xt#)*1.2+5-rnd(10)
TZ# = zt#+(zb#-zt#)*1.2+5-rnd(10)
BPA# = atanfull(TX#-x#,TZ#-z#)
XAdd# = speed*cos(90-BPA#)
ZAdd# = speed*sin(90-BPA#)
position object ID, object position x(ID)+Delta(XAdd#), object position y(ID), object position z(ID)+Delta(ZAdd#)
endif
endfunction
`Delta
Function UpdateDelta()
TheTimer=Timer()
LoopTime=TheTimer-lastTime
lastTime=TheTimer
Endfunction
Function Delta(Value1#)
ResultValue#=Value1#*0.060*Looptime
Endfunction ResultValue#
Function MakeSphere(Size#)
Numbre=FreeObject()
if Numbre>TopObject then TopObject=Numbre
make object sphere Numbre, Size#
if Numbre > MaxObjects
MaxObjects = Numbre
endif
Endfunction Numbre
Function MakeCube(Size#)
Numbre=FreeObject()
if Numbre>TopObject then TopObject=Numbre
make object cube Numbre, Size#
if Numbre > MaxObjects
MaxObjects = Numbre
endif
Endfunction Numbre
function FreeObject()
repeat
inc i
if object exist(i)=0 then found=1
until found
endfunction i
Function ObjectExist(Numbre)
i=0
if Numbre>0
if Object Exist(Numbre)
i=1
endif
endif
endfunction i
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1