Chapter 7

7. Action

If you downloaded the tutorial data file before Section 2 of this chapter was written (Feb 28), please redownload it (don't forget to regenerate tutorial.h), because three sound effects have been added to it to support Section 2. There will also be another update to support Section 3 soon.

7.1 Explosions

There are few action games without any explosions, and we don't really want to be worse, we want A LOT of explosions. We can't have any of that fully armed bombs falling into the ground and magically vanishing without an explosion, can we? Now, an explosion effect is an independent effect that can be triggered by a bomb, but then exists independent of it. Thus, we need to write an explosion class, and launch one whenever the bomb hits the ground.

Since the code is getting large, I will not show it in its entirety unnecessarily. So, at the top, add

#define BOOM_DELAY 5
and add this class before the projectile class
class explosion : public sprite {
 int frame,delay,magnitude;
 explosion(fix _X,fix _Y,int mag)
  : sprite(_X,_Y,(RLE_SPRITE*)data[TUT_BOOM1].dat) {
  magnitude=mag; frame=0; delay=0;
 virtual void draw(BITMAP*dest) {
 virtual bool animate() {
  if ((delay++)>BOOM_DELAY) {
   if ((frame++)>magnitude) return TRUE;
  return FALSE;
Here, the magnitude field tells how many frames of the explosions should be shown, and the draw() method is overridden to show the explosion with X,Y representing the center position instead of the top-left position of the sprites, mostly because our explosion sprites have different sizes but they are all centered.

To make it easy for the code to choose which graphics to show, we have ordered the sprites (in the data file) by numbering each BOOM sprite, thus making it easy to choose the correct image by just adding the frame number we want to BOOM1, and showing that. Since the Grabber automatically sorts the data file, this is very handy for animations where several frames has to be shown in sequence.

Now, we need an explosion to be activated when a bomb hits the ground, so we will need to change the projectile class to:

class projectile : public sprite {
 fix DX,DY;
 int force;
 projectile(fix _X,fix _Y,fix _DX,fix _DY,RLE_SPRITE*img,int power)
  : sprite(_X,_Y,img) { DX=_DX; DY=_DY; force=power; }
 virtual bool animate() {
  move(DX,DY); DY+=GRAVITY;
  if (Y+image->h>=MAX_Y) {
   sprites.push_back(new explosion(X+image->w/2,Y+image->h,force/2));
   return TRUE;
  } else return FALSE;
When a bomb hits the ground now, it will spawn an explosion with a magnitude that is one half of the assigned explosive power (which we set to 5 for a bomb, so the magnitude will be 2).

7.2 Sound Effects

With Allegro, this is trivial. First, we must install sound support. After install_keyboard(); (or wherever) in main(), insert
to install the Allegro Sound System. Now, we will trigger a neat explosion sound whenever an explosion is set off. Change the explosion constructor to read:
 explosion(fix _X,fix _Y,int mag)
  : sprite(_X,_Y,(RLE_SPRITE*)data[TUT_BOOM1].dat) {
  magnitude=mag; frame=0; delay=0;
Here, we stop any previously playing explosion sounds (otherwise the result might get *very* loud and distorted, especially considering the quantity of bombs we are able to pour out), then start a new one. The loudness is here 255 (max), the pan (0-255) is set according to the X position of the explosion for those lucky enough to have a stereo sound card, and the frequency is normalized (1000), and it should not loop. (The int typecast in the pan calculation is for avoiding a fixed-point overflow.)

For fun, you may try the other two sound effects I put in the datafile, TUT_BANG1 and TUT_BANG2, but these were sampled from videos by a friend, so they are probably copyrighted.

7.3 Enemies

7.4 Collision Detection

These have not been written yet. Please be patient. Meanwhile, try Allegro Vivace, the related How To Program Games, Allegro FAQ and Coding Techniques, SGI STL Reference, and so on.

If you have any suggestions for improvement so far, send them to