James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 02:50:07 2019 +0000
Revision:
111:4848c399fae0
Parent:
109:5ba766522df0
Child:
114:280903dd7e06
:0

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