James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Mon May 06 22:57:04 2019 +0000
Revision:
91:c01a736fb0d9
Parent:
86:01f33d94e496
Child:
92:2e6d88e44f56
paddle commented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 27:1b5038b0a7a2 1 #include "BreakoutEngine.h"
jamesheavey 91:c01a736fb0d9 2
jamesheavey 27:1b5038b0a7a2 3 BreakoutEngine::BreakoutEngine()
jamesheavey 27:1b5038b0a7a2 4 {
jamesheavey 27:1b5038b0a7a2 5
jamesheavey 27:1b5038b0a7a2 6 }
jamesheavey 27:1b5038b0a7a2 7
jamesheavey 27:1b5038b0a7a2 8 BreakoutEngine::~BreakoutEngine()
jamesheavey 27:1b5038b0a7a2 9 {
jamesheavey 27:1b5038b0a7a2 10
jamesheavey 27:1b5038b0a7a2 11 }
jamesheavey 27:1b5038b0a7a2 12
jamesheavey 64:c3426b417ad9 13 void BreakoutEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
jamesheavey 27:1b5038b0a7a2 14 {
jamesheavey 27:1b5038b0a7a2 15 // initialise the game parameters
jamesheavey 27:1b5038b0a7a2 16 _paddle_width = paddle_width;
jamesheavey 27:1b5038b0a7a2 17 _paddle_height = paddle_height;
jamesheavey 27:1b5038b0a7a2 18 _ball_size = ball_size;
jamesheavey 27:1b5038b0a7a2 19 _speed = speed;
jamesheavey 27:1b5038b0a7a2 20 _number_left = 18;
jamesheavey 30:e3d2f0ca416f 21 _index = 0;
jamesheavey 45:b1ac80481d4f 22 _cool_time = 0.0f;
jamesheavey 64:c3426b417ad9 23 _score = 0;
jamesheavey 64:c3426b417ad9 24 _prev_score = 0;
jamesheavey 62:64559062e0ec 25 _multiplier = 1;
jamesheavey 91:c01a736fb0d9 26
jamesheavey 27:1b5038b0a7a2 27 // y position on screen - WIDTH is defined in N5110.h
jamesheavey 82:d1341d632890 28 _paddley = HEIGHT - GAP - 1;
jamesheavey 27:1b5038b0a7a2 29
jamesheavey 27:1b5038b0a7a2 30 // puts paddles and ball in middle
jamesheavey 82:d1341d632890 31 _paddle.init(_paddley,_paddle_height,_paddle_width);
jamesheavey 82:d1341d632890 32 _ball.init(_ball_size,_speed,_paddle.get_pos().x + 7);
jamesheavey 91:c01a736fb0d9 33
jamesheavey 91:c01a736fb0d9 34 _brick11.init(3,GAP_TOP+1,3);
jamesheavey 86:01f33d94e496 35 _brick12.init(16,GAP_TOP+1,3);
jamesheavey 86:01f33d94e496 36 _brick13.init(29,GAP_TOP+1,3);
jamesheavey 86:01f33d94e496 37 _brick14.init(42,GAP_TOP+1,3);
jamesheavey 86:01f33d94e496 38 _brick15.init(55,GAP_TOP+1,3);
jamesheavey 86:01f33d94e496 39 _brick16.init(68,GAP_TOP+1,3);
jamesheavey 91:c01a736fb0d9 40
jamesheavey 91:c01a736fb0d9 41 _brick21.init(3,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 86:01f33d94e496 42 _brick22.init(16,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 86:01f33d94e496 43 _brick23.init(29,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 86:01f33d94e496 44 _brick24.init(42,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 86:01f33d94e496 45 _brick25.init(55,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 86:01f33d94e496 46 _brick26.init(68,GAP_TOP+BRICK_HEIGHT+2,2);
jamesheavey 91:c01a736fb0d9 47
jamesheavey 91:c01a736fb0d9 48 _brick31.init(3,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 86:01f33d94e496 49 _brick32.init(16,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 86:01f33d94e496 50 _brick33.init(29,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 86:01f33d94e496 51 _brick34.init(42,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 86:01f33d94e496 52 _brick35.init(55,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 86:01f33d94e496 53 _brick36.init(68,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
jamesheavey 91:c01a736fb0d9 54
jamesheavey 91:c01a736fb0d9 55 listofBricks.push_back(_brick11);
jamesheavey 34:07ded1f83c59 56 listofBricks.push_back(_brick12);
jamesheavey 34:07ded1f83c59 57 listofBricks.push_back(_brick13);
jamesheavey 34:07ded1f83c59 58 listofBricks.push_back(_brick14);
jamesheavey 34:07ded1f83c59 59 listofBricks.push_back(_brick15);
jamesheavey 34:07ded1f83c59 60 listofBricks.push_back(_brick16);
jamesheavey 34:07ded1f83c59 61 listofBricks.push_back(_brick21);
jamesheavey 34:07ded1f83c59 62 listofBricks.push_back(_brick22);
jamesheavey 34:07ded1f83c59 63 listofBricks.push_back(_brick23);
jamesheavey 34:07ded1f83c59 64 listofBricks.push_back(_brick24);
jamesheavey 34:07ded1f83c59 65 listofBricks.push_back(_brick25);
jamesheavey 34:07ded1f83c59 66 listofBricks.push_back(_brick26);
jamesheavey 34:07ded1f83c59 67 listofBricks.push_back(_brick31);
jamesheavey 34:07ded1f83c59 68 listofBricks.push_back(_brick32);
jamesheavey 34:07ded1f83c59 69 listofBricks.push_back(_brick33);
jamesheavey 34:07ded1f83c59 70 listofBricks.push_back(_brick34);
jamesheavey 34:07ded1f83c59 71 listofBricks.push_back(_brick35);
jamesheavey 34:07ded1f83c59 72 listofBricks.push_back(_brick36);
jamesheavey 91:c01a736fb0d9 73
jamesheavey 30:e3d2f0ca416f 74 _laser1.init(-10);
jamesheavey 30:e3d2f0ca416f 75 _laser2.init(-10);
jamesheavey 30:e3d2f0ca416f 76 _laser3.init(-10);
jamesheavey 91:c01a736fb0d9 77
jamesheavey 35:3a614d539a54 78 listofLasers.push_back(_laser1);
jamesheavey 35:3a614d539a54 79 listofLasers.push_back(_laser2);
jamesheavey 35:3a614d539a54 80 listofLasers.push_back(_laser3);
jamesheavey 27:1b5038b0a7a2 81 }
jamesheavey 27:1b5038b0a7a2 82
jamesheavey 81:735e5ee2c92a 83 void BreakoutEngine::reset_game() // rename to reset add in ball reset and use to increase the number of balls each time
jamesheavey 91:c01a736fb0d9 84 {
jamesheavey 60:63d69184ec0a 85 reset_num_left();
jamesheavey 82:d1341d632890 86 _ball.init(_ball_size,_speed + _multiplier/2, _paddle.get_pos().x + 7); // replace the 1 with a multiplier private variable that tracks the number of times continued from victory
jamesheavey 82:d1341d632890 87 _paddle.recentre();
jamesheavey 60:63d69184ec0a 88 int pointer = 0;
jamesheavey 91:c01a736fb0d9 89 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 91:c01a736fb0d9 90 if (pointer <= 5) {
jamesheavey 60:63d69184ec0a 91 it_R -> set_posx((pointer * 13) + 3);
jamesheavey 91:c01a736fb0d9 92 } else if (pointer <= 11) {
jamesheavey 60:63d69184ec0a 93 it_R -> set_posx(((pointer-6) * 13) + 3);
jamesheavey 91:c01a736fb0d9 94 } else if (pointer <= 17) {
jamesheavey 60:63d69184ec0a 95 it_R -> set_posx(((pointer-12) * 13) + 3);
jamesheavey 60:63d69184ec0a 96 }
jamesheavey 91:c01a736fb0d9 97
jamesheavey 91:c01a736fb0d9 98 it_R -> reset_lives(_multiplier);
jamesheavey 60:63d69184ec0a 99 pointer ++;
jamesheavey 60:63d69184ec0a 100 }
jamesheavey 91:c01a736fb0d9 101 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 84:6483503a72fc 102 it_L -> set_posx(-10);
jamesheavey 84:6483503a72fc 103 }
jamesheavey 60:63d69184ec0a 104 }
jamesheavey 91:c01a736fb0d9 105
jamesheavey 60:63d69184ec0a 106
jamesheavey 72:7254d2a8a1cd 107 void BreakoutEngine::read_input(Gamepad &pad, bool tilt, float sens)
jamesheavey 27:1b5038b0a7a2 108 {
jamesheavey 27:1b5038b0a7a2 109 _d = pad.get_direction();
jamesheavey 27:1b5038b0a7a2 110 _mag = pad.get_mag();
jamesheavey 91:c01a736fb0d9 111
jamesheavey 72:7254d2a8a1cd 112 if (tilt == true) {
jamesheavey 82:d1341d632890 113 _paddle.set_tilt();
jamesheavey 91:c01a736fb0d9 114 } else {
jamesheavey 82:d1341d632890 115 _paddle.set_joy();
jamesheavey 27:1b5038b0a7a2 116 }
jamesheavey 91:c01a736fb0d9 117
jamesheavey 82:d1341d632890 118 _paddle.set_sens(sens);
jamesheavey 91:c01a736fb0d9 119
jamesheavey 45:b1ac80481d4f 120 if (pad.check_event(Gamepad::B_PRESSED) && _cool_time <= 0) { // max of 3 lasers on screen at once
jamesheavey 91:c01a736fb0d9 121
jamesheavey 82:d1341d632890 122 Vector2D p_pos = _paddle.get_pos();
jamesheavey 37:dd1538ae6534 123 it_L = listofLasers.begin();
jamesheavey 91:c01a736fb0d9 124 switch(_index) {
jamesheavey 30:e3d2f0ca416f 125 case 0:
jamesheavey 37:dd1538ae6534 126 advance(it_L, 0);
jamesheavey 37:dd1538ae6534 127 it_L -> set_posx(p_pos.x+7);
jamesheavey 37:dd1538ae6534 128 it_L -> set_posy(p_pos.y);
jamesheavey 30:e3d2f0ca416f 129 inc_index();
jamesheavey 30:e3d2f0ca416f 130 break;
jamesheavey 30:e3d2f0ca416f 131 case 1:
jamesheavey 37:dd1538ae6534 132 advance(it_L, 1);
jamesheavey 37:dd1538ae6534 133 it_L -> set_posx(p_pos.x+7);
jamesheavey 37:dd1538ae6534 134 it_L -> set_posy(p_pos.y);
jamesheavey 30:e3d2f0ca416f 135 inc_index();
jamesheavey 30:e3d2f0ca416f 136 break;
jamesheavey 30:e3d2f0ca416f 137 case 2:
jamesheavey 37:dd1538ae6534 138 advance(it_L, 2);
jamesheavey 37:dd1538ae6534 139 it_L -> set_posx(p_pos.x+7);
jamesheavey 37:dd1538ae6534 140 it_L -> set_posy(p_pos.y);
jamesheavey 30:e3d2f0ca416f 141 reset_index();
jamesheavey 30:e3d2f0ca416f 142 break;
jamesheavey 30:e3d2f0ca416f 143 }
jamesheavey 91:c01a736fb0d9 144
jamesheavey 91:c01a736fb0d9 145
jamesheavey 46:810b3a7fc387 146 _cool_time = 0.75f;
jamesheavey 91:c01a736fb0d9 147 } else {
jamesheavey 45:b1ac80481d4f 148 _cool_time -= 0.125; // 1/8 as fps is 8
jamesheavey 45:b1ac80481d4f 149 }
jamesheavey 91:c01a736fb0d9 150
jamesheavey 27:1b5038b0a7a2 151 }
jamesheavey 27:1b5038b0a7a2 152
jamesheavey 91:c01a736fb0d9 153 void BreakoutEngine::inc_index()
jamesheavey 91:c01a736fb0d9 154 {
jamesheavey 30:e3d2f0ca416f 155 _index ++;
jamesheavey 30:e3d2f0ca416f 156 }
jamesheavey 30:e3d2f0ca416f 157
jamesheavey 91:c01a736fb0d9 158 void BreakoutEngine::reset_index()
jamesheavey 91:c01a736fb0d9 159 {
jamesheavey 30:e3d2f0ca416f 160 _index = 0;
jamesheavey 30:e3d2f0ca416f 161 }
jamesheavey 30:e3d2f0ca416f 162
jamesheavey 27:1b5038b0a7a2 163 void BreakoutEngine::draw(N5110 &lcd)
jamesheavey 27:1b5038b0a7a2 164 {
jamesheavey 27:1b5038b0a7a2 165 // draw the elements in the LCD buffer
jamesheavey 27:1b5038b0a7a2 166 // pitch
jamesheavey 40:ac53905346fb 167 lcd.drawRect(0,GAP_TOP - 2,WIDTH,HEIGHT - GAP_TOP + 2,FILL_TRANSPARENT);
jamesheavey 27:1b5038b0a7a2 168 //score
jamesheavey 40:ac53905346fb 169 print_scores(lcd);
jamesheavey 27:1b5038b0a7a2 170 // paddles
jamesheavey 82:d1341d632890 171 _paddle.draw(lcd);
jamesheavey 27:1b5038b0a7a2 172 // ball
jamesheavey 27:1b5038b0a7a2 173 _ball.draw(lcd);
jamesheavey 91:c01a736fb0d9 174
jamesheavey 27:1b5038b0a7a2 175 //print_scores(lcd);
jamesheavey 91:c01a736fb0d9 176
jamesheavey 91:c01a736fb0d9 177 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 39:a9bb03bef107 178 it_R->draw(lcd);
jamesheavey 34:07ded1f83c59 179 }
jamesheavey 91:c01a736fb0d9 180 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 35:3a614d539a54 181 it_L->draw(lcd);
jamesheavey 35:3a614d539a54 182 }
jamesheavey 27:1b5038b0a7a2 183
jamesheavey 27:1b5038b0a7a2 184 }
jamesheavey 27:1b5038b0a7a2 185
jamesheavey 27:1b5038b0a7a2 186
jamesheavey 27:1b5038b0a7a2 187 void BreakoutEngine::update(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 188 {
jamesheavey 81:735e5ee2c92a 189 check_loss(pad);
jamesheavey 27:1b5038b0a7a2 190 // important to update paddles and ball before checking collisions so can
jamesheavey 27:1b5038b0a7a2 191 // correct for it before updating the display
jamesheavey 82:d1341d632890 192 _paddle.update(_d,_mag);
jamesheavey 27:1b5038b0a7a2 193 _ball.update();
jamesheavey 91:c01a736fb0d9 194
jamesheavey 91:c01a736fb0d9 195 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 35:3a614d539a54 196 it_L->update();
jamesheavey 35:3a614d539a54 197 }
jamesheavey 91:c01a736fb0d9 198
jamesheavey 27:1b5038b0a7a2 199 lives_leds(pad);
jamesheavey 91:c01a736fb0d9 200
jamesheavey 42:347c20a16ee6 201 check_wall_collisions(pad);
jamesheavey 27:1b5038b0a7a2 202 check_paddle_collisions(pad);
jamesheavey 27:1b5038b0a7a2 203 check_brick_collisions(pad);
jamesheavey 36:cb73014d3a99 204 check_laser_collisions(pad);
jamesheavey 91:c01a736fb0d9 205
jamesheavey 91:c01a736fb0d9 206
jamesheavey 27:1b5038b0a7a2 207 }
jamesheavey 27:1b5038b0a7a2 208
jamesheavey 27:1b5038b0a7a2 209 void BreakoutEngine::lives_leds(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 210 {
jamesheavey 82:d1341d632890 211 if (_paddle.get_lives() == 0) {
jamesheavey 27:1b5038b0a7a2 212 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 213 }
jamesheavey 91:c01a736fb0d9 214
jamesheavey 82:d1341d632890 215 else if (_paddle.get_lives() == 1) {
jamesheavey 27:1b5038b0a7a2 216 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 217 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 218 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 219 }
jamesheavey 91:c01a736fb0d9 220
jamesheavey 82:d1341d632890 221 else if (_paddle.get_lives() == 2) {
jamesheavey 27:1b5038b0a7a2 222 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 223 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 224 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 225 pad.led(2,1);
jamesheavey 27:1b5038b0a7a2 226 }
jamesheavey 91:c01a736fb0d9 227
jamesheavey 82:d1341d632890 228 else if (_paddle.get_lives() == 3) {
jamesheavey 27:1b5038b0a7a2 229 //turn leftmost led on
jamesheavey 27:1b5038b0a7a2 230 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 231 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 232 pad.led(2,1);
jamesheavey 27:1b5038b0a7a2 233 pad.led(3,1);
jamesheavey 27:1b5038b0a7a2 234 }
jamesheavey 27:1b5038b0a7a2 235
jamesheavey 82:d1341d632890 236 else if (_paddle.get_lives() == 4) {
jamesheavey 27:1b5038b0a7a2 237 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 238 pad.led(5,0);
jamesheavey 27:1b5038b0a7a2 239 pad.led(6,0);
jamesheavey 27:1b5038b0a7a2 240 }
jamesheavey 91:c01a736fb0d9 241
jamesheavey 82:d1341d632890 242 else if (_paddle.get_lives() == 5) {
jamesheavey 27:1b5038b0a7a2 243 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 244 pad.led(6,0);
jamesheavey 27:1b5038b0a7a2 245 }
jamesheavey 91:c01a736fb0d9 246
jamesheavey 82:d1341d632890 247 else if (_paddle.get_lives() == 6) {
jamesheavey 27:1b5038b0a7a2 248 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 249 }
jamesheavey 27:1b5038b0a7a2 250 }
jamesheavey 27:1b5038b0a7a2 251
jamesheavey 91:c01a736fb0d9 252 int BreakoutEngine::get_lives()
jamesheavey 91:c01a736fb0d9 253 {
jamesheavey 82:d1341d632890 254 return _paddle.get_lives();
jamesheavey 27:1b5038b0a7a2 255 }
jamesheavey 27:1b5038b0a7a2 256
jamesheavey 42:347c20a16ee6 257 void BreakoutEngine::check_wall_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 40:ac53905346fb 263 if (ball_pos.y <= GAP_TOP - 1) { // 1 due to 1 pixel boundary
jamesheavey 40:ac53905346fb 264 ball_pos.y = GAP_TOP - 1; // bounce off ceiling without going off screen
jamesheavey 27:1b5038b0a7a2 265 ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 266 // audio feedback
jamesheavey 27:1b5038b0a7a2 267 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 268 }
jamesheavey 91:c01a736fb0d9 269
jamesheavey 27:1b5038b0a7a2 270 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 27:1b5038b0a7a2 271 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 27:1b5038b0a7a2 272 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 273 // audio feedback
jamesheavey 27:1b5038b0a7a2 274 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 275 }
jamesheavey 91:c01a736fb0d9 276
jamesheavey 27:1b5038b0a7a2 277 // check if hit bottom wall
jamesheavey 27:1b5038b0a7a2 278 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 27:1b5038b0a7a2 279 // hit bottom
jamesheavey 27:1b5038b0a7a2 280 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 27:1b5038b0a7a2 281 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 282 // audio feedback
jamesheavey 27:1b5038b0a7a2 283 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 284 }
jamesheavey 27:1b5038b0a7a2 285
jamesheavey 27:1b5038b0a7a2 286 // update ball parameters
jamesheavey 27:1b5038b0a7a2 287 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 288 _ball.set_pos(ball_pos);
jamesheavey 91:c01a736fb0d9 289
jamesheavey 91:c01a736fb0d9 290 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 41:fe383cbb51b2 291 if (
jamesheavey 42:347c20a16ee6 292 (it_L -> get_y() <= GAP_TOP - 2)
jamesheavey 41:fe383cbb51b2 293 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 91:c01a736fb0d9 294 // if it has, fix position and reflect x velocity
jamesheavey 41:fe383cbb51b2 295 it_L -> set_posx(-10);
jamesheavey 41:fe383cbb51b2 296 }
jamesheavey 41:fe383cbb51b2 297 }
jamesheavey 91:c01a736fb0d9 298
jamesheavey 41:fe383cbb51b2 299 }
jamesheavey 41:fe383cbb51b2 300
jamesheavey 27:1b5038b0a7a2 301 void BreakoutEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 302 {
jamesheavey 27:1b5038b0a7a2 303 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 304 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 305 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 27:1b5038b0a7a2 306
jamesheavey 27:1b5038b0a7a2 307 // check p1 first
jamesheavey 82:d1341d632890 308 Vector2D p1_pos = _paddle.get_pos();
jamesheavey 27:1b5038b0a7a2 309
jamesheavey 27:1b5038b0a7a2 310 // see if ball has hit the paddle by checking for overlaps
jamesheavey 27:1b5038b0a7a2 311 if (
jamesheavey 91:c01a736fb0d9 312 (ball_pos.x >= p1_pos.x) && //left
jamesheavey 27:1b5038b0a7a2 313 (ball_pos.x <= p1_pos.x + _paddle_width) && //right
jamesheavey 82:d1341d632890 314 (ball_pos.y >= _paddley) && //bottom
jamesheavey 82:d1341d632890 315 (ball_pos.y <= _paddley + _paddle_height) //top
jamesheavey 27:1b5038b0a7a2 316 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 91:c01a736fb0d9 317 // if it has, fix position and reflect x velocity
jamesheavey 27:1b5038b0a7a2 318 pad.tone(1000.0,0.1);
jamesheavey 82:d1341d632890 319 ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 320 ball_velocity.y = -ball_velocity.y;
jamesheavey 91:c01a736fb0d9 321
jamesheavey 27:1b5038b0a7a2 322 // 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 82:d1341d632890 323 // ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 324 // ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 325 // }
jamesheavey 27:1b5038b0a7a2 326 // else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 27:1b5038b0a7a2 327 // 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 328 // if (ball_velocity.x > 0) {
jamesheavey 27:1b5038b0a7a2 329 // ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 330 // }
jamesheavey 91:c01a736fb0d9 331 // ball_pos.y = _paddley + _paddle_heigh - 1;
jamesheavey 91:c01a736fb0d9 332 // ball_velocity.y = -tan(ang);
jamesheavey 91:c01a736fb0d9 333 // }
jamesheavey 91:c01a736fb0d9 334 // else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 91:c01a736fb0d9 335 // 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 91:c01a736fb0d9 336 // if (ball_velocity.x < 0) {
jamesheavey 91:c01a736fb0d9 337 // ball_velocity.x = -ball_velocity.x;
jamesheavey 91:c01a736fb0d9 338 // }
jamesheavey 91:c01a736fb0d9 339 // ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 91:c01a736fb0d9 340 // ball_velocity.y = -tan(ang);
jamesheavey 91:c01a736fb0d9 341 // }
jamesheavey 27:1b5038b0a7a2 342 }
jamesheavey 27:1b5038b0a7a2 343
jamesheavey 27:1b5038b0a7a2 344 // write new attributes
jamesheavey 27:1b5038b0a7a2 345 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 346 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 347 }
jamesheavey 27:1b5038b0a7a2 348
jamesheavey 27:1b5038b0a7a2 349 void BreakoutEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 350 {
jamesheavey 27:1b5038b0a7a2 351 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 352 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 353 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 91:c01a736fb0d9 354
jamesheavey 91:c01a736fb0d9 355 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 34:07ded1f83c59 356 if (
jamesheavey 39:a9bb03bef107 357 (ball_pos.x >= it_R -> get_x()) && //left
jamesheavey 39:a9bb03bef107 358 (ball_pos.x <= it_R -> get_x() + BRICK_WIDTH) && //right
jamesheavey 39:a9bb03bef107 359 (ball_pos.y >= it_R -> get_y()) && //bottom
jamesheavey 39:a9bb03bef107 360 (ball_pos.y <= it_R -> get_y() + BRICK_HEIGHT) //top
jamesheavey 34:07ded1f83c59 361 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 91:c01a736fb0d9 362 // if it has, fix position and reflect x velocity
jamesheavey 91:c01a736fb0d9 363 if (ball_velocity.y < 0) {
jamesheavey 48:966f2cf803ec 364 ball_pos.y = it_R -> get_y() + BRICK_HEIGHT;
jamesheavey 91:c01a736fb0d9 365 } else {
jamesheavey 48:966f2cf803ec 366 ball_pos.y = it_R -> get_y();
jamesheavey 48:966f2cf803ec 367 }
jamesheavey 34:07ded1f83c59 368 ball_velocity.y = -ball_velocity.y;
jamesheavey 34:07ded1f83c59 369 // audio feedback
jamesheavey 34:07ded1f83c59 370 pad.tone(1000.0,0.1);
jamesheavey 39:a9bb03bef107 371 it_R -> hit();
jamesheavey 34:07ded1f83c59 372 //delete _brick11;
jamesheavey 69:db1354676fde 373 if(it_R-> hit() == true) {
jamesheavey 67:c362df66fac9 374 it_R -> set_posx(-100);
jamesheavey 67:c362df66fac9 375 one_less();
jamesheavey 67:c362df66fac9 376 }
jamesheavey 34:07ded1f83c59 377 }
jamesheavey 27:1b5038b0a7a2 378 }
jamesheavey 27:1b5038b0a7a2 379 // write new attributes
jamesheavey 27:1b5038b0a7a2 380 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 381 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 382 }
jamesheavey 36:cb73014d3a99 383
jamesheavey 32:4dba7a85dbb2 384 void BreakoutEngine::check_laser_collisions(Gamepad &pad)
jamesheavey 32:4dba7a85dbb2 385 {
jamesheavey 32:4dba7a85dbb2 386 // read current ball attributes
jamesheavey 34:07ded1f83c59 387 // check p1 first
jamesheavey 91:c01a736fb0d9 388 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 91:c01a736fb0d9 389 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 36:cb73014d3a99 390 if (
jamesheavey 39:a9bb03bef107 391 (it_L -> get_x() >= it_R -> get_x()) && //left
jamesheavey 39:a9bb03bef107 392 (it_L -> get_x() <= it_R -> get_x() + BRICK_WIDTH) && //right
jamesheavey 39:a9bb03bef107 393 (it_L -> get_y() >= it_R -> get_y()) && //bottom
jamesheavey 39:a9bb03bef107 394 (it_L -> get_y()<= it_R -> get_y() + BRICK_HEIGHT) //top
jamesheavey 36:cb73014d3a99 395 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 91:c01a736fb0d9 396 // if it has, fix position and reflect x velocity
jamesheavey 37:dd1538ae6534 397 it_L -> set_posx(-10);
jamesheavey 36:cb73014d3a99 398 // audio feedback
jamesheavey 36:cb73014d3a99 399 pad.tone(1000.0,0.1);
jamesheavey 69:db1354676fde 400 if(it_R-> hit() == true) {
jamesheavey 39:a9bb03bef107 401 it_R -> set_posx(-100);
jamesheavey 36:cb73014d3a99 402 one_less();
jamesheavey 36:cb73014d3a99 403 }
jamesheavey 36:cb73014d3a99 404 }
jamesheavey 34:07ded1f83c59 405 }
jamesheavey 34:07ded1f83c59 406 }
jamesheavey 36:cb73014d3a99 407 }
jamesheavey 32:4dba7a85dbb2 408
jamesheavey 81:735e5ee2c92a 409 bool BreakoutEngine::check_loss(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 410 {
jamesheavey 27:1b5038b0a7a2 411 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 412 // P1 has scored
jamesheavey 27:1b5038b0a7a2 413 if (ball_pos.y > HEIGHT) {
jamesheavey 82:d1341d632890 414 _paddle.lose_life();
jamesheavey 27:1b5038b0a7a2 415 //lose_screen(); // go to loss screen then initialise again
jamesheavey 91:c01a736fb0d9 416
jamesheavey 82:d1341d632890 417 _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + 7);
jamesheavey 27:1b5038b0a7a2 418 pad.tone(1500.0,0.5);
jamesheavey 27:1b5038b0a7a2 419 return true;
jamesheavey 91:c01a736fb0d9 420 } else {
jamesheavey 27:1b5038b0a7a2 421 return false;
jamesheavey 27:1b5038b0a7a2 422 }
jamesheavey 27:1b5038b0a7a2 423 }
jamesheavey 27:1b5038b0a7a2 424
jamesheavey 34:07ded1f83c59 425 void BreakoutEngine::print_scores(N5110 &lcd) //maybe add to oneless
jamesheavey 27:1b5038b0a7a2 426 {
jamesheavey 27:1b5038b0a7a2 427 // get scores from paddles
jamesheavey 64:c3426b417ad9 428 int score = _prev_score + (18 - get_num_left())*100 * _multiplier; //maybe add a previous score so the score carries over through victory screens. add hi score fix so restart restarts from 321
jamesheavey 91:c01a736fb0d9 429 // also, add a multiplier of 2 for every contiue on victory
jamesheavey 91:c01a736fb0d9 430 _score = score;
jamesheavey 91:c01a736fb0d9 431
jamesheavey 27:1b5038b0a7a2 432 // print to LCD i
jamesheavey 27:1b5038b0a7a2 433 char buffer1[14];
jamesheavey 64:c3426b417ad9 434 sprintf(buffer1,"%2d",score);
jamesheavey 91:c01a736fb0d9 435 lcd.printString("SCORE: ",2,0);
jamesheavey 64:c3426b417ad9 436 lcd.printString(buffer1,WIDTH/2 -2,0); // font is 8 wide, so leave 4 pixel gap from middle assuming two digits
jamesheavey 27:1b5038b0a7a2 437 }
jamesheavey 62:64559062e0ec 438
jamesheavey 62:64559062e0ec 439
jamesheavey 91:c01a736fb0d9 440 int BreakoutEngine::get_prev_score()
jamesheavey 91:c01a736fb0d9 441 {
jamesheavey 47:1d1a827be81b 442 return _prev_score;
jamesheavey 47:1d1a827be81b 443 }
jamesheavey 91:c01a736fb0d9 444 void BreakoutEngine::set_prev_score(int prev_score)
jamesheavey 91:c01a736fb0d9 445 {
jamesheavey 64:c3426b417ad9 446 _prev_score = prev_score;
jamesheavey 64:c3426b417ad9 447 }
jamesheavey 91:c01a736fb0d9 448 int BreakoutEngine::get_num_left()
jamesheavey 91:c01a736fb0d9 449 {
jamesheavey 27:1b5038b0a7a2 450 return _number_left;
jamesheavey 27:1b5038b0a7a2 451 }
jamesheavey 91:c01a736fb0d9 452 void BreakoutEngine::one_less()
jamesheavey 91:c01a736fb0d9 453 {
jamesheavey 27:1b5038b0a7a2 454 _number_left -= 1;
jamesheavey 60:63d69184ec0a 455 }
jamesheavey 91:c01a736fb0d9 456 void BreakoutEngine::reset_num_left()
jamesheavey 91:c01a736fb0d9 457 {
jamesheavey 60:63d69184ec0a 458 _number_left = 18;
jamesheavey 62:64559062e0ec 459 }
jamesheavey 91:c01a736fb0d9 460 int BreakoutEngine::get_score()
jamesheavey 91:c01a736fb0d9 461 {
jamesheavey 64:c3426b417ad9 462 return _score;
jamesheavey 62:64559062e0ec 463 }
jamesheavey 91:c01a736fb0d9 464 void BreakoutEngine::inc_mult()
jamesheavey 91:c01a736fb0d9 465 {
jamesheavey 62:64559062e0ec 466 _multiplier ++;
jamesheavey 62:64559062e0ec 467 }
jamesheavey 91:c01a736fb0d9 468 void BreakoutEngine::set_mult_zero()
jamesheavey 91:c01a736fb0d9 469 {
jamesheavey 62:64559062e0ec 470 _multiplier = 0;
jamesheavey 62:64559062e0ec 471 }