FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed Apr 24 05:40:12 2019 +0000
Revision:
29:5168318d3e88
Parent:
27:1b5038b0a7a2
Child:
30:e3d2f0ca416f
laser implemented, need to add collisions for it

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 27:1b5038b0a7a2 1 #include "BreakoutEngine.h"
jamesheavey 27:1b5038b0a7a2 2
jamesheavey 27:1b5038b0a7a2 3
jamesheavey 27:1b5038b0a7a2 4
jamesheavey 27:1b5038b0a7a2 5 BreakoutEngine::BreakoutEngine()
jamesheavey 27:1b5038b0a7a2 6 {
jamesheavey 27:1b5038b0a7a2 7
jamesheavey 27:1b5038b0a7a2 8 }
jamesheavey 27:1b5038b0a7a2 9
jamesheavey 27:1b5038b0a7a2 10 BreakoutEngine::~BreakoutEngine()
jamesheavey 27:1b5038b0a7a2 11 {
jamesheavey 27:1b5038b0a7a2 12
jamesheavey 27:1b5038b0a7a2 13 }
jamesheavey 27:1b5038b0a7a2 14
jamesheavey 27:1b5038b0a7a2 15 void BreakoutEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
jamesheavey 27:1b5038b0a7a2 16 {
jamesheavey 27:1b5038b0a7a2 17 // initialise the game parameters
jamesheavey 27:1b5038b0a7a2 18 _paddle_width = paddle_width;
jamesheavey 27:1b5038b0a7a2 19 _paddle_height = paddle_height;
jamesheavey 27:1b5038b0a7a2 20 _ball_size = ball_size;
jamesheavey 27:1b5038b0a7a2 21 _speed = speed;
jamesheavey 27:1b5038b0a7a2 22 _number_left = 18;
jamesheavey 27:1b5038b0a7a2 23
jamesheavey 27:1b5038b0a7a2 24 // y position on screen - WIDTH is defined in N5110.h
jamesheavey 27:1b5038b0a7a2 25 _p1y = HEIGHT - GAP;
jamesheavey 27:1b5038b0a7a2 26
jamesheavey 27:1b5038b0a7a2 27 // puts paddles and ball in middle
jamesheavey 27:1b5038b0a7a2 28 _p1.init(_p1y,_paddle_height,_paddle_width);
jamesheavey 27:1b5038b0a7a2 29 _ball.init(_ball_size,_speed);
jamesheavey 27:1b5038b0a7a2 30
jamesheavey 27:1b5038b0a7a2 31 _brick11.init(3,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these
jamesheavey 27:1b5038b0a7a2 32 _brick12.init(16,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 33 _brick13.init(29,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 34 _brick14.init(42,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 35 _brick15.init(55,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 36 _brick16.init(68,GAP+1,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 37
jamesheavey 27:1b5038b0a7a2 38 _brick21.init(3,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these
jamesheavey 27:1b5038b0a7a2 39 _brick22.init(16,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 40 _brick23.init(29,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 41 _brick24.init(42,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 42 _brick25.init(55,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 43 _brick26.init(68,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 44
jamesheavey 27:1b5038b0a7a2 45 _brick31.init(3,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these
jamesheavey 27:1b5038b0a7a2 46 _brick32.init(16,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 47 _brick33.init(29,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 48 _brick34.init(42,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 49 _brick35.init(55,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 50 _brick36.init(68,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH);
jamesheavey 27:1b5038b0a7a2 51
jamesheavey 29:5168318d3e88 52 _laser.init(-10);
jamesheavey 29:5168318d3e88 53
jamesheavey 27:1b5038b0a7a2 54 //listofBricks.push_back(_brick11); maybe be able yo iterate through and this.draw(), maybe useful if i figure out how to delete objects aswell
jamesheavey 27:1b5038b0a7a2 55 //listofBricks.push_back(_brick12);
jamesheavey 27:1b5038b0a7a2 56 //listofBricks.push_back(_brick13);
jamesheavey 27:1b5038b0a7a2 57 //listofBricks.push_back(_brick14);
jamesheavey 27:1b5038b0a7a2 58 //listofBricks.push_back(_brick15);
jamesheavey 27:1b5038b0a7a2 59 //listofBricks.push_back(_brick16);
jamesheavey 27:1b5038b0a7a2 60
jamesheavey 27:1b5038b0a7a2 61 //listofBricks.push_back(_brick21);
jamesheavey 27:1b5038b0a7a2 62 //listofBricks.push_back(_brick22);
jamesheavey 27:1b5038b0a7a2 63 //listofBricks.push_back(_brick23);
jamesheavey 27:1b5038b0a7a2 64 //listofBricks.push_back(_brick24);
jamesheavey 27:1b5038b0a7a2 65 //listofBricks.push_back(_brick25);
jamesheavey 27:1b5038b0a7a2 66 //listofBricks.push_back(_brick26);
jamesheavey 27:1b5038b0a7a2 67
jamesheavey 27:1b5038b0a7a2 68 //listofBricks.push_back(_brick31);
jamesheavey 27:1b5038b0a7a2 69 //listofBricks.push_back(_brick32);
jamesheavey 27:1b5038b0a7a2 70 //listofBricks.push_back(_brick33);
jamesheavey 27:1b5038b0a7a2 71 //listofBricks.push_back(_brick34);
jamesheavey 27:1b5038b0a7a2 72 //listofBricks.push_back(_brick35);
jamesheavey 27:1b5038b0a7a2 73 //listofBricks.push_back(_brick36);
jamesheavey 27:1b5038b0a7a2 74 }
jamesheavey 27:1b5038b0a7a2 75
jamesheavey 27:1b5038b0a7a2 76 void BreakoutEngine::read_input(Gamepad &pad, bool x)
jamesheavey 27:1b5038b0a7a2 77 {
jamesheavey 27:1b5038b0a7a2 78 _d = pad.get_direction();
jamesheavey 27:1b5038b0a7a2 79 _mag = pad.get_mag();
jamesheavey 27:1b5038b0a7a2 80
jamesheavey 27:1b5038b0a7a2 81 if (x == true) {
jamesheavey 27:1b5038b0a7a2 82 _p1.set_tilt();
jamesheavey 27:1b5038b0a7a2 83 }
jamesheavey 27:1b5038b0a7a2 84 else {
jamesheavey 27:1b5038b0a7a2 85 _p1.set_joy();
jamesheavey 27:1b5038b0a7a2 86 }
jamesheavey 29:5168318d3e88 87 if (pad.check_event(Gamepad::B_PRESSED)) {
jamesheavey 29:5168318d3e88 88
jamesheavey 29:5168318d3e88 89 Vector2D p_pos = _p1.get_pos();
jamesheavey 29:5168318d3e88 90 Vector2D l_pos = _laser.get_pos();
jamesheavey 29:5168318d3e88 91 l_pos.x = p_pos.x+7;
jamesheavey 29:5168318d3e88 92 l_pos.y = p_pos.y;
jamesheavey 29:5168318d3e88 93 _laser.set_pos(l_pos);
jamesheavey 27:1b5038b0a7a2 94 //_pause = pad.check_event(Gamepad::START_PRESSED); // == false then system("PAUSE") or cin.get()
jamesheavey 29:5168318d3e88 95 }
jamesheavey 27:1b5038b0a7a2 96 }
jamesheavey 27:1b5038b0a7a2 97
jamesheavey 27:1b5038b0a7a2 98 void BreakoutEngine::draw(N5110 &lcd)
jamesheavey 27:1b5038b0a7a2 99 {
jamesheavey 27:1b5038b0a7a2 100 // draw the elements in the LCD buffer
jamesheavey 27:1b5038b0a7a2 101 // pitch
jamesheavey 27:1b5038b0a7a2 102 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
jamesheavey 27:1b5038b0a7a2 103 //score
jamesheavey 27:1b5038b0a7a2 104 //print_scores(lcd);
jamesheavey 27:1b5038b0a7a2 105 // paddles
jamesheavey 27:1b5038b0a7a2 106 _p1.draw(lcd);
jamesheavey 27:1b5038b0a7a2 107 // ball
jamesheavey 27:1b5038b0a7a2 108 _ball.draw(lcd);
jamesheavey 27:1b5038b0a7a2 109
jamesheavey 27:1b5038b0a7a2 110 //print_scores(lcd);
jamesheavey 27:1b5038b0a7a2 111
jamesheavey 27:1b5038b0a7a2 112 _brick11.draw(lcd);
jamesheavey 27:1b5038b0a7a2 113 _brick12.draw(lcd);
jamesheavey 27:1b5038b0a7a2 114 _brick13.draw(lcd);
jamesheavey 27:1b5038b0a7a2 115 _brick14.draw(lcd);
jamesheavey 27:1b5038b0a7a2 116 _brick15.draw(lcd);
jamesheavey 27:1b5038b0a7a2 117 _brick16.draw(lcd);
jamesheavey 27:1b5038b0a7a2 118
jamesheavey 27:1b5038b0a7a2 119 _brick21.draw(lcd);
jamesheavey 27:1b5038b0a7a2 120 _brick22.draw(lcd);
jamesheavey 27:1b5038b0a7a2 121 _brick23.draw(lcd);
jamesheavey 27:1b5038b0a7a2 122 _brick24.draw(lcd);
jamesheavey 27:1b5038b0a7a2 123 _brick25.draw(lcd);
jamesheavey 27:1b5038b0a7a2 124 _brick26.draw(lcd);
jamesheavey 27:1b5038b0a7a2 125
jamesheavey 27:1b5038b0a7a2 126 _brick31.draw(lcd);
jamesheavey 27:1b5038b0a7a2 127 _brick32.draw(lcd);
jamesheavey 27:1b5038b0a7a2 128 _brick33.draw(lcd);
jamesheavey 27:1b5038b0a7a2 129 _brick34.draw(lcd);
jamesheavey 27:1b5038b0a7a2 130 _brick35.draw(lcd);
jamesheavey 27:1b5038b0a7a2 131 _brick36.draw(lcd);
jamesheavey 27:1b5038b0a7a2 132
jamesheavey 29:5168318d3e88 133 _laser.draw(lcd);
jamesheavey 27:1b5038b0a7a2 134
jamesheavey 27:1b5038b0a7a2 135
jamesheavey 27:1b5038b0a7a2 136 // for (it = listofBricks.begin(); it != listofBricks.end(); it++)
jamesheavey 27:1b5038b0a7a2 137 // {
jamesheavey 27:1b5038b0a7a2 138 // int x = it->_x;
jamesheavey 27:1b5038b0a7a2 139 // int y = it->_y;
jamesheavey 27:1b5038b0a7a2 140
jamesheavey 27:1b5038b0a7a2 141 // lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT);
jamesheavey 27:1b5038b0a7a2 142 // }
jamesheavey 27:1b5038b0a7a2 143
jamesheavey 27:1b5038b0a7a2 144
jamesheavey 27:1b5038b0a7a2 145
jamesheavey 27:1b5038b0a7a2 146
jamesheavey 27:1b5038b0a7a2 147
jamesheavey 27:1b5038b0a7a2 148 // for(int i = GAP+1; i <= WIDTH; i+= BRICK_WIDTH + 1){
jamesheavey 27:1b5038b0a7a2 149 // for(int j = GAP+1; j <= (BRICK_HEIGHT + 1)*3; j+= BRICK_HEIGHT + 1){
jamesheavey 27:1b5038b0a7a2 150 // Vector2D _grid[i][j]= _brick.init(i, j,BRICK_WIDTH,BRICK_HEIGHT);
jamesheavey 27:1b5038b0a7a2 151 // }
jamesheavey 27:1b5038b0a7a2 152 // }
jamesheavey 27:1b5038b0a7a2 153 }
jamesheavey 27:1b5038b0a7a2 154
jamesheavey 27:1b5038b0a7a2 155
jamesheavey 27:1b5038b0a7a2 156 void BreakoutEngine::update(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 157 {
jamesheavey 27:1b5038b0a7a2 158 check_goal(pad);
jamesheavey 27:1b5038b0a7a2 159 // important to update paddles and ball before checking collisions so can
jamesheavey 27:1b5038b0a7a2 160 // correct for it before updating the display
jamesheavey 27:1b5038b0a7a2 161 _p1.update(_d,_mag);
jamesheavey 27:1b5038b0a7a2 162 _ball.update();
jamesheavey 29:5168318d3e88 163 _laser.update();
jamesheavey 27:1b5038b0a7a2 164 lives_leds(pad);
jamesheavey 27:1b5038b0a7a2 165
jamesheavey 27:1b5038b0a7a2 166 check_wall_collision(pad);
jamesheavey 27:1b5038b0a7a2 167 check_paddle_collisions(pad);
jamesheavey 27:1b5038b0a7a2 168 check_brick_collisions(pad);
jamesheavey 27:1b5038b0a7a2 169 }
jamesheavey 27:1b5038b0a7a2 170
jamesheavey 27:1b5038b0a7a2 171 void BreakoutEngine::lives_leds(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 172 {
jamesheavey 27:1b5038b0a7a2 173 if (_p1.get_lives() == 0) {
jamesheavey 27:1b5038b0a7a2 174 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 175 }
jamesheavey 27:1b5038b0a7a2 176
jamesheavey 27:1b5038b0a7a2 177 else if (_p1.get_lives() == 1) {
jamesheavey 27:1b5038b0a7a2 178 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 179 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 180 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 181 }
jamesheavey 27:1b5038b0a7a2 182
jamesheavey 27:1b5038b0a7a2 183 else if (_p1.get_lives() == 2) {
jamesheavey 27:1b5038b0a7a2 184 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 185 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 186 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 187 pad.led(2,1);
jamesheavey 27:1b5038b0a7a2 188 }
jamesheavey 27:1b5038b0a7a2 189
jamesheavey 27:1b5038b0a7a2 190 else if (_p1.get_lives() == 3) {
jamesheavey 27:1b5038b0a7a2 191 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 192 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 193 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 194 pad.led(2,1);
jamesheavey 27:1b5038b0a7a2 195 pad.led(3,1);
jamesheavey 27:1b5038b0a7a2 196 }
jamesheavey 27:1b5038b0a7a2 197
jamesheavey 27:1b5038b0a7a2 198 else if (_p1.get_lives() == 4) {
jamesheavey 27:1b5038b0a7a2 199 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 200 pad.led(5,0);
jamesheavey 27:1b5038b0a7a2 201 pad.led(6,0);
jamesheavey 27:1b5038b0a7a2 202 }
jamesheavey 27:1b5038b0a7a2 203
jamesheavey 27:1b5038b0a7a2 204 else if (_p1.get_lives() == 5) {
jamesheavey 27:1b5038b0a7a2 205 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 206 pad.led(6,0);
jamesheavey 27:1b5038b0a7a2 207 }
jamesheavey 27:1b5038b0a7a2 208
jamesheavey 27:1b5038b0a7a2 209 else if (_p1.get_lives() == 6) {
jamesheavey 27:1b5038b0a7a2 210 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 211 }
jamesheavey 27:1b5038b0a7a2 212 }
jamesheavey 27:1b5038b0a7a2 213
jamesheavey 27:1b5038b0a7a2 214 int BreakoutEngine::get_lives() {
jamesheavey 27:1b5038b0a7a2 215 return _p1.get_lives();
jamesheavey 27:1b5038b0a7a2 216 }
jamesheavey 27:1b5038b0a7a2 217
jamesheavey 27:1b5038b0a7a2 218 void BreakoutEngine::check_wall_collision(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 219 {
jamesheavey 27:1b5038b0a7a2 220 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 221 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 222 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 27:1b5038b0a7a2 223
jamesheavey 27:1b5038b0a7a2 224 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
jamesheavey 27:1b5038b0a7a2 225 ball_pos.y = 1; // bounce off ceiling without going off screen
jamesheavey 27:1b5038b0a7a2 226 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 227 // audio feedback
jamesheavey 27:1b5038b0a7a2 228 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 229 }
jamesheavey 27:1b5038b0a7a2 230
jamesheavey 27:1b5038b0a7a2 231 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 27:1b5038b0a7a2 232 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 27:1b5038b0a7a2 233 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 234 // audio feedback
jamesheavey 27:1b5038b0a7a2 235 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 236 }
jamesheavey 27:1b5038b0a7a2 237
jamesheavey 27:1b5038b0a7a2 238 // check if hit bottom wall
jamesheavey 27:1b5038b0a7a2 239 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 27:1b5038b0a7a2 240 // hit bottom
jamesheavey 27:1b5038b0a7a2 241 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 27:1b5038b0a7a2 242 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 243 // audio feedback
jamesheavey 27:1b5038b0a7a2 244 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 245 }
jamesheavey 27:1b5038b0a7a2 246
jamesheavey 27:1b5038b0a7a2 247 // update ball parameters
jamesheavey 27:1b5038b0a7a2 248 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 249 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 250
jamesheavey 27:1b5038b0a7a2 251 }
jamesheavey 27:1b5038b0a7a2 252
jamesheavey 27:1b5038b0a7a2 253 void BreakoutEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 254 {
jamesheavey 27:1b5038b0a7a2 255 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 256 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 257 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 27:1b5038b0a7a2 258
jamesheavey 27:1b5038b0a7a2 259 // check p1 first
jamesheavey 27:1b5038b0a7a2 260 Vector2D p1_pos = _p1.get_pos();
jamesheavey 27:1b5038b0a7a2 261
jamesheavey 27:1b5038b0a7a2 262 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 263 if (
jamesheavey 27:1b5038b0a7a2 264 (ball_pos.x >= p1_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 265 (ball_pos.x <= p1_pos.x + _paddle_width) && //right
jamesheavey 27:1b5038b0a7a2 266 (ball_pos.y >= _p1y) && //bottom
jamesheavey 27:1b5038b0a7a2 267 (ball_pos.y <= _p1y + _paddle_height) //top
jamesheavey 27:1b5038b0a7a2 268 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 269 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 270 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 271 ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 272 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 273
jamesheavey 27:1b5038b0a7a2 274 // 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
jamesheavey 27:1b5038b0a7a2 275 // ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 276 // ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 277 // }
jamesheavey 27:1b5038b0a7a2 278 // else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 27:1b5038b0a7a2 279 // 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
jamesheavey 27:1b5038b0a7a2 280 // if (ball_velocity.x > 0) {
jamesheavey 27:1b5038b0a7a2 281 // ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 282 // }
jamesheavey 27:1b5038b0a7a2 283 // ball_pos.y = _p1y + _paddle_heigh - 1;
jamesheavey 27:1b5038b0a7a2 284 // ball_velocity.y = -tan(ang);
jamesheavey 27:1b5038b0a7a2 285 // }
jamesheavey 27:1b5038b0a7a2 286 // else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 27:1b5038b0a7a2 287 // 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
jamesheavey 27:1b5038b0a7a2 288 // if (ball_velocity.x < 0) {
jamesheavey 27:1b5038b0a7a2 289 // ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 290 // }
jamesheavey 27:1b5038b0a7a2 291 // ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 292 // ball_velocity.y = -tan(ang);
jamesheavey 27:1b5038b0a7a2 293 // }
jamesheavey 27:1b5038b0a7a2 294 }
jamesheavey 27:1b5038b0a7a2 295
jamesheavey 27:1b5038b0a7a2 296 // write new attributes
jamesheavey 27:1b5038b0a7a2 297 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 298 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 299 }
jamesheavey 27:1b5038b0a7a2 300
jamesheavey 27:1b5038b0a7a2 301 void BreakoutEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 302 {
jamesheavey 27:1b5038b0a7a2 303 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 304 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 305 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 27:1b5038b0a7a2 306
jamesheavey 27:1b5038b0a7a2 307 // check p1 first
jamesheavey 27:1b5038b0a7a2 308 Vector2D _brick11_pos = _brick11.get_pos();
jamesheavey 27:1b5038b0a7a2 309
jamesheavey 27:1b5038b0a7a2 310 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 311 if (
jamesheavey 27:1b5038b0a7a2 312 (ball_pos.x >= _brick11_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 313 (ball_pos.x <= _brick11_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 314 (ball_pos.y >= _brick11_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 315 (ball_pos.y <= _brick11_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 316 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 317 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 318 ball_pos.y = _brick11_pos.y + BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 319 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 320 // audio feedback
jamesheavey 27:1b5038b0a7a2 321 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 322 _brick11.hit();
jamesheavey 27:1b5038b0a7a2 323 //delete _brick11;
jamesheavey 27:1b5038b0a7a2 324 _brick11_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 325 _brick11_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 326 _brick11.set_pos(_brick11_pos);
jamesheavey 27:1b5038b0a7a2 327 one_less();
jamesheavey 27:1b5038b0a7a2 328 }
jamesheavey 27:1b5038b0a7a2 329 // check p1 first
jamesheavey 27:1b5038b0a7a2 330 Vector2D _brick12_pos = _brick12.get_pos();
jamesheavey 27:1b5038b0a7a2 331
jamesheavey 27:1b5038b0a7a2 332 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 333 if (
jamesheavey 27:1b5038b0a7a2 334 (ball_pos.x >= _brick12_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 335 (ball_pos.x <= _brick12_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 336 (ball_pos.y >= _brick12_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 337 (ball_pos.y <= _brick12_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 338 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 339 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 340 ball_pos.y = _brick12_pos.y + BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 341 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 342 // audio feedback
jamesheavey 27:1b5038b0a7a2 343 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 344 _brick12.hit();
jamesheavey 27:1b5038b0a7a2 345 //delete _brick12;
jamesheavey 27:1b5038b0a7a2 346 _brick12_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 347 _brick12_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 348 _brick12.set_pos(_brick12_pos);
jamesheavey 27:1b5038b0a7a2 349 one_less();
jamesheavey 27:1b5038b0a7a2 350 }
jamesheavey 27:1b5038b0a7a2 351 // check p1 first
jamesheavey 27:1b5038b0a7a2 352 Vector2D _brick13_pos = _brick13.get_pos();
jamesheavey 27:1b5038b0a7a2 353
jamesheavey 27:1b5038b0a7a2 354 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 355 if (
jamesheavey 27:1b5038b0a7a2 356 (ball_pos.x >= _brick13_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 357 (ball_pos.x <= _brick13_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 358 (ball_pos.y >= _brick13_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 359 (ball_pos.y <= _brick13_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 360 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 361 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 362 ball_pos.y = _brick13_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 363 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 364 // audio feedback
jamesheavey 27:1b5038b0a7a2 365 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 366 _brick13.hit();
jamesheavey 27:1b5038b0a7a2 367 //delete _brick13;
jamesheavey 27:1b5038b0a7a2 368 _brick13_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 369 _brick13_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 370 _brick13.set_pos(_brick13_pos);
jamesheavey 27:1b5038b0a7a2 371 one_less();
jamesheavey 27:1b5038b0a7a2 372 }
jamesheavey 27:1b5038b0a7a2 373 // check p1 first
jamesheavey 27:1b5038b0a7a2 374 Vector2D _brick14_pos = _brick14.get_pos();
jamesheavey 27:1b5038b0a7a2 375
jamesheavey 27:1b5038b0a7a2 376 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 377 if (
jamesheavey 27:1b5038b0a7a2 378 (ball_pos.x >= _brick14_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 379 (ball_pos.x <= _brick14_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 380 (ball_pos.y >= _brick14_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 381 (ball_pos.y <= _brick14_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 382 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 383 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 384 ball_pos.y = _brick14_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 385 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 386 // audio feedback
jamesheavey 27:1b5038b0a7a2 387 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 388 _brick14.hit();
jamesheavey 27:1b5038b0a7a2 389 //delete _brick14;
jamesheavey 27:1b5038b0a7a2 390 _brick14_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 391 _brick14_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 392 _brick14.set_pos(_brick14_pos);
jamesheavey 27:1b5038b0a7a2 393 one_less();
jamesheavey 27:1b5038b0a7a2 394 }
jamesheavey 27:1b5038b0a7a2 395 // check p1 first
jamesheavey 27:1b5038b0a7a2 396 Vector2D _brick15_pos = _brick15.get_pos();
jamesheavey 27:1b5038b0a7a2 397
jamesheavey 27:1b5038b0a7a2 398 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 399 if (
jamesheavey 27:1b5038b0a7a2 400 (ball_pos.x >= _brick15_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 401 (ball_pos.x <= _brick15_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 402 (ball_pos.y >= _brick15_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 403 (ball_pos.y <= _brick15_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 404 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 405 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 406 ball_pos.y = _brick15_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 407 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 408 // audio feedback
jamesheavey 27:1b5038b0a7a2 409 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 410 _brick15.hit();
jamesheavey 27:1b5038b0a7a2 411 //delete _brick15;
jamesheavey 27:1b5038b0a7a2 412 _brick15_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 413 _brick15_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 414 _brick15.set_pos(_brick15_pos);
jamesheavey 27:1b5038b0a7a2 415 one_less();
jamesheavey 27:1b5038b0a7a2 416 }
jamesheavey 27:1b5038b0a7a2 417 // check p1 first
jamesheavey 27:1b5038b0a7a2 418 Vector2D _brick16_pos = _brick16.get_pos();
jamesheavey 27:1b5038b0a7a2 419
jamesheavey 27:1b5038b0a7a2 420 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 421 if (
jamesheavey 27:1b5038b0a7a2 422 (ball_pos.x >= _brick16_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 423 (ball_pos.x <= _brick16_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 424 (ball_pos.y >= _brick16_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 425 (ball_pos.y <= _brick16_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 426 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 427 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 428 ball_pos.y = _brick16_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 429 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 430 // audio feedback
jamesheavey 27:1b5038b0a7a2 431 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 432 _brick16.hit();
jamesheavey 27:1b5038b0a7a2 433 //delete _brick16;
jamesheavey 27:1b5038b0a7a2 434 _brick16_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 435 _brick16_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 436 _brick16.set_pos(_brick16_pos);
jamesheavey 27:1b5038b0a7a2 437 one_less();
jamesheavey 27:1b5038b0a7a2 438 }
jamesheavey 27:1b5038b0a7a2 439 // check p1 first
jamesheavey 27:1b5038b0a7a2 440 Vector2D _brick21_pos = _brick21.get_pos();
jamesheavey 27:1b5038b0a7a2 441
jamesheavey 27:1b5038b0a7a2 442 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 443 if (
jamesheavey 27:1b5038b0a7a2 444 (ball_pos.x >= _brick21_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 445 (ball_pos.x <= _brick21_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 446 (ball_pos.y >= _brick21_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 447 (ball_pos.y <= _brick21_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 448 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 449 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 450 ball_pos.y = _brick21_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 451 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 452 // audio feedback
jamesheavey 27:1b5038b0a7a2 453 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 454 _brick21.hit();
jamesheavey 27:1b5038b0a7a2 455 //delete _brick21;
jamesheavey 27:1b5038b0a7a2 456 _brick21_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 457 _brick21_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 458 _brick21.set_pos(_brick21_pos);
jamesheavey 27:1b5038b0a7a2 459 one_less();
jamesheavey 27:1b5038b0a7a2 460 }
jamesheavey 27:1b5038b0a7a2 461 // check p1 first
jamesheavey 27:1b5038b0a7a2 462 Vector2D _brick22_pos = _brick22.get_pos();
jamesheavey 27:1b5038b0a7a2 463
jamesheavey 27:1b5038b0a7a2 464 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 465 if (
jamesheavey 27:1b5038b0a7a2 466 (ball_pos.x >= _brick22_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 467 (ball_pos.x <= _brick22_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 468 (ball_pos.y >= _brick22_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 469 (ball_pos.y <= _brick22_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 470 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 471 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 472 ball_pos.y = _brick22_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 473 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 474 // audio feedback
jamesheavey 27:1b5038b0a7a2 475 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 476 _brick22.hit();
jamesheavey 27:1b5038b0a7a2 477 //delete _brick22;
jamesheavey 27:1b5038b0a7a2 478 _brick22_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 479 _brick22_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 480 _brick22.set_pos(_brick22_pos);
jamesheavey 27:1b5038b0a7a2 481 one_less();
jamesheavey 27:1b5038b0a7a2 482 }
jamesheavey 27:1b5038b0a7a2 483 // check p1 first
jamesheavey 27:1b5038b0a7a2 484 Vector2D _brick23_pos = _brick23.get_pos();
jamesheavey 27:1b5038b0a7a2 485
jamesheavey 27:1b5038b0a7a2 486 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 487 if (
jamesheavey 27:1b5038b0a7a2 488 (ball_pos.x >= _brick23_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 489 (ball_pos.x <= _brick23_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 490 (ball_pos.y >= _brick23_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 491 (ball_pos.y <= _brick23_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 492 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 493 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 494 ball_pos.y = _brick23_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 495 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 496 // audio feedback
jamesheavey 27:1b5038b0a7a2 497 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 498 _brick23.hit();
jamesheavey 27:1b5038b0a7a2 499 //delete _brick23;
jamesheavey 27:1b5038b0a7a2 500 _brick23_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 501 _brick23_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 502 _brick23.set_pos(_brick23_pos);
jamesheavey 27:1b5038b0a7a2 503 one_less();
jamesheavey 27:1b5038b0a7a2 504 }
jamesheavey 27:1b5038b0a7a2 505 // check p1 first
jamesheavey 27:1b5038b0a7a2 506 Vector2D _brick24_pos = _brick24.get_pos();
jamesheavey 27:1b5038b0a7a2 507
jamesheavey 27:1b5038b0a7a2 508 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 509 if (
jamesheavey 27:1b5038b0a7a2 510 (ball_pos.x >= _brick24_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 511 (ball_pos.x <= _brick24_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 512 (ball_pos.y >= _brick24_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 513 (ball_pos.y <= _brick24_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 514 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 515 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 516 ball_pos.y = _brick24_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 517 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 518 // audio feedback
jamesheavey 27:1b5038b0a7a2 519 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 520 _brick24.hit();
jamesheavey 27:1b5038b0a7a2 521 //delete _brick24;
jamesheavey 27:1b5038b0a7a2 522 _brick24_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 523 _brick24_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 524 _brick24.set_pos(_brick24_pos);
jamesheavey 27:1b5038b0a7a2 525 one_less();
jamesheavey 27:1b5038b0a7a2 526 }
jamesheavey 27:1b5038b0a7a2 527 // check p1 first
jamesheavey 27:1b5038b0a7a2 528 Vector2D _brick25_pos = _brick25.get_pos();
jamesheavey 27:1b5038b0a7a2 529
jamesheavey 27:1b5038b0a7a2 530 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 531 if (
jamesheavey 27:1b5038b0a7a2 532 (ball_pos.x >= _brick25_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 533 (ball_pos.x <= _brick25_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 534 (ball_pos.y >= _brick25_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 535 (ball_pos.y <= _brick25_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 536 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 537 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 538 ball_pos.y = _brick25_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 539 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 540 // audio feedback
jamesheavey 27:1b5038b0a7a2 541 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 542 _brick25.hit();
jamesheavey 27:1b5038b0a7a2 543 //delete _brick25;
jamesheavey 27:1b5038b0a7a2 544 _brick25_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 545 _brick25_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 546 _brick25.set_pos(_brick25_pos);
jamesheavey 27:1b5038b0a7a2 547 one_less();
jamesheavey 27:1b5038b0a7a2 548 }
jamesheavey 27:1b5038b0a7a2 549 // check p1 first
jamesheavey 27:1b5038b0a7a2 550 Vector2D _brick26_pos = _brick26.get_pos();
jamesheavey 27:1b5038b0a7a2 551
jamesheavey 27:1b5038b0a7a2 552 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 553 if (
jamesheavey 27:1b5038b0a7a2 554 (ball_pos.x >= _brick26_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 555 (ball_pos.x <= _brick26_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 556 (ball_pos.y >= _brick26_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 557 (ball_pos.y <= _brick26_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 558 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 559 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 560 ball_pos.y = _brick26_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 561 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 562 // audio feedback
jamesheavey 27:1b5038b0a7a2 563 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 564 _brick26.hit();
jamesheavey 27:1b5038b0a7a2 565 //delete _brick26;
jamesheavey 27:1b5038b0a7a2 566 _brick26_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 567 _brick26_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 568 _brick26.set_pos(_brick26_pos);
jamesheavey 27:1b5038b0a7a2 569 one_less();
jamesheavey 27:1b5038b0a7a2 570 }
jamesheavey 27:1b5038b0a7a2 571 // check p1 first
jamesheavey 27:1b5038b0a7a2 572 Vector2D _brick31_pos = _brick31.get_pos();
jamesheavey 27:1b5038b0a7a2 573
jamesheavey 27:1b5038b0a7a2 574 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 575 if (
jamesheavey 27:1b5038b0a7a2 576 (ball_pos.x >= _brick31_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 577 (ball_pos.x <= _brick31_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 578 (ball_pos.y >= _brick31_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 579 (ball_pos.y <= _brick31_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 580 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 581 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 582 ball_pos.y = _brick31_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 583 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 584 // audio feedback
jamesheavey 27:1b5038b0a7a2 585 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 586 _brick31.hit();
jamesheavey 27:1b5038b0a7a2 587 //delete _brick31;
jamesheavey 27:1b5038b0a7a2 588 _brick31_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 589 _brick31_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 590 _brick31.set_pos(_brick31_pos);
jamesheavey 27:1b5038b0a7a2 591 one_less();
jamesheavey 27:1b5038b0a7a2 592 }
jamesheavey 27:1b5038b0a7a2 593 // check p1 first
jamesheavey 27:1b5038b0a7a2 594 Vector2D _brick32_pos = _brick32.get_pos();
jamesheavey 27:1b5038b0a7a2 595
jamesheavey 27:1b5038b0a7a2 596 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 597 if (
jamesheavey 27:1b5038b0a7a2 598 (ball_pos.x >= _brick32_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 599 (ball_pos.x <= _brick32_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 600 (ball_pos.y >= _brick32_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 601 (ball_pos.y <= _brick32_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 602 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 603 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 604 ball_pos.y = _brick32_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 605 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 606 // audio feedback
jamesheavey 27:1b5038b0a7a2 607 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 608 _brick32.hit();
jamesheavey 27:1b5038b0a7a2 609 //delete _brick32;
jamesheavey 27:1b5038b0a7a2 610 _brick32_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 611 _brick32_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 612 _brick32.set_pos(_brick32_pos);
jamesheavey 27:1b5038b0a7a2 613 one_less();
jamesheavey 27:1b5038b0a7a2 614 }
jamesheavey 27:1b5038b0a7a2 615 // check p1 first
jamesheavey 27:1b5038b0a7a2 616 Vector2D _brick33_pos = _brick33.get_pos();
jamesheavey 27:1b5038b0a7a2 617
jamesheavey 27:1b5038b0a7a2 618 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 619 if (
jamesheavey 27:1b5038b0a7a2 620 (ball_pos.x >= _brick33_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 621 (ball_pos.x <= _brick33_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 622 (ball_pos.y >= _brick33_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 623 (ball_pos.y <= _brick33_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 624 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 625 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 626 ball_pos.y = _brick33_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 627 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 628 // audio feedback
jamesheavey 27:1b5038b0a7a2 629 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 630 _brick33.hit();
jamesheavey 27:1b5038b0a7a2 631 //delete _brick33;
jamesheavey 27:1b5038b0a7a2 632 _brick33_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 633 _brick33_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 634 _brick33.set_pos(_brick33_pos);
jamesheavey 27:1b5038b0a7a2 635 one_less();
jamesheavey 27:1b5038b0a7a2 636 }
jamesheavey 27:1b5038b0a7a2 637 // check p1 first
jamesheavey 27:1b5038b0a7a2 638 Vector2D _brick34_pos = _brick34.get_pos();
jamesheavey 27:1b5038b0a7a2 639
jamesheavey 27:1b5038b0a7a2 640 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 641 if (
jamesheavey 27:1b5038b0a7a2 642 (ball_pos.x >= _brick34_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 643 (ball_pos.x <= _brick34_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 644 (ball_pos.y >= _brick34_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 645 (ball_pos.y <= _brick34_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 646 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 647 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 648 ball_pos.y = _brick34_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 649 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 650 // audio feedback
jamesheavey 27:1b5038b0a7a2 651 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 652 _brick34.hit();
jamesheavey 27:1b5038b0a7a2 653 //delete _brick34;
jamesheavey 27:1b5038b0a7a2 654 _brick34_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 655 _brick34_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 656 _brick34.set_pos(_brick34_pos);
jamesheavey 27:1b5038b0a7a2 657 one_less();
jamesheavey 27:1b5038b0a7a2 658 }
jamesheavey 27:1b5038b0a7a2 659 // check p1 first
jamesheavey 27:1b5038b0a7a2 660 Vector2D _brick35_pos = _brick35.get_pos();
jamesheavey 27:1b5038b0a7a2 661
jamesheavey 27:1b5038b0a7a2 662 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 663 if (
jamesheavey 27:1b5038b0a7a2 664 (ball_pos.x >= _brick35_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 665 (ball_pos.x <= _brick35_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 666 (ball_pos.y >= _brick35_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 667 (ball_pos.y <= _brick35_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 668 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 669 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 670 ball_pos.y = _brick35_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 671 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 672 // audio feedback
jamesheavey 27:1b5038b0a7a2 673 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 674 _brick35.hit();
jamesheavey 27:1b5038b0a7a2 675 //delete _brick35;
jamesheavey 27:1b5038b0a7a2 676 _brick35_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 677 _brick35_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 678 _brick35.set_pos(_brick35_pos);
jamesheavey 27:1b5038b0a7a2 679 one_less();
jamesheavey 27:1b5038b0a7a2 680 }
jamesheavey 27:1b5038b0a7a2 681 // check p1 first
jamesheavey 27:1b5038b0a7a2 682 Vector2D _brick36_pos = _brick36.get_pos();
jamesheavey 27:1b5038b0a7a2 683
jamesheavey 27:1b5038b0a7a2 684 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 685 if (
jamesheavey 27:1b5038b0a7a2 686 (ball_pos.x >= _brick36_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 687 (ball_pos.x <= _brick36_pos.x + BRICK_WIDTH) && //right
jamesheavey 27:1b5038b0a7a2 688 (ball_pos.y >= _brick36_pos.y) && //bottom
jamesheavey 27:1b5038b0a7a2 689 (ball_pos.y <= _brick36_pos.y + BRICK_HEIGHT) //top
jamesheavey 27:1b5038b0a7a2 690 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 27:1b5038b0a7a2 691 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 692 ball_pos.y = _brick36_pos.y+ BRICK_HEIGHT;
jamesheavey 27:1b5038b0a7a2 693 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 694 // audio feedback
jamesheavey 27:1b5038b0a7a2 695 pad.tone(1000.0,0.1);
jamesheavey 27:1b5038b0a7a2 696 _brick36.hit();
jamesheavey 27:1b5038b0a7a2 697 //delete _brick36;
jamesheavey 27:1b5038b0a7a2 698 _brick36_pos.x = -100;
jamesheavey 27:1b5038b0a7a2 699 _brick36_pos.y = -100;
jamesheavey 27:1b5038b0a7a2 700 _brick36.set_pos(_brick36_pos);
jamesheavey 27:1b5038b0a7a2 701 one_less();
jamesheavey 27:1b5038b0a7a2 702 }
jamesheavey 27:1b5038b0a7a2 703
jamesheavey 27:1b5038b0a7a2 704 // write new attributes
jamesheavey 27:1b5038b0a7a2 705 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 706 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 707 }
jamesheavey 27:1b5038b0a7a2 708
jamesheavey 27:1b5038b0a7a2 709 bool BreakoutEngine::check_goal(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 710 {
jamesheavey 27:1b5038b0a7a2 711 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 712 // P1 has scored
jamesheavey 27:1b5038b0a7a2 713 if (ball_pos.y > HEIGHT) {
jamesheavey 27:1b5038b0a7a2 714 _p1.lose_life();
jamesheavey 27:1b5038b0a7a2 715 //lose_screen(); // go to loss screen then initialise again
jamesheavey 27:1b5038b0a7a2 716
jamesheavey 27:1b5038b0a7a2 717 _ball.init(_ball_size,_speed);
jamesheavey 27:1b5038b0a7a2 718 pad.tone(1500.0,0.5);
jamesheavey 27:1b5038b0a7a2 719 return true;
jamesheavey 27:1b5038b0a7a2 720 }
jamesheavey 27:1b5038b0a7a2 721 else {
jamesheavey 27:1b5038b0a7a2 722 return false;
jamesheavey 27:1b5038b0a7a2 723 }
jamesheavey 27:1b5038b0a7a2 724 }
jamesheavey 27:1b5038b0a7a2 725
jamesheavey 27:1b5038b0a7a2 726 //void flash_backlight (N5110 &lcd) {
jamesheavey 27:1b5038b0a7a2 727 // lcd.setBrightness(0);
jamesheavey 27:1b5038b0a7a2 728 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 729 // lcd.setBrightness(1);
jamesheavey 27:1b5038b0a7a2 730 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 731 // lcd.setBrightness(0);
jamesheavey 27:1b5038b0a7a2 732 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 733 // lcd.setBrightness(1);
jamesheavey 27:1b5038b0a7a2 734 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 735 // lcd.setBrightness(0);
jamesheavey 27:1b5038b0a7a2 736 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 737 // lcd.setBrightness(1);
jamesheavey 27:1b5038b0a7a2 738 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 739 // lcd.setBrightness(0);
jamesheavey 27:1b5038b0a7a2 740 // wait(0.1);
jamesheavey 27:1b5038b0a7a2 741 // lcd.setBrightness(1);
jamesheavey 27:1b5038b0a7a2 742 //}
jamesheavey 27:1b5038b0a7a2 743
jamesheavey 27:1b5038b0a7a2 744
jamesheavey 27:1b5038b0a7a2 745 void BreakoutEngine::print_scores(N5110 &lcd)
jamesheavey 27:1b5038b0a7a2 746 {
jamesheavey 27:1b5038b0a7a2 747 // get scores from paddles
jamesheavey 27:1b5038b0a7a2 748 int p1_score = _p1.get_lives();
jamesheavey 27:1b5038b0a7a2 749
jamesheavey 27:1b5038b0a7a2 750 // print to LCD i
jamesheavey 27:1b5038b0a7a2 751 char buffer1[14];
jamesheavey 27:1b5038b0a7a2 752 sprintf(buffer1,"%2d",p1_score);
jamesheavey 27:1b5038b0a7a2 753 lcd.printString(buffer1,WIDTH/2 -8,4); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
jamesheavey 27:1b5038b0a7a2 754 }
jamesheavey 27:1b5038b0a7a2 755
jamesheavey 27:1b5038b0a7a2 756 int BreakoutEngine::get_num_left(){
jamesheavey 27:1b5038b0a7a2 757 return _number_left;
jamesheavey 27:1b5038b0a7a2 758 }
jamesheavey 27:1b5038b0a7a2 759 void BreakoutEngine::one_less() {
jamesheavey 27:1b5038b0a7a2 760 _number_left -= 1;
jamesheavey 27:1b5038b0a7a2 761 }