Game Development Reference
In-Depth Information
The code needs to be called to be effective. We will call it when the ThinkTimer
ticks. To see that it is working, we will turn on the scrollbar. Add the following
code to Road.vb:
Private Sub ThinkTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ThinkTimer.Tick
'Passing happens; we need this to think, not to draw.
Call SortToys()
'Real AI code goes here.
End Sub
Private Sub PanScrollBar_Scroll(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _
PanScrollBar.Scroll
'Redraw at our new place.
Dim Toy As Vehicle
For Each Toy In ToyBox
Toy.Draw(PanScrollBar.Value)
Next Toy
End Sub
Run this code, making sure to start the simulation. Let it run a second or two and
then stop the simulation. The scrollbar should allow you to scroll back to the F1+
vehicle way in the back. Going the other way should take you to the lead truck.
The call to SortToys() also calls CollisionDetect() . The AI needs the sort, so we
must sort before the AI runs because the animation moved the vehicles, and they
could have been passing each other. However, when debugging, you can call the
sort after every animation frame to make sure that everything works and no
collisions have taken place. Be sure to watch the Immediate window for
debugging output; the collision message will go there.
It is time to add that real AI code promised by the comment in the ThinkTimer
Tick event handler. Replace that comment with the following code:
'Now do the AI.
Dim Toy As Vehicle
Dim i As Integer
'Debug.WriteLine("Thinking...")
'Run the AI, front to back.
For i = ToyBox.Count To 1 Step -1
Toy = CType(ToyBox(i), Vehicle)
 
Search Nedrilad ::




Custom Search