于在VisualBasic中实现最短路径搜索的简单解决方案:
GameAI:支持一个二维数组格式的自动路径查找!
DimaAsNewAStar'thepathfindingclass
DimmAsNewMathHelper'somerotationandcalculationfunctions
DimlAsNewLineController'thelineofviewhelper
DimvAsNewVelocityController'movementcontroller
DimnCountAsLong'counter,usedforthetimerdelayfunctions
DimbCountAsBoolean'flag,usedforthetimerdelayfunctions
DimMap(10)AsString*10'usedtodefineourlabyrinthstartingvalues
PrivateSubCommand1_Click()
'
'Functionsfindsthepathfromx1,y1tox2,y2'
'
Dimx1AsLong'positions
Dimy1AsLong
Dimx2AsLong
Dimy2AsLong
DimxAsLong
DimyAsLong
P1.Cls'redrawourmace
plotMace
x1=Format(Text1.Text)'getthepositionsfromtheGUI
y1=Format(Text2.Text)
x2=Format(Text3.Text)
y2=Format(Text4.Text)
IfCheck1.Value=vbCheckedThen'isdiagonalmovementallowed?
a.Allow8DirectionsTrue
Else
a.Allow8DirectionsFalse
EndIf
a.SetHeightVal(Text5.Text)'settheplayer'sheightandwidth
a.SetWidthVal(Text5.Text)
Ifa.FindAPath(x1,y1,x2,y2)=FalseThen'findthepath
MsgBox"Nowaytothegoal"'sorry,nopathtothegoal
ExitSub
EndIf
WhileNota.GoalReached'drawthepathtothescreenuntilgoalreached
x=a.NodeGetX'getcurrentxposition
y=a.NodeGetY'getcurrentyposition
Fori=0ToVal(Text5.Text)-1
Forj=0ToVal(Text5.Text)-1
P1.Circle((x+j)*10+5,(y+i)*10+5),4,RGB(0,255,255)
Nextj
Nexti
a.NextPathNode'steptonextpathposition
Wend
EndSub
PrivateSubCommand2_Click()
'
'calculatetheanglebetweenstartandendpoint'
'
plotMace
MsgBoxFormat(m.Angle2D(Val(Text1.Text),Val(Text2.Text),_
Val(Text3.Text),Val(Text4.Text)))
EndSub
PrivateSubCommand3_Click()
'
'calculatetheanglebetween2angles'
'
Dima1AsLong
Dima2AsLong
a1=Format(InputBox("Angle1:","AbsoluteDifferencebetween2Angles","0"))
a2=Format(InputBox("Angle2:","AbsoluteDifferencebetween2Angles","90"))
MsgBoxFormat(m.AbsAngleDiff(a1,a2))
EndSub
PrivateSubCommand4_Click()
'
'calculatethedistancebetweenstartandendpoint'
'
DimdAsLong
plotMace
d=m.Distance2D(Val(Text1.Text),Val(Text2.Text),Val(Text3.Text),Val(Text4.Text))
MsgBox"Distance:"&Format(d),"Distancebetween2Points"
EndSub
PrivateSubCommand5_Click()
'
'drawadirectlinebetweenstartandendpointandcheckifwewalkthroughwalls'
'
DimxAsLong
DimyAsLong
DimtAsLong
plotMace
IfDrawLine(Val(Text1.Text)*10+5,Val(Text2.Text)*10+5,Val(Text3.Text)*10+5,Val(Text4.Text)*10+5)Then
MsgBox"Startpointcanseethegoal",vbInformation,"LineofView"
Else
MsgBox"Heygoal,whereareyou,Ican'tseeyou.",vbCritical,"LineofView"
EndIf
EndSub
PrivateFunctionDrawLine(x1AsLong,y1AsLong,x2AsLong,y2AsLong)AsBoolean
'
'drawsadiretlinefromx1,y1tox2,y2andreturnsfalse,ifthelinewalks'
'throughwalls.Thelineispaintedredonwalls.'
'
DimxAsLong
DimyAsLong
DimtAsLong
DimbCanSeeGoalAsBoolean
bCanSeeGoal=True
l.InitLinex1,y1,x2,y2
Do