FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed Apr 24 05:56:06 2019 +0000
Revision:
30:e3d2f0ca416f
Parent:
29:5168318d3e88
Child:
31:516d4e27765a
max number of lasers bug

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