Game Development Reference
In-Depth Information
Figure 8.3
An initial test run of Cars and Trucks.
directly in front of it, it could hardly be hitting any vehicle in front of both of
them. We will have to sort the toy box every time we want to go through it in
order, but the sort should be quick because the list retains a great deal of order
between sorts. Run the code in the debugger. You should see something
resembling Figure 8.3.
If you look at the X positions we loaded and hunt for the 200 value in the list,
you will see that there is a 200-pixel-long truck located at X=0. On the left side of
the form, we see the tail end of that truck at 200. The 200 number we fed to
the initial Draw() calls in New() was picked for this reason. We get a static view. If
we extend the size of the form, we can see the two trucks up ahead, but there is
no way to see the convoy of vehicles behind. They would be a lot easier to see if
they were moving.
Movement and Animation
We loaded all the vehicles at a speed of 50 pixels per second. Without any AI, they
cannot change speed. If we did not put any of them on top of another, we can
defer collision detection until after we get them to move.
We will program in a variable frame rate. Each frame, every vehicle, starting from
the front, is moved forward by its speed in pixels per second divided by the frame
rate. Floating-point math keeps track of fractions for us, giving us some freedom
in setting the frame rate. The upper limit to frame rate will depend on the
particulars of your system. After moving everything, we draw it in its new place.
Animation frames are initiated by a timer. We will start with the user-interface
elements. Switch to the Design view of Road.vb; then follow these steps:
1. A glance at Figure 8.2 may be helpful as you place controls. Drag a Label
control from the Toolbox to the upper-right corner of the form. Change its
Search Nedrilad ::

Custom Search