Hi again!
I am still working on my Space Invaders game and I was wondering whether somebody could have a look on my code.
Basically I managed to get an array of enemies to move and shoot bullets down the screen. I must have done something wrong with the bullets starting point, as sometimes they shoot from the space instead of enemies location.
Another thing is that when I shoot a bullet as player 1 and I hit one of the enemies, instead of deleting the image it gets pasted (?) multiple times and it looks like a crazy party with a stroboscope.
Do you think that I have done something wrong with my collision check?
Here is a bit of the code, if someone could have a look please:
void enemiesSetup(int alienX[], int alienY[])
{
// For loop that inserts a picture of enemy in each subscript in an array
for(int i=1;i<=37;i++)
{
dbCreateAnimatedSprite(i,"alien1.bmp",6,1,ALIEN_IMAGE);
dbSprite(i,alienX[i], alienY[i], ALIEN_IMAGE);
}
}
// Creates and controls enemies
void enemiesMove(int alienX[], int alienY[])
{
// Number of enemies on the screen
const int SIZE = 37;
// For loop that inserts a picture of enemy in each subscript in an array
for(int i=1;i<=SIZE;i++)
{
dbSprite(i,alienX[i],alienY[i], ALIEN_IMAGE);
dbPlaySprite(i,1,6,50);
// Check the ALIEN X position. If it is less
// than or equal to 0, mirror the ALIEN sprite.
if (alienX[1] <= 0)
{
dbMirrorSprite(i);
}
// If the ALIEN is at the right edge of the
// screen, mirror the ALIEN sprite.
if (alienX[35] + dbSpriteWidth(ALIEN_SPRITE) >= dbScreenWidth())
{
dbMirrorSprite(i);
}
// If the alien is on the right or left end of the screen, he will move down
if (alienX[35] > 580 || alienX[1] <= 0)
{
alienY[i] += ALIEN_MOVE;
}
// If the ALIEN is currently mirrored, move right.
if ( dbSpriteMirrored(ALIEN_SPRITE) )
{
alienX[i] += ALIEN_MOVE;
}
// If the ALIEN is not mirrored, move left.
else
{
alienX[i] -= ALIEN_MOVE;
}
}
}
void enemiesDie()
{
int bulletX = dbSpriteX (LASER1_SPRITE);
int bulletY = dbSpriteY (LASER1_SPRITE);
int enemyX;
int enemyY;
int iEnemySprite = 1;
for (int i = 0; i < 37; i++)
{
iEnemySprite++;
enemyX = dbSpriteX ( iEnemySprite );
enemyY = dbSpriteY ( iEnemySprite );
// have we scored a hit?
if ( ( bulletX > enemyX && bulletX < enemyX + 54 ) || ( bulletX > enemyX && bulletX < enemyX + 54 ) )
{
if ( ( bulletY > enemyY && bulletY < enemyY + 54 ) || ( bulletY + 9 > enemyY && bulletY + 9 < enemyY + 54 ) )
{
// delete the sprite
dbDeleteSprite( iEnemySprite );
dbDeleteSprite(LASER1_SPRITE);
}
}
}
}
void enemiesShoot(int alienX[], int alienY[])
{
// go through each enemy bullet 51 to 52
for ( int iBulletSprite = 51 ; iBulletSprite < 55 ; iBulletSprite++ )
{
// if the sprite exists then its in the game and needs to be updated
if ( dbSpriteExist ( iBulletSprite ) )
{
dbSprite ( iBulletSprite , dbSpriteX ( iBulletSprite ) , dbSpriteY ( iBulletSprite ) + 3 , 51 );
if ( dbSpriteY ( iBulletSprite ) > 640-9 )
dbDeleteSprite ( iBulletSprite );
}
else
{
enemiesAddBullet( iBulletSprite, alienX, alienY );
}
}
}
// time to add an enemy bullet
void enemiesAddBullet ( int iBulletSprite, int alienX[], int alienY[] )
{
int enemyX = dbRND(36);
// add a bullet on a random X and Y position
for ( int i = 36 ; i >= 0 ; i-- )
{
dbRandomize ( dbTimer() );
int iEnemySprite = (( i / 6 ) + enemyX);
dbSprite ( iBulletSprite , dbSpriteX ( iEnemySprite ), dbSpriteY ( iEnemySprite ) + dbSpriteHeight (iEnemySprite), 51 );
return;
}
}