In the previous lesson we created a basic car class and used it to move around the map. In this lesson we'll be making the car's movement a bit better and populating some of the display panel's sections (time played, distance travelled, fuel, temperature).
The Car panel and the sections being populated in this lesson are shown in the image below:


If you are manually creating the project make file then use the one from lesson 3 as the same files are being used.
The following files will be changed in this lesson:


In the animations.xml file are references to the following graphics (indicated by the 'id' attribute): speedo, fuel, temperature
strips These are located in the graphics file transam.bmp and are the horizontal coloured strips. They are used to represent the speed of the car, the amount of fuel and the car's temperature.


The car will be controlled by the cursor keys. Cursors control turning and acceleration/braking. The Left Shift key can now be used as a hand-brake. In this release, it will simply slow down the car quicker.
The instruments/values in the game (on car panel as shown above) will function as follows: There is a link between temperature and speed. When speed enters the red area the temperature will increase, when the speed is in the yellow area the temperature remains the same, when the speed is in the green temperature will decrease. Once temperature reaches the maximum the car will cut-out and cannot be accelerated until the temperature is out of the danger red area. When the car has 'cut-out' it will reduce to a slow speed, making it vulnerable to attack.
For future versions we will be implementing some more features such as, collision with objects will result in the temperature hitting red (i.e. the car will cut-out) and damage sustained, and enemies will follow the car based on set rules and will use an A* algorithm for determing the route to take.
This version will see two control methods added (though in later lessons this is removed as I decided to stick to one!). Both methods alter the way acceleration works - the first mode will accelerate when up cursor is pressed and decelerate slowly when released, the second mode will not decelerate when released.

Code Walkthrough (game_vars.cpp/game_transam.h/game_transam.cpp)

The animations for the additional graphics are added. Additional animations are loaded very simply and are done so via the Animation class constructor in the SetupGame function in the game_transam.cpp file. For example, the temperature gauge is loaded as follows:
DisplayTemperature =new Animation(GameLibrary,"temperature");
Where 'temperature' is the animation referenced in the animations.xml file with the id attribute of 'temperature'.
So far all of our animations have actually been static graphics. In the next lesson we will add the menu and a proper animation will be used and a more optimised method of referencing static graphics shown.

Code Walkthrough (game_game.cpp)

This file contains our two automated callback functions: TransAmGameLogic and TransAmGameDrawing.
The changes to this file are minimal and are as follows:
  1. The TransAmGameLogic is updated to increase the 'time' entry. Because this function is guaranteed to be called every frame, and we have set the FPS to be 60, we can simply update a variable once per call and every 60 times we have one second. The Configuration object can give us the fps - GameConfiguration->CapsSystem.fps . Remember, 'GameConfiguration' is our variable initialised in the SetupGame function from the AXL Framework
  2. The TransAmGameDrawing is updated to draw the additional items. These are simply Allegro blit/draw_sprite functions using the Animations we have added. Remember the Animation public data member ReadOnly_CurrentItem returns the Animation's current graphic item. As a reminder, the destination bitmap, as always, is the AXL Framework's DrawingSurface. The DrawingSurface is determined by the config.xml file and will either be a memory or video bitmap and will be drawn to the screen using double buffering, triple buffering or page flipping.

Code Walkthrough (game_car.cpp/game_car.h)

First up, for all the new features we are adding, we need some variables. In the game_car.h header file we create new member variables for temperature, handbrake, distance travelled and a few constants to help us out with calculating the speed, temperature and fuel values.
The temperature and fuel values are calculated and increased/decreased according to the specification set out above in the 'TranzAm' section.
NextMove() is the car's main update method and is called once per frame from the TransAmGameLogic() function (as has always been the case). So it is clear (hopefully) that all updates to fuel, temperature, distance and speed will be done here too.
The changes to this method (sometimes the code in the lessons are best viewed using a file diff program - e.g. WinMerge - this is how I'm writing this tutorial, btw) are as follows: Note: all the code above was based on trial and error, there sometimes isn't a magic way of doing something!
The only odd thing about this function is the addition of the second driving mode 'simple acceleration' that was mentioned earlier. This simply adjusts the way the car is moved. To see it working in this version, alter the variable 'SimpleAcceleration' in the Car::SetupCar method at the top of the source file. In the previous lessons only one driving mode was available - this hasn't really changed, just moved around to accomodate this second mode.
And as it happens, later on you will see this second driving mode removed as I found it a bit pants.


Again, very little Allegro knowledge has been required, other than using the draw_sprite (masked blitting) and blit (non-masked blitting).
So, all that's required is to compile, run and see the changes. Note, there are no fuel pick-ups in the current version so when you run out of fuel, you will have to quit by pressing the ESCape key. In a later lesson we will making all the foreground objects on the map active (such as fuel and the collision items like trees, etc) and learning how to store custom values for tiles that can be used in code. I've also noticed this version sometimes crashes on exit, but rest assured it doesn't in later versions.


In the next lesson, the game will not change as such as we will be making a menu system. The menu system will be created to show you how the AXL Configuration class loads and saves values, how to create custom file based configuration items, how to change your game to use double/triple/paging buffering techniques, how to switch between windowed and fullscreen, how to change the game's FPS, how to use and up date an animation, plus a few more things.