James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 23 07:42:44 2019 +0000
Revision:
13:418a71b24b37
Parent:
12:f714a9e8c55b
Child:
14:38560375d420
victory screen added, brick collision fixed

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