Here is a copy of a post in that thread where I explained bullets:
I did my bullets like this:
Code:
#define MAX_BULLETS 10
struct bullet {
int x, y;
int exists;
} bullets[MAX_BULLETS];
Then every time right before I draw to the screen (well, the backbuffer) I call the function updatebullets() which goes through each bullet and subtracts from y, to make it move up the screen. I also have that function change exists to 0 if the bullet goes off the screen, later I will have the code for when a bullet hits something. I loop through it like this
Code:
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
bullets[i].y -= 5;
}
}
That code loops through the whole bullets array and for everyone that exists it subtract five from y, therfore all the bullets move up the screen.
When I create a bullet I have to loop through the bullets array and find the first empty space. If I had an integer for how many bullets are on the screen, and add to it everytime I add a bullet, and delete from it everytime a bullet disapears, I would overwrite them. Here is an example of how that would not work (using the bullet struct from above)
Code:
int NumberOfBullets = 0;
// Okay lets say the user presses space
// We create a bullet
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 300;
bullets[NumberOfBullets].y = 400;
int NumberOfBullets++;
// Now lets create another bullet
// Note the first bullets location would be bullets[0]
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 200;
bullets[NumberOfBullets].y = 450;
NumberOfBullets++;
// Now lets say some time has passed in our game, the first
// bullet is off screen now, but the second is still on screen.
// Now our code would break
// First since our first bullet went off screen we do this
NumberOfBullets--;
// Now what happends if we create a new bullet?
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 245;
bullets[NumberOfBullets].y = 420;
// Uh oh, we just over wrote our second bullet, they are both
// bullet[1].
I tried that method first, it would not work, obviously. So now everytime I create a bullet, I have it loop through the bullets array and find the first place where exists = 0; The code for this is not hard, I will show you.
Code:
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 0)
{
// We found a bullet in the array that does not exist yet
// meaning we can use that place to hold a bullet
// We want to give it coordinates, then set exists to 1 so
// it gets drawn, and nothing overwrites it
bullets[i].x = 200;
bullets[i].y = 200;
bullets[i].exists = 1;
// A problem I had earlier was that I did not put a break
// statement here, that made it create a bullet with the
// same coordinates every place there was a empty bullet
// which meant that I could only have one bullet at a time
break;
}
}
Well, I hope I explained everything, here is the code to my game so far, there are no enemies yet, but you can have up to 30 bullets on screen at once, there is no flicker in bullet movement or plane movement. I am still looking for a new plane graphic. I think I am gonna quit with this game, because it is starting to get lame. I need to design what I want the game to be more, before I code. I will work on another game shortly, probably either pong or a space attack game. Here is the full code to the airplane game:
Code:
#include <allegro.h>
#define MAX_BULLETS 10
// Draws ship and bullets to buffer, then
void redraw(); // blits buffer to screen
// Initializes Allegro, sets screen mode
void setup(); // and loads bitmaps
void shutdown(); // Destroys bitmaps and calls allegro_exit()
void updatebullets(); // Updates bullet coordinates
BITMAP *background = NULL; // Clouds background
BITMAP *bullet = NULL; // A bullet...
BITMAP *plane = NULL; // Da plane! Da plan!
BITMAP *buffer = NULL; // The buffer, reduces flicker
// I found the easiest way to implement bullets is to create
// an array of structures, so it is easy to keep track of
// each bullets coordinates and state (exists or not)
struct bullet
{
int x, y;
int exists;
} bullets[MAX_BULLETS];
// The ships coordinates, maybe I should make a ship structure...
int x = 300, y = 375;
int main()
{
setup();
while(!key[KEY_ESC])
{
if(key[KEY_UP] && y > 5) // Move up
y = y - 5;
else if(key[KEY_DOWN] && y < 400) // Move down
y = y + 5;
else if(key[KEY_LEFT] && x > 5) // Move left
x = x - 5;
else if(key[KEY_RIGHT] && x < 525) // Move right
x = x + 5;
else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
{
// Find an empty place in the bullets struct
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 0) // We found an empty slot
{
bullets[i].exists = 1; // Make it exist and set its coords
bullets[i].x = x;
bullets[i].y = y;
break;
}
}
}
updatebullets();
redraw();
}
shutdown();
return 0;
}
END_OF_MAIN();
void redraw()
{
blit(background, buffer, 0, 0, 0, 0, 640, 480);
draw_sprite(buffer, plane, x, y);
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
draw_sprite(buffer, bullet, bullets[i].x, bullets[i].y);
}
}
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
}
void setup()
{
allegro_init();
install_keyboard();
// Set colors depth and set graphics mode
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
// Load bitmaps
background = load_bitmap("background.bmp", NULL);
bullet = load_bitmap("bullet.bmp", NULL);
plane = load_bitmap("plane.bmp", NULL);
buffer = create_bitmap(640, 480);
// Make sure all bullets do not exist
for(int i = 0; i < MAX_BULLETS; i++)
{
bullets[i].exists = 0;
}
}
void shutdown()
{
destroy_bitmap(background);
destroy_bitmap(plane);
allegro_exit();
}
void updatebullets()
{
// Delete bullets that go past screen
for(int j = 0; j < MAX_BULLETS; j++)
{
if(bullets[j].exists == 1)
{
if(bullets[j].y <= 0)
bullets[j].exists = 0;
}
}
// Move bullets up the screen
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
bullets[i].y -= 5;
}
}
}
Here is a link to the whole thread: http://www.cprogramming.com/cboard/s...threadid=19231