This post is a portion of Part 9 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 9.0 – Revisiting Our Game
- 9.1 – Revisiting Our Game – The Player
- 9.2 – Revisiting Out Game – The Comet
- 9.3 – Revisiting Our Game – Explosions
We finish up this series part by adding explosions to our games.
Full source can be found here.
I am having more problems, I don’t know if it’s my compiler or what, but I edited your code in a few ways, mainly adding a bullet image and a Hearts display. It giving me about 22 errors. The code includes images so i’ll just include a link to the download.
http://www.mediafire.com/?a96dj9ccq6qaj0a
Thankyou in advance for helping me, because your tutorials ARE AWESOME!!!
I am having a slight problem with the explosions updating, they are drawn correctly but they sit on their first frame and never dissapear, i checked your downloadable code and couldn’t find any differences in the explosions code, i’m completly stumped :/
Øuch
In funkction UpdateCommet remember about incrementing frameCount when u are testing condition about delay.
if(++explosions[i].frameCount >= explosions[i].frameDelay)
These two + are very important ;p
UpdateExplosions, not UpdateCommet, sorry for mistake.
I have been working hard on making this work, either by trying to add the exact location of the item in the moment of it’s demise or of the object which destroys it in that moment, even tried to force it to receive the location of the destroyer when he gets a +1 in score, still nothing worked.
The way I use it, is first create a header with all of the objects, write their features like ID, boundy, boundx, x, y, images related to them etc, initiate them before the main as prototypes, put them inside the main where they are needed and then I complete their coding after the main.
It always crashes my game though, exactly when I draw the image of the certain object.
What I try to do is load an object of type blood, which takes from the players life when he goes through it again, part of the code is this :
[code]
void initBlood(Blood blood[], int size, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2, ALLEGRO_BITMAP *image3)
{
for (int i = 0; i <= size; i++)
{
blood[i].ID = BLOOD;
blood[i].live = false;
blood[i].boundx = 19;
blood[i].boundy = 19;
blood[i].image = image;
blood[i].image2 = image2;
blood[i].image3 = image3;
}
}
void drawBlood(Blood blood[], int size, int bloodCounter)
{
for ( int i = 0; i <= size; i++)
{
if(blood[i].live)
{
if(bloodCounter == 0)
{
int imageWidth = al_get_bitmap_width(blood[i].image);
int imageHeight = al_get_bitmap_height(blood[i].image);
al_draw_bitmap(blood[i].image, blood[i].x – imageWidth/2, blood[i].y – imageHeight/2, 0);
bloodCounter = rand() % 2;
} else if ( bloodCounter == 1)
{
int imageWidth = al_get_bitmap_width(blood[i].image2);
int imageHeight = al_get_bitmap_height(blood[i].image2);
al_draw_bitmap(blood[i].image2, blood[i].x – imageWidth/2, blood[i].y – imageHeight/2, 0);
bloodCounter = 2;
} else if ( bloodCounter == 2)
{
int imageWidth = al_get_bitmap_width(blood[i].image3);
int imageHeight = al_get_bitmap_height(blood[i].image3);
al_draw_bitmap(blood[i].image3, blood[i].x – imageWidth/2, blood[i].y – imageHeight/2, 0);
bloodCounter = rand() % 2;
}
}
}
}[/code]
In this second variant, I entirely skip the creation of another function and I make a way to just automatically make it when it dies.
Note I have plenty other images loaded and the game works properly.
[code]void initBlood(Blood blood[], int size, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2, ALLEGRO_BITMAP *image3)
{
for (int i = 0; i <= size; i++)
{
blood[i].ID = BLOOD;
blood[i].live = false;
blood[i].boundx = 19;
blood[i].boundy = 19;
blood[i].image = image;
blood[i].image2 = image2;
blood[i].image3 = image3;
}
}
void updateSquirrel(Squirrel squirrel[], int size, Bunny &bunny, Blood blood[], int dSize)
{
for(int i = 0; i <= size; i++)
{
if (squirrel[i].live)
{
if(
squirrel[i].x – squirrel[i].boundx/2 bunny.x – bunny.boundx/2 &&
squirrel[i].y – squirrel[i].boundy/2 bunny.y – bunny.boundy/2
)
{
// for ( int f = 0; f <= dSize; f++)
// {
blood[1].live = true;
blood[1].x = squirrel[i].x;
blood[1].y = squirrel[i].y;
if(blood[1].live == true)
{
int imageWidth = al_get_bitmap_width(blood[i].image3);
int imageHeight = al_get_bitmap_height(blood[i].image3);
al_draw_bitmap(blood[i].image, blood[i].x – imageWidth, blood[i].y – imageHeight, 0);
}
// }
bunny.score++;
squirrel[i].live = false;
}
}
}
}[/code]
Ehem, I apologize, but the cooment in truthful, I copied it from another place I posted it, what I am doing is using your tutorials to have a base to make my own game, but when I try to load an object of type blood in the location, in the moment, another object dies, …my game dies, Could you please help? Thank you
Really sorry for so many reposts, with very badly written comments, anyhow, kind of fixed it, but not, I only get the menu, then nothing else happens but the background and menu screen, which I create before the game starting => loading the blood, something is wrong but I don’t see what. Even if I turn everything off, it only works again if I turn initBlood(); off as well
I so wish I could delete the comments, thank you so much for the tutorials, I fixed it eventually, it was a logic of the code error, compiler wise it was working, but it was creating a small bug which, in it’s turn, killed everything, thanks again