James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu Apr 25 20:32:41 2019 +0000
Revision:
35:3a614d539a54
Parent:
34:07ded1f83c59
Child:
36:cb73014d3a99
lasers added to list

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