I want to make Breakout, but I am having problems destroying the bricks upon collision with the ball, all the bricks disappear upon collision, however, I only want the hit brick to disappear, how can I achieve that? (I am using allegro and C++) The Bricks class:-
class Bricks{
public:
int x;
int y;
bool isDestroyed;
ALLEGRO_BITMAP *brick;
}Brickss[40];
The draw function:-
void draw(){
al_draw_bitmap(background, 0, 0, 0);
al_draw_bitmap(paddle, paddleXPos, paddleYPos, 0);
al_draw_bitmap(ball, ballXPos, ballYPos, 0);
for (int drawBricks = 0; drawBricks < 8; drawBricks++){
for (Brickss[drawBricks].x = 0; Brickss[drawBricks].x < 1000; Brickss[drawBricks].x += 120){
Brickss[drawBricks].y = 0;
if (Brickss[drawBricks].isDestroyed == false){
al_draw_bitmap(Brickss[drawBricks].brick, Brickss[drawBricks].x, Brickss[drawBricks].y, 0);
}
}
}
al_flip_display();
}
The main function:-
int main(){
//initialize all what we need in allegro and load all the pictures(background,ball,paddle,bricks)
init();
loadBitmaps();
while (!done){
al_clear_to_color(al_map_rgb(255, 255, 0));
ALLEGRO_EVENT ev;
al_wait_for_event(eventqueue, &ev);
al_start_timer(timer);
//motion of the ball
if (ev.type == ALLEGRO_EVENT_TIMER){
moveBall();
}
//motion of the player
if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT){
paddleXPos -= movePaddleX;
}
if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT){
paddleXPos += movePaddleX;
}
//bounce back if the ball hits the paddle
if (intersects(ball, paddle, ballXPos, ballYPos, paddleXPos, paddleYPos)){
moveBallX *= -1;
moveBallY *= -1;
}
for (int drawBricks = 0; drawBricks < 8; drawBricks++){
for (Brickss[drawBricks].x = 0; Brickss[drawBricks].x < 1000; Brickss[drawBricks].x += 120){
Brickss[drawBricks].y = 0;
if (intersects(ball, Brickss[drawBricks].brick, ballXPos, ballYPos, Brickss[drawBricks].x, Brickss[drawBricks].y)){
Brickss[drawBricks].isDestroyed = true;
}
}
}
//draw everything to the backbuffer
al_set_target_backbuffer(display);
draw();
}
al_flip_display();
al_rest(10);
}