James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

BreakoutEngine/BreakoutEngine.cpp

Committer:
jamesheavey
Date:
2019-04-24
Revision:
27:1b5038b0a7a2
Child:
29:5168318d3e88

File content as of revision 27:1b5038b0a7a2:

#include "BreakoutEngine.h"


    
BreakoutEngine::BreakoutEngine()
{

}

BreakoutEngine::~BreakoutEngine()
{

}

void BreakoutEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
{
    // initialise the game parameters
    _paddle_width = paddle_width;
    _paddle_height = paddle_height;
    _ball_size = ball_size;
    _speed = speed;
    _number_left = 18;

    // y position on screen - WIDTH is defined in N5110.h
    _p1y = HEIGHT - GAP;

    // puts paddles and ball in middle
    _p1.init(_p1y,_paddle_height,_paddle_width);
    _ball.init(_ball_size,_speed);
    
    _brick11.init(3,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);                              // need to figure out how to make a list of these
    _brick12.init(16,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
    _brick13.init(29,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
    _brick14.init(42,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
    _brick15.init(55,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
    _brick16.init(68,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
    
    _brick21.init(3,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);                              // need to figure out how to make a list of these
    _brick22.init(16,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
    _brick23.init(29,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
    _brick24.init(42,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
    _brick25.init(55,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
    _brick26.init(68,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
    
    _brick31.init(3,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);                              // need to figure out how to make a list of these
    _brick32.init(16,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
    _brick33.init(29,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
    _brick34.init(42,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
    _brick35.init(55,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
    _brick36.init(68,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
    
    //listofBricks.push_back(_brick11);  maybe be able yo iterate through and this.draw(), maybe useful if i figure out how to delete objects aswell
    //listofBricks.push_back(_brick12);
    //listofBricks.push_back(_brick13);
    //listofBricks.push_back(_brick14);
    //listofBricks.push_back(_brick15);
    //listofBricks.push_back(_brick16);
    
    //listofBricks.push_back(_brick21);
    //listofBricks.push_back(_brick22);
    //listofBricks.push_back(_brick23);
    //listofBricks.push_back(_brick24);
    //listofBricks.push_back(_brick25);
    //listofBricks.push_back(_brick26);
    
    //listofBricks.push_back(_brick31);
    //listofBricks.push_back(_brick32);
    //listofBricks.push_back(_brick33);
    //listofBricks.push_back(_brick34);
    //listofBricks.push_back(_brick35);
    //listofBricks.push_back(_brick36);
}

void BreakoutEngine::read_input(Gamepad &pad, bool x)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
    
    if (x == true) {
        _p1.set_tilt();
    }
    else {
        _p1.set_joy();
    }
    //_pause = pad.check_event(Gamepad::START_PRESSED);   // == false then system("PAUSE") or cin.get()
}

void BreakoutEngine::draw(N5110 &lcd)
{
    // draw the elements in the LCD buffer
    // pitch
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    //score
    //print_scores(lcd);
    // paddles
    _p1.draw(lcd);
    // ball
    _ball.draw(lcd);
    
    //print_scores(lcd);
    
    _brick11.draw(lcd);
    _brick12.draw(lcd);
    _brick13.draw(lcd);
    _brick14.draw(lcd);
    _brick15.draw(lcd);
    _brick16.draw(lcd);
    
    _brick21.draw(lcd);
    _brick22.draw(lcd);
    _brick23.draw(lcd);
    _brick24.draw(lcd);
    _brick25.draw(lcd);
    _brick26.draw(lcd);
    
    _brick31.draw(lcd);
    _brick32.draw(lcd);
    _brick33.draw(lcd);
    _brick34.draw(lcd);
    _brick35.draw(lcd);
    _brick36.draw(lcd);
    
    
    
//    for (it = listofBricks.begin(); it != listofBricks.end(); it++)
//    {
//       int x = it->_x;
//       int y = it->_y;
       
//        lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT);
//    }
    
    

    
    
//    for(int i = GAP+1; i <= WIDTH; i+= BRICK_WIDTH + 1){
//         for(int j = GAP+1; j <= (BRICK_HEIGHT + 1)*3; j+= BRICK_HEIGHT + 1){
//            Vector2D _grid[i][j]= _brick.init(i, j,BRICK_WIDTH,BRICK_HEIGHT);
//        }
//    }
}


void BreakoutEngine::update(Gamepad &pad)
{
    check_goal(pad);
    // important to update paddles and ball before checking collisions so can
    // correct for it before updating the display
    _p1.update(_d,_mag);
    _ball.update();
    lives_leds(pad);
    
    check_wall_collision(pad);
    check_paddle_collisions(pad);
    check_brick_collisions(pad);
}

void BreakoutEngine::lives_leds(Gamepad &pad)
{
    if (_p1.get_lives() == 0) {
        pad.leds_off();
    }
    
    else if (_p1.get_lives() == 1) {
        //turn leftmost led on
        pad.leds_off();
        pad.led(1,1);
    }
    
    else if (_p1.get_lives() == 2) {
        //turn leftmost led on
        pad.leds_off();
        pad.led(1,1);
        pad.led(2,1);
    }
    
    else if (_p1.get_lives() == 3) {
        //turn leftmost led on
        pad.leds_off();
        pad.led(1,1);
        pad.led(2,1);
        pad.led(3,1);
    }

    else if (_p1.get_lives() == 4) {
        pad.leds_on();
        pad.led(5,0);
        pad.led(6,0);
    }
    
    else if (_p1.get_lives() == 5) {
        pad.leds_on();
        pad.led(6,0);
    }
    
    else if (_p1.get_lives() == 6) {
        pad.leds_on();
    }
}

int BreakoutEngine::get_lives() {
    return _p1.get_lives();
}

void BreakoutEngine::check_wall_collision(Gamepad &pad)
{
    // read current ball attributes
    Vector2D ball_pos = _ball.get_pos();
    Vector2D ball_velocity = _ball.get_velocity();

    if (ball_pos.y <= 1) {  //  1 due to 1 pixel boundary
        ball_pos.y = 1;  // bounce off ceiling without going off screen
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }
    
    else if (ball_pos.x <= 1) {  //  1 due to 1 pixel boundary
        ball_pos.x = 1;  // bounce off ceiling without going off screen
        ball_velocity.x = -ball_velocity.x;
        // audio feedback
        pad.tone(750.0,0.1);
    }
        
    // check if hit bottom wall
    else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
        // hit bottom
        ball_pos.x = (WIDTH-1) - _ball_size;  // stops ball going off screen
        ball_velocity.x = -ball_velocity.x;
        // audio feedback
        pad.tone(750.0,0.1);
    }

    // update ball parameters
    _ball.set_velocity(ball_velocity);
    _ball.set_pos(ball_pos);
    
}

void BreakoutEngine::check_paddle_collisions(Gamepad &pad)
{
    // read current ball attributes
    Vector2D ball_pos = _ball.get_pos();
    Vector2D ball_velocity = _ball.get_velocity();

    // check p1 first
    Vector2D p1_pos = _p1.get_pos();

    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= p1_pos.x) && //left 
        (ball_pos.x <= p1_pos.x + _paddle_width) && //right
        (ball_pos.y >= _p1y) && //bottom
        (ball_pos.y <= _p1y + _paddle_height)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        pad.tone(1000.0,0.1);
        ball_pos.y = _p1y + _paddle_height - 1;
        ball_velocity.y = -ball_velocity.y;
    
//        if (ball_pos.x == p1_pos.x + PADDLE_WIDTH/2) {     // check ballxpos in relation to paddle xpos. translate the distance from the centre to an angle between 30 and 60 degrees in that direction
//            ball_pos.y = _p1y + _paddle_height - 1;
//            ball_velocity.y = -ball_velocity.y;
//        }
//        else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
//            float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
//            if (ball_velocity.x > 0) {
//                ball_velocity.x = -ball_velocity.x;
//            }
 //           ball_pos.y = _p1y + _paddle_heigh - 1;
 //           ball_velocity.y = -tan(ang);
 //       }
 //       else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
 //           float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
 //           if (ball_velocity.x < 0) {
 //               ball_velocity.x = -ball_velocity.x;
 //           }
 //           ball_pos.y = _p1y + _paddle_height - 1;
 //           ball_velocity.y = -tan(ang);
 //       }
    }

    // write new attributes
    _ball.set_velocity(ball_velocity);
    _ball.set_pos(ball_pos);
}

void BreakoutEngine::check_brick_collisions(Gamepad &pad)
{
    // read current ball attributes
    Vector2D ball_pos = _ball.get_pos();
    Vector2D ball_velocity = _ball.get_velocity();
             
    // check p1 first
    Vector2D _brick11_pos = _brick11.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick11_pos.x) && //left
        (ball_pos.x <= _brick11_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick11_pos.y) && //bottom
        (ball_pos.y <= _brick11_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick11_pos.y + BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick11.hit();
        //delete _brick11;
        _brick11_pos.x = -100;
        _brick11_pos.y = -100;
        _brick11.set_pos(_brick11_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick12_pos = _brick12.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick12_pos.x) && //left
        (ball_pos.x <= _brick12_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick12_pos.y) && //bottom
        (ball_pos.y <= _brick12_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick12_pos.y + BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick12.hit();
        //delete _brick12;
        _brick12_pos.x = -100;
        _brick12_pos.y = -100;
        _brick12.set_pos(_brick12_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick13_pos = _brick13.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick13_pos.x) && //left
        (ball_pos.x <= _brick13_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick13_pos.y) && //bottom
        (ball_pos.y <= _brick13_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick13_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick13.hit();
        //delete _brick13;
        _brick13_pos.x = -100;
        _brick13_pos.y = -100;
        _brick13.set_pos(_brick13_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick14_pos = _brick14.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick14_pos.x) && //left
        (ball_pos.x <= _brick14_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick14_pos.y) && //bottom
        (ball_pos.y <= _brick14_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick14_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick14.hit();
        //delete _brick14;
        _brick14_pos.x = -100;
        _brick14_pos.y = -100;
        _brick14.set_pos(_brick14_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick15_pos = _brick15.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick15_pos.x) && //left
        (ball_pos.x <= _brick15_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick15_pos.y) && //bottom
        (ball_pos.y <= _brick15_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick15_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick15.hit();
        //delete _brick15;
        _brick15_pos.x = -100;
        _brick15_pos.y = -100;
        _brick15.set_pos(_brick15_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick16_pos = _brick16.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick16_pos.x) && //left
        (ball_pos.x <= _brick16_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick16_pos.y) && //bottom
        (ball_pos.y <= _brick16_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick16_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick16.hit();
        //delete _brick16;
        _brick16_pos.x = -100;
        _brick16_pos.y = -100;
        _brick16.set_pos(_brick16_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick21_pos = _brick21.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick21_pos.x) && //left
        (ball_pos.x <= _brick21_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick21_pos.y) && //bottom
        (ball_pos.y <= _brick21_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick21_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick21.hit();
        //delete _brick21;
        _brick21_pos.x = -100;
        _brick21_pos.y = -100;
        _brick21.set_pos(_brick21_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick22_pos = _brick22.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick22_pos.x) && //left
        (ball_pos.x <= _brick22_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick22_pos.y) && //bottom
        (ball_pos.y <= _brick22_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick22_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick22.hit();
        //delete _brick22;
        _brick22_pos.x = -100;
        _brick22_pos.y = -100;
        _brick22.set_pos(_brick22_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick23_pos = _brick23.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick23_pos.x) && //left
        (ball_pos.x <= _brick23_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick23_pos.y) && //bottom
        (ball_pos.y <= _brick23_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick23_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick23.hit();
        //delete _brick23;
        _brick23_pos.x = -100;
        _brick23_pos.y = -100;
        _brick23.set_pos(_brick23_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick24_pos = _brick24.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick24_pos.x) && //left
        (ball_pos.x <= _brick24_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick24_pos.y) && //bottom
        (ball_pos.y <= _brick24_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick24_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick24.hit();
        //delete _brick24;
        _brick24_pos.x = -100;
        _brick24_pos.y = -100;
        _brick24.set_pos(_brick24_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick25_pos = _brick25.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick25_pos.x) && //left
        (ball_pos.x <= _brick25_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick25_pos.y) && //bottom
        (ball_pos.y <= _brick25_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick25_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick25.hit();
        //delete _brick25;
        _brick25_pos.x = -100;
        _brick25_pos.y = -100;
        _brick25.set_pos(_brick25_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick26_pos = _brick26.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick26_pos.x) && //left
        (ball_pos.x <= _brick26_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick26_pos.y) && //bottom
        (ball_pos.y <= _brick26_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick26_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick26.hit();
        //delete _brick26;
        _brick26_pos.x = -100;
        _brick26_pos.y = -100;
        _brick26.set_pos(_brick26_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick31_pos = _brick31.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick31_pos.x) && //left
        (ball_pos.x <= _brick31_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick31_pos.y) && //bottom
        (ball_pos.y <= _brick31_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick31_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick31.hit();
        //delete _brick31;
        _brick31_pos.x = -100;
        _brick31_pos.y = -100;
        _brick31.set_pos(_brick31_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick32_pos = _brick32.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick32_pos.x) && //left
        (ball_pos.x <= _brick32_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick32_pos.y) && //bottom
        (ball_pos.y <= _brick32_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick32_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick32.hit();
        //delete _brick32;
        _brick32_pos.x = -100;
        _brick32_pos.y = -100;
        _brick32.set_pos(_brick32_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick33_pos = _brick33.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick33_pos.x) && //left
        (ball_pos.x <= _brick33_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick33_pos.y) && //bottom
        (ball_pos.y <= _brick33_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick33_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick33.hit();
        //delete _brick33;
        _brick33_pos.x = -100;
        _brick33_pos.y = -100;
        _brick33.set_pos(_brick33_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick34_pos = _brick34.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick34_pos.x) && //left
        (ball_pos.x <= _brick34_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick34_pos.y) && //bottom
        (ball_pos.y <= _brick34_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick34_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick34.hit();
        //delete _brick34;
        _brick34_pos.x = -100;
        _brick34_pos.y = -100;
        _brick34.set_pos(_brick34_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick35_pos = _brick35.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick35_pos.x) && //left
        (ball_pos.x <= _brick35_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick35_pos.y) && //bottom
        (ball_pos.y <= _brick35_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick35_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick35.hit();
        //delete _brick35;
        _brick35_pos.x = -100;
        _brick35_pos.y = -100;
        _brick35.set_pos(_brick35_pos);
        one_less();
    }
    // check p1 first
    Vector2D _brick36_pos = _brick36.get_pos();
    
    // see if ball has hit the paddle by checking for overlaps
    if (
        (ball_pos.x >= _brick36_pos.x) && //left
        (ball_pos.x <= _brick36_pos.x + BRICK_WIDTH) && //right
        (ball_pos.y >= _brick36_pos.y) && //bottom
        (ball_pos.y <= _brick36_pos.y + BRICK_HEIGHT)  //top
    ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
        // if it has, fix position and reflect x velocity  
        ball_pos.y = _brick36_pos.y+ BRICK_HEIGHT;
        ball_velocity.y = -ball_velocity.y;
        // audio feedback
        pad.tone(1000.0,0.1);
        _brick36.hit();
        //delete _brick36;
        _brick36_pos.x = -100;
        _brick36_pos.y = -100;
        _brick36.set_pos(_brick36_pos);
        one_less();
    }

    // write new attributes
    _ball.set_velocity(ball_velocity);
    _ball.set_pos(ball_pos);
}

bool BreakoutEngine::check_goal(Gamepad &pad)
{
    Vector2D ball_pos = _ball.get_pos();
    // P1 has scored
    if (ball_pos.y > HEIGHT) {
        _p1.lose_life();
        //lose_screen(); // go to loss screen then initialise again
        
        _ball.init(_ball_size,_speed);
        pad.tone(1500.0,0.5);
        return true;
    }
    else {
        return false;
    }
}

//void flash_backlight (N5110 &lcd) {
//    lcd.setBrightness(0);
//    wait(0.1);
//    lcd.setBrightness(1);
//    wait(0.1);
//    lcd.setBrightness(0);
//    wait(0.1);
//    lcd.setBrightness(1);
//    wait(0.1);
//    lcd.setBrightness(0);
//    wait(0.1);
//    lcd.setBrightness(1);
//    wait(0.1);
//    lcd.setBrightness(0);
//    wait(0.1);
//    lcd.setBrightness(1);
//}


void BreakoutEngine::print_scores(N5110 &lcd)
{
    // get scores from paddles
    int p1_score = _p1.get_lives();

    // print to LCD i
    char buffer1[14];
    sprintf(buffer1,"%2d",p1_score);
    lcd.printString(buffer1,WIDTH/2 -8,4);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
}

int BreakoutEngine::get_num_left(){
    return _number_left;
}
void BreakoutEngine::one_less() {
    _number_left -= 1;
}