I am having an issue with some code. I am using Allegro 4.1.18 with DevC++ 4.9.9.2. I am working on a Space Invaders clone. I started having trouble with firing multiple missles. Drawing one is fine; it's one when I introduce multiple missles that I have the problem. I created a separate test project to troubleshoot. All the test project does is draw missles when space is pressed. The problem is, the missles come out very fast and the spacing is not constant - you have to barely touch space to only get one missle to come out. The missles are overlapping or are spaced erratically. I would like the missles to have uniform spacing when they fire. I am posting this test code. Any help would be appreciated. I can't figure out if it the timing, the animation, both or bad code. Thanks main.h
//define the sprite structure
typedef struct SPRITE
{
int alive;
int x,y;
int num;
int width,height;
int xspeed,yspeed;
int xdelay,ydelay;
int xcount,ycount;
int curframe,maxframe,animdir;
int framecount,framedelay;
}SPRITE;
SPRITE *bullet[4];
SPRITE mybullet[4];
BITMAP *bullet_bmp;
BITMAP *buff;
void init();
void deinit();
void update_bullets();
void update(SPRITE *spr);
void fire_weapon();
main.c
#include <allegro.h>
#include "main.h"
void fire_weapon()
{
int n;
for (n=0; n<4; n++)
{
if (!bullet[n]->alive)
{
bullet[n]->alive++;
bullet[n]->x = 320;
bullet[n]->y = 400;
return;
}
}
}
void update(SPRITE *spr)
{
if(!spr->alive){
return;
}
if(spr->y <=0)
{
spr->alive = 0;
return;
}
spr->x += 0;
spr->y -=20;
}
//Updates and draws the bullet
void update_bullets()
{
int n;
//update/draw bullets
for (n=0; n<4; n++)
if (bullet[n]->alive)
{
update(bullet[n]);
//blit(bullet_bmp, buff, 0, 0, bullet[n]->x, bullet[n]->y, 15, 17);
draw_sprite(buff,bullet_bmp, bullet[n]->x, bullet[n]->y);
}
}
int main() {
int n;
init();
//Create the backbuffer
buff = create_bitmap(640, 480);
bullet_bmp = load_bitmap("bullet.bmp", 0);
//initialize the bullet sprites
for (n=0; n<4; n++)
{
bullet[n] = malloc(sizeof(SPRITE));
bullet[n]->alive = 0;
bullet[n]->height = bullet_bmp->h;
bullet[n]->width = bullet_bmp->w;
bullet[n]->x = 0;
bullet[n]->y = 0;
}
while (!key[KEY_ESC]) {
/* put your code here */
//We don't have a background, so just clear the buffer to black
clear_to_color(buff, makecol(0,0,0));
if(key[KEY_SPACE])
{
fire_weapon();
}
update_bullets();
//Draw the contents of the backbuffer to the screen
blit(buff, screen, 0, 0, 0, 0, 640, 480);
rest(60);
}
destroy_bitmap(bullet_bmp);
free(bullet);
deinit();
return 0;
}
END_OF_MAIN();
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
}
void deinit() {
clear_keybuf();
}