1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
bool fireCannonBall(int cannon_x, int cannon_y,int targets[], int target_x[],int target_y[], int no_targets)
{
int obj_no1 = 0;
int obj_no = 0;
int i = 0;
int ball_x = 0;
int ball_y = 0;
int target_width = 40;
int target_height = 40;
int j = 0;
int score = 0;
int shots = 0;
bool Hit = false;// set true when a hit occurs
//equation for ball coordinates
ball_x = cannon_x + 20;
ball_y = cannon_y - 1;
//display cannon
obj_no = displayBMP("cannon.bmp", cannon_x, cannon_y);
obj_no1 = drawCircle(5, ball_x, ball_y);
do
{
//Move to top of screen
if (up())
{
Hit = false;//
ball_y = cannon_y - 1;// reset ball_y for next shot
shots = shots+1;
for (i = ball_y; i>=0; i--)
{
//drawing circle
ball_x = cannon_x + 20;
moveObject(obj_no1, ball_x, ball_y--);
for (j = 0; j < no_targets; j++)
{
if (( ball_x > target_x[j]) && (ball_x < target_x[j] + target_width) &&
(ball_y > target_y[j]) && (ball_y < target_y[j] + target_height))
{
removeObject(targets[j]);
removeObject(obj_no1);
obj_no1 = drawCircle(5, ball_x, cannon_y);
Hit = true;// time for cannonball to stop falling
}
}
if( Hit )
{
score = score + 1;
if (score == 10)
{
break; break;
}
break;// exit the for loop
}
}
}
//move right
if (right())
{
cannon_x++;
moveObject(obj_no,cannon_x, cannon_y);
moveObject(obj_no1, cannon_x+20, cannon_y);
}
//move left
if (left())
{
cannon_x--;
moveObject(obj_no, cannon_x, cannon_y);
moveObject(obj_no1, cannon_x+20, cannon_y);
}
}while(true);
}
|