#define ENEMIES 10
.
.
.
SDL_Surface *screen, *back, *lifeicon, *gameoverimg, *healthbar;
.
.
.
CSpriteBase enemybase; // The enemy sprite's base
CBasicEnemy *enemies[ENEMIES]; // Individual enemies
.
.
.
// Add an enemy onto the screen
void addEnemy()
{
// We loop through all the spots for enemies
for (int i=0; i < ENEMIES; i++)
{
// If the spot is free
if (enemies[i] == NULL)
{
// Make one enemy out of two
if (rand() % 2 == 0)
{
enemies[i] = new CEnemy2();
enemies[i]->init(&enemybase, screen);
// mark him as blue (moving up and down)
enemies[i]->setType(rand()%300+1);
}
else
{
enemies[i] = new CBasicEnemy;
enemies[i]->init(&enemybase, screen);
// mark him as purple
enemies[i]->setType(0);
}
// we stop the animation on the enemy sprite for safety
// (so that it wouldn's start blinking blue and purple all the time)
enemies[i]->stopAnim();
// We give the enemy a random y coordinate so that each enemy
// pops up at a different location...
enemies[i]->set(800, 40+rand()%470);
// We also give the enemy random speed and life values
enemies[i]->setSpeed(rand()%4+1);
enemies[i]->setLife(rand()%3+1);
// fill in the spot
enemies[i]->setVisible(1);
return;
}
}
}
.
.
.
// The main function...
int main(int argc, char *argv[])
{
.
.
.
enemybase.init("data/sprites", "enemy");
.
.
.
for (int i=0; i<ENEMIES; i++)
{
enemies[i] = NULL;
}
.
.
.
// Loop while done equals true
int done=0;
while (done == 0)
{
.
.
.
// If the game isn't paused nor over then
if (!paused && !gameover)
{
.
.
.
// If 3/4 seconds have passed since the last enemy came out
if (sdlgt-elast>750)
{
// Add a new enemy
addEnemy();
// Mark the time when this enemy came out
elast=(Uint32)sdlgt;
}
.
.
.
}
.
// We loop through all the spots for enemies
for (int i=0; i < ENEMIES; i++)
{
// If the spot is free
if (enemies[i] != NULL)
{
delete enemies[i];
enemies[i] = NULL;
}
}
.
.
return 0;
}