Game Development Reference
The careful eye will have noticed that the code for the width of a headlight uses a
backslash ( \ ) instead of a forward slash ( / ) when dividing by 4. The backslash is
an integer divide with fractions truncated. The result does not need type con-
version when assigned to an integer variable. We want narrow headlights, and we
do not care if they are one pixel narrower than rounding would call for.
Also worth noting is that our road is sideways. That means we have to deal with
the fact that height on the form turns into the width of our vehicles. The length of
our vehicles is the width of the controls that draw them. The drawing code will
also have to keep this transformation in mind. We will have our class speak in
terms of X position and vehicle length to avoid confusion.
The development environment will complain about Me.Draw( 200) because we
have not added that chunk of code. The 200 value will make more sense when
we see the positions we use to place the initial vehicles on the road. Without the
draw call, all the controls would wind up in the top-left corner of the form. This
way, we can take a glance at our starting data before things move. Add the
following code to the class.
Public Sub Draw(ByVal offset As Integer)
'Headlights go out twice our speed.
HeadLights.Width = 2 * currentV
'Position us in the proper lane.
Body.Top = VehicleWidth * (10 - 2 * myLane)
'Headlights same as the body.
HeadLights.Top = Body.Top
'Name tag above the body.
NameTag.Top = Body.Top - NameTag.Height
'And at the right spot along the way.
'Everything gets the offset.
'Body is a Label control. Its width is our
'vehicle's length. Our right edge is at X, so
'our left is our length further back.
Body.Left = Me.X - Body.Width - offset
'Headlights have their left edge at our position.
HeadLights.Left = Me.X - offset
'Center the name tag.
NameTag.Left = Body.Left + Body.Width \ 2 - NameTag.Width \ 2
'Show how fast we are going.
Body.Text = currentV.ToString