Remove aliens when they have been hit! So far the code i have works it just will
ID: 3558754 • Letter: R
Question
Remove aliens when they have been hit!
So far the code i have works it just will not remove the aliens when they have been hit! Please help me
(USEFUL INFO:Compare the x/y-coordinate for the ball with the upperlef/lower-right coordinates for the target using the following rule:
if ( ( ball_x > target_x[i]) && (ball_x < target_x[i] + target_width) &&(ball_y > target_y[i]) && (ball_y < target_y[i] + target_height) )
Where i is the loop control variable and target_width/target_height are the width/height for the target (the target_width and target_height is 40 because the target is a 40x40 bitmap image).
the code i have so far is:
bool fireCannonBall(int cannon_x, int cannon_y,int targets[], int target_x[],int target_y[], int no_targets)
{
//Variable Declaration/initialization
int i = 0;
int x = 0;
int y = 440;
int ball_obj=0;
int ball_x = 20;
int ball_y = 440;
int cannon_obj=0;
int target_width=40;
int target_height=40;
//display cannon
cannon_obj=displayBMP("cannon.bmp",cannon_x,cannon_y);
//allow the cannon to be moved left/right
while(true)
{
//Allow cannon to move left/right
if (left())
{
x--;
}
if (right())
{
x++;
}
//Check for boundary conditions
//Clip to 0/600
if (x <= 0)
x = 0;
if (x + 44 >= 639)
x = 595;
//Move the object
moveObject(cannon_obj,x,y);
//Test for fire
if (up())
{
//Display the ball
ball_obj = drawCircle(5,x+ball_x,ball_y);
//Move the ball up
for (i = 0; i < 440; i++)
{
moveObject(ball_obj,x+ball_x,ball_y--);
if(( ball_x > target_x[i]) && (ball_x < target_x[i] + target_width)&&(ball_y > target_y[i]) && (ball_y < target_y[i] + target_height) )
{
//Remove the ball
removeObject(ball_obj);
//Remove the target
removeObject(targets[i]);
}
else if(ball_y<=0)
{
//Remove the object
removeObject(ball_obj);
}
}
//Break out of loop
break
}
}
return 0;
}
Explanation / Answer
if ( ( ball_x > target_x[i] || ball_x < target_x[i]) && (ball_x < target_x[i] + target_width/2 || ball_x > target_x[i] - target_width/2) &&(ball_y > target_y[i] || ball_y < target_y[i]) && (ball_y < target_y[i] + target_height/2 || ball_y > target_y[i] - target_height/2) )