Game Development Reference
This code deals with the sideways road, but it needs help from the class in terms
of the X position. Our virtual world is one of integer values, usually in terms of
pixels, made possible by our flat, 2D simulation. We use a floating point value to
store position so that we can accumulate fractions of a pixel of motion because
our frame rate and speeds do not always divide evenly. Aside from that, every-
thing is integer pixels, so we will provide a function that converts our floating-
point position to the closest integer. While we are doing that, we will provide the
rest of the functions used to get read-only access to the internal data of the
vehicle. Add the following code to the Vehicle class.
Public Function ID() As String
'Where along the road is it?
Public Function X() As Integer
'How fast am I going?
Public Function Speed() As Integer
'How long is my car?
Public Function Length() As Integer
'What lane am I in?
Public Function Lane() As Integer
That gives us a good start on vehicles. We can test the code by adding some code
to the Road form to create a few vehicles. We need to change our focus from
the Vehicle class to the Road form. View the code of the Road.vb and add the
#Region "Public Stuff"
'A place to keep our car collection.
Public ToyBox As New Collection