Game Development Reference
SlowLineShape.Y1 = Vehicle.VehicleWidth * 19 \ 2
SlowLineShape.Y2 = SlowLineShape.Y1
'Get the fast line into place (it moves).
FastLineShape.X1 = 0
FastLineShape.X2 = Me.Width
FastLineShape.Y1 = Vehicle.VehicleWidth * 19 \ 2 - _
2 * Vehicle.VehicleWidth * Lanes()
FastLineShape.Y2 = FastLineShape.Y1
Private Sub LanesUpDown_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LanesUpDown.ValueChanged
'The AI can think for itself. The fast lane stripe needs our help.
Call Road_Resize(Nothing, Nothing)
If you added the lines, run the code in the debugger and change the number of
lanes. The fast line should respond correctly to the number of lanes specified by
the control. The slow line should be in place half a car width below the line of
cars. If you did not add the lines, there will not be any visible effect until we add
As a precursor to the AI, we need to add some helper code to the code in Road.vb.
We will add two routines: one to sort the cars and another to check for collisions.
A side effect of sorting the cars is that we can figure out how to set the scrollbar so
that when the simulation is stopped, we can scroll around and see all the vehicles.
Add the following code to Road.vb:
'The AI and the collision detection need a sorted list.
Private Sub SortToys()
Dim swapped As Boolean = True
Dim Behind As Vehicle
Dim Ahead As Vehicle
'This is the sorting loop.
swapped = False
Dim i As Integer
Fori=1ToToyBox.Count - 1
'The back has a lower subscript.
Behind = CType(ToyBox(i), Vehicle)