hey sorry for bringing this up again, but with the recent challenge for ai coding and this topic i thought it might help if i posted something i made a while back. my internet has been down so i wasnt able to post it earlier.
Here's an example of simple ai. probably not the best one but it works.
Use the mouse to control the blue circle and try to run from the red one. both circles use ai:
sync on : sync rate 200 : cls
dim wall(100,4)
dim player(4)
dim enemy(4)
dim highscore(10)
start:
score=0
for num=1 to 100
wall(num,1)=rnd(629)
wall(num,3)=wall(num,1)+10
wall(num,2)=rnd(469)
wall(num,4)=wall(num,2)+10
for nums=1 to num
if nums<>num
if wall(num,1)<wall(nums,3) and wall(num,3)>wall(nums,1) and wall(num,2)<wall(nums,4) and wall(num,4)>wall(nums,2)
num=num-1
nums=num
endif
endif
next nums
next num
repeat
player(1)=rnd(629)
player(3)=player(1)+10
player(2)=rnd(469)
player(4)=player(2)+10
p=0
for num=1 to 100
if player(1)<wall(num,3) and player(3)>wall(num,1) and player(2)<wall(num,4) and player(4)>wall(num,2)
num=100
p=1
endif
next num
until p=0
repeat
enemy(1)=rnd(629)
enemy(3)=enemy(1)+10
enemy(2)=rnd(469)
enemy(4)=enemy(2)+10
e=0
for num=1 to 100
if player(1)<wall(num,3) and player(3)>wall(num,1) and player(2)<wall(num,4) and player(4)>wall(num,2)
num=100
e=1
endif
next num
if enemy(1)<player(3) and enemy(3)>player(1) and enemy(2)<player(4) and enemy(4)>player(2)
e=1
endif
until e=0
do
inc score
mx=mousex() : my=mousey()
ink rgb(255,255,255),0
for num=1 to 100
box wall(num,1),wall(num,2),wall(num,3),wall(num,4)
next num
ink rgb(0,255,0),0
text 0,0,str$(score)
gosub pai
ink rgb(0,0,255),0
circle player(1)+5,player(2)+5,5
ink rgb(255,0,0),0
gosub eai
circle enemy(1)+5,enemy(2)+5,5
gosub startover
sync
cls
loop
eai:
if enemy(1)+5<player(1)+5
xcol=0
for num=1 to 100
if enemy(3)+1=>wall(num,1) and enemy(3)+1<=wall(num,3) and enemy(2)<=wall(num,4) and enemy(4)=>wall(num,2)
xcol=1
endif
next num
if xcol=0
enemy(1)=enemy(1)+1
enemy(3)=enemy(3)+1
endif
else
xcol=0
for num=1 to 100
if enemy(1)-1<=wall(num,3) and enemy(1)-1=>wall(num,1) and enemy(2)=<wall(num,4) and enemy(4)=>wall(num,2)
xcol=1
endif
next num
if xcol=0
enemy(1)=enemy(1)-1
enemy(3)=enemy(3)-1
endif
endif
if enemy(2)+5<player(2)+5
ycol=0
for num=1 to 100
if enemy(4)+1<=wall(num,4) and enemy(4)+1=>wall(num,2) and enemy(1)<=wall(num,3) and enemy(3)=>wall(num,1)
ycol=1
endif
next num
if ycol=0
enemy(2)=enemy(2)+1
enemy(4)=enemy(4)+1
endif
else
ycol=0
for num=1 to 100
if enemy(2)-1<=wall(num,4) and enemy(2)-1=>wall(num,2) and enemy(1)<=wall(num,3) and enemy(3)=>wall(num,1)
ycol=1
endif
next num
if ycol=0
enemy(2)=enemy(2)-1
enemy(4)=enemy(4)-1
endif
endif
return
pai:
if player(1)+5<mx
if player(1)+5<633
xcol=0
for plus=2 to 1 step -1
for num=1 to 100
if player(3)+plus=>wall(num,1) and player(3)+plus<=wall(num,3) and player(2)<=wall(num,4) and player(4)=>wall(num,2)
xcol=plus
endif
next num
next plus
if xcol=0
player(1)=player(1)+2
player(3)=player(3)+2
else
if xcol=2
player(1)=player(1)+1
player(3)=player(3)+1
endif
endif
endif
else
if player(1)+5>6
xcol=0
for plus=2 to 1 step -1
for num=1 to 100
if player(1)-plus<=wall(num,3) and player(1)-plus=>wall(num,1) and player(2)<=wall(num,4) and player(4)=>wall(num,2)
xcol=plus
endif
next num
next plus
if xcol=0
player(1)=player(1)-2
player(3)=player(3)-2
else
if xcol=2
player(1)=player(1)-1
player(3)=player(3)-1
endif
endif
endif
endif
if player(2)+5<my
if player(2)+5<473
ycol=0
for plus=2 to 1 step -1
for num=1 to 100
if player(4)+plus<=wall(num,4) and player(4)+plus=>wall(num,2) and player(1)<=wall(num,3) and player(3)=>wall(num,1)
ycol=plus
endif
next num
next plus
if ycol=0
player(2)=player(2)+2
player(4)=player(4)+2
else
if ycol=2
player(2)=player(2)+1
player(4)=player(4)+1
endif
endif
endif
else
if player(2)+5>6
ycol=0
for plus=2 to 1 step -1
for num=1 to 100
if player(2)-plus<=wall(num,4) and player(2)-plus=>wall(num,2) and player(1)<=wall(num,3) and player(3)=>wall(num,1)
ycol=plus
endif
next num
next plus
if ycol=0
player(2)=player(2)-2
player(4)=player(4)-2
else
if ycol=2
player(2)=player(2)-1
player(4)=player(4)-1
endif
endif
endif
endif
return
startover:
if enemy(1)<player(3) and enemy(3)>player(1) and enemy(2)<player(4) and enemy(4)>player(2)
cls
new=0
hi=10
for h=1 to 10
if score>highscore(h)
new=1
repeat
highscore(hi)=highscore(hi-1)
dec hi
until hi=h
highscore(h)=score
h=10
endif
next h
ink rgb(255,255,255),0
if new=1
center text 320,50,"New Highscore: "+str$(score)
else
center text 320,50,"Your Score was: "+str$(score)
endif
center text 320,130,"HIGHSCORES"
for h=1 to 10
center text 320,130+(H*20),str$(highscore(h))
next h
center text 320,400,"Press any key to continue!"
suspend for key
goto start
endif
return