James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 23 18:40:01 2019 +0000
Revision:
20:e88c2eb13791
Parent:
18:e5eb50c5e61f
same as below

Who changed what in which revision?

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