C++ Game Programming
Basic C++ Lessons
This set of lessons will give you a simple introduction to programming in the C++ language.
Allegro Lessons
This set of lessons will demonstrate how to use the Allegro graphics library to create video games.
Game Source Code
Here you will find source code to several games so that you can see the code in action.
Recommended Books
Here is a list of books which should help you with C++

Backgrounds
Now that we've got basic animations out the way, lets look into adding a background to our game. In the Pong example, every time the ball or a paddle moved, a black circle or rectangle took the place of the objects last position so that the player would not see a trace of it. What if we have a multicolored background though? Easy, using a technique known as "dirty rectangles" we can easily replace the background with the right color/image. For this we create two 2-dimensional arrays. One will be the background, and the other will contain the objects that will be moving around. This way we can check an objects last position and know what color/image to fill that portion of the screen with. It might sound some what confusing, hopefully an example will help answer any questions.

Here is an example for a tile based game using dirty rectangles. You will need the box image.


#include <allegro.h>

BITMAP* box;
BITMAP* buffer;

//These will hold the player's position
//instead of using pixels, we will use the position within the 2d array
int x = 15;
int y = 11;

int tempX = 15;
int tempY = 11;

//This will hold the value of how many boxes are left outside of the holes.
int boxes;

//This will be our background, 1 = clouds, 2 = brick, 3 = floor, 4 = hole
int map[24][32] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,4,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

//This will contain all the objects, 100 = player, 101 = box
int objMap[24][32] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

void setupGame(){
 
    buffer = create_bitmap( 640, 480);
    box = load_bitmap( "box.bmp", NULL);
    boxes = 1;
    
    for (int i = 0; i <= 24; i++){
     
        for( int t = 0; t <= 32; t++){
            
            if( map[i][t] == 1) rectfill( buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol( 128, 255, 255));
            else if( map[i][t] == 2) rectfill( buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol( 255, 128, 0));
            else if( map[i][t] == 3) rectfill( buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol( 255, 0, 0));
            else if( map[i][t] == 4) rectfill( buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol( 0, 0, 0));
            
           }       
        
    }  
    
    for (int i = 0; i <= 24; i++){
     
        for( int t = 0; t <= 32; t++){
            
            if( objMap[i][t] == 100) circlefill( buffer, (t * 20) + 10, (i * 20) + 10, 10, makecol( 255, 255, 0));
            else if( objMap[i][t] == 101) draw_sprite( buffer, box, t * 20, i * 20);
            
           }       
        
    }   
    
    draw_sprite( screen, buffer, 0, 0);
          
}    

void movePlayer(){
    
    tempX = x;
    tempY = y;
    
    if ( key[KEY_UP] && map[y - 1][x] == 3){
        
        if( objMap[y - 1][x] != 101){ 
                --y;
        } else if( objMap[y - 1][x] == 101 && map[y - 2][x] == 3 || objMap[y - 1][x] == 101 && map[y - 2][x] == 4){
        
            if (map[y - 2][x] == 4) --boxes;
        
            objMap[y - 2][x] = 101;
            draw_sprite( buffer, box, x * 20, (y - 2) *20);
            --y;
        }    
                
    } else if ( key[KEY_DOWN] && map[y + 1][x] == 3){
        
        if( objMap[y + 1][x] != 101){ 
                ++y;
        } else if( objMap[y + 1][x] == 101 && map[y + 2][x] == 3 || objMap[y + 1][x] == 101 && map[y + 2][x] == 4){
        
            if (map[y + 2][x] == 4) --boxes;
        
            objMap[y + 2][x] = 101;
            draw_sprite( buffer, box, x * 20, (y + 2) *20);
            ++y;
        }  
                
    } else if ( key[KEY_RIGHT] && map[y][x + 1] == 3){
        
        if( objMap[y][x + 1] != 101){ 
                ++x;
        } else if( objMap[y][x + 1] == 101 && map[y][x + 2] == 3 || objMap[y][x + 1] == 101 && map[y][x + 2] == 4){
        
            if (map[y][x + 2] == 4) --boxes;
        
            objMap[y][x + 2] = 101;
            draw_sprite( buffer, box, (x + 2) * 20, y * 20);
            ++x;
        } 
                
    } else if ( key[KEY_LEFT] && map[y][x - 1] == 3){
        
        if( objMap[y][x - 1] != 101){ 
                --x;
        } else if( objMap[y][x - 1] == 101 && map[y][x - 2] == 3 || objMap[y][x - 1] == 101 && map[y][x - 2] == 4){
        
            if (map[y][x - 2] == 4) --boxes;
        
            objMap[y][x - 2] = 101;
            draw_sprite( buffer, box, (x - 2) * 20, y * 20);
            --x;
        } 
                
    }
      
    
    acquire_screen();
    
    rectfill( buffer, tempX * 20, tempY * 20, (tempX + 1) * 20, (tempY + 1) * 20, makecol( 255, 0, 0));
    
    circlefill( buffer, (x * 20) + 10, (y * 20) + 10, 10, makecol( 255, 255, 0));
    
    draw_sprite( screen, buffer, 0, 0);
    release_screen();
    
    objMap[tempY][tempX] = 0;
    objMap[y][x] = 101;
    
    if ( !boxes){
        textout_ex( screen, font, "You Win!!!", 320, 240, makecol(0, 0, 255), makecol(0, 0, 0));   
    }    
    
    rest(100);
}    

int main(){
    
    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    setupGame();
    
    while( !key[KEY_ESC]){
     
         movePlayer();
           
    }    
    
    return 0;
}
END_OF_MAIN();    
This is a simple version of sokoban, a game where the player tries to push all the boxes on the stage into the holes provided. It used 20x20 pixel tiles. Lets take a closer look at the code.

Here I am using two 2D arrays to keep track of what needs to be updated. We will look into creating a map editor later on so that we won't have to type out so many numbers in order to create a map. One thing you should notice about the map is that when checking a specific tile you must use arrayName[y][x], the x and y are inverted (unless if you want to declare the 2D array as arrayName[32][24], this would make editing a bit of a hassle).

main() has the usual setup. setupGame() draws the screen and sets how many boxes will be in the current level, since this is intended to show dirt rectangles, I only included one.

movePlayer() controls the player, moves the box when it is pushed by the player, and declares the player a winner if he succeeds.

When the player presses an arrow key, movePlayer() checks to see if there is anything in tile the player want to move to. If there is nothing, it will check for a box, if there isnt one, it will move the player. If there is a box, it will check the tile where the box will be pushed to. If that tile is empty, or a hole, it will move the box and subtract it from the total number of boxes left in the game.

Afterwards, it draws the player and box to screen, and covers up the location the player used to be in (We do not need to bother with the tile the box was in since it will be occupied by the player). After this it will update the players location on the object map and declare you a winner if there are no more boxes left.

Google