FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
136:04a2724f90cf
Final Submission. I have read and agreed with Statement of Academic Integrity.

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 123:39740c246fc2 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 123:39740c246fc2 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 123:39740c246fc2 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 123:39740c246fc2 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 123:39740c246fc2 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 123:39740c246fc2 85
jamesheavey 114:280903dd7e06 86 _powerup.init();
jamesheavey 27:1b5038b0a7a2 87 }
jamesheavey 27:1b5038b0a7a2 88
jamesheavey 136:04a2724f90cf 89
jamesheavey 93:18f81996ea89 90 void BreakoutEngine::reset_game() // resets the game after victory screen
jamesheavey 91:c01a736fb0d9 91 {
jamesheavey 93:18f81996ea89 92 reset_num_left(); // resets _number_left to 18
jamesheavey 93:18f81996ea89 93 _paddle.recentre(); // recentres the paddle on screen
jamesheavey 111:4848c399fae0 94 //printf("Paddle lives = %d", _paddle.get_lives());
jamesheavey 109:5ba766522df0 95 _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 116:2793d31ca691 96 _powerup.set_posx(-50);
jamesheavey 123:39740c246fc2 97
jamesheavey 93:18f81996ea89 98 int pointer = 0; // tracks the rows, if 6 bricks are placed, it moves to the next row
jamesheavey 91:c01a736fb0d9 99 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 93:18f81996ea89 100 if (pointer <= 5) { // 6 bricks in the first row
jamesheavey 60:63d69184ec0a 101 it_R -> set_posx((pointer * 13) + 3);
jamesheavey 91:c01a736fb0d9 102 } else if (pointer <= 11) {
jamesheavey 93:18f81996ea89 103 it_R -> set_posx(((pointer-6) * 13) + 3); // pointer - 6 to offset it back to the first column of bricks
jamesheavey 91:c01a736fb0d9 104 } else if (pointer <= 17) {
jamesheavey 93:18f81996ea89 105 it_R -> set_posx(((pointer-12) * 13) + 3); // pointer - 12 to offset it back to the first column of bricks
jamesheavey 60:63d69184ec0a 106 }
jamesheavey 91:c01a736fb0d9 107
jamesheavey 109:5ba766522df0 108 it_R -> reset_lives(_multiplier); // increases the bricks number of lives by the multiplier + their _base_lives
jamesheavey 93:18f81996ea89 109 pointer ++; // increment the pointer
jamesheavey 136:04a2724f90cf 110 //printf("pointer = %d",pointer);
jamesheavey 60:63d69184ec0a 111 }
jamesheavey 91:c01a736fb0d9 112 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 93:18f81996ea89 113 it_L -> set_posx(-10); // moves all the lasers off screen
jamesheavey 84:6483503a72fc 114 }
jamesheavey 60:63d69184ec0a 115 }
jamesheavey 91:c01a736fb0d9 116
jamesheavey 60:63d69184ec0a 117
jamesheavey 105:4e7585d8e5e2 118 void BreakoutEngine::read_input(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 119 {
jamesheavey 27:1b5038b0a7a2 120 _d = pad.get_direction();
jamesheavey 27:1b5038b0a7a2 121 _mag = pad.get_mag();
jamesheavey 91:c01a736fb0d9 122
jamesheavey 124:d635e3154bf3 123 if (pad.check_event(Gamepad::B_PRESSED) && _cool_time <= 0) { // if B is pressed and it is not cooling down then fire a laser
jamesheavey 91:c01a736fb0d9 124
jamesheavey 82:d1341d632890 125 Vector2D p_pos = _paddle.get_pos();
jamesheavey 37:dd1538ae6534 126 it_L = listofLasers.begin();
jamesheavey 124:d635e3154bf3 127 switch(_index) { // switch between the lasers each time the button is pressed so that it doesnt just re position the same laser each time
jamesheavey 30:e3d2f0ca416f 128 case 0:
jamesheavey 37:dd1538ae6534 129 advance(it_L, 0);
jamesheavey 124:d635e3154bf3 130 it_L -> set_posx(p_pos.x+7); // move laser 1 to middle of paddle
jamesheavey 37:dd1538ae6534 131 it_L -> set_posy(p_pos.y);
jamesheavey 30:e3d2f0ca416f 132 inc_index();
jamesheavey 136:04a2724f90cf 133 //printf("index = %d",_index);
jamesheavey 30:e3d2f0ca416f 134 break;
jamesheavey 30:e3d2f0ca416f 135 case 1:
jamesheavey 37:dd1538ae6534 136 advance(it_L, 1);
jamesheavey 124:d635e3154bf3 137 it_L -> set_posx(p_pos.x+7); // move laser 2 to middle of paddle
jamesheavey 37:dd1538ae6534 138 it_L -> set_posy(p_pos.y);
jamesheavey 30:e3d2f0ca416f 139 inc_index();
jamesheavey 136:04a2724f90cf 140 //printf("index = %d",_index);
jamesheavey 30:e3d2f0ca416f 141 break;
jamesheavey 30:e3d2f0ca416f 142 case 2:
jamesheavey 37:dd1538ae6534 143 advance(it_L, 2);
jamesheavey 124:d635e3154bf3 144 it_L -> set_posx(p_pos.x+7); // move laser 3 to middle of paddle
jamesheavey 37:dd1538ae6534 145 it_L -> set_posy(p_pos.y);
jamesheavey 136:04a2724f90cf 146 //printf("index = %d",_index);
jamesheavey 30:e3d2f0ca416f 147 reset_index();
jamesheavey 136:04a2724f90cf 148 //printf("index = %d",_index);
jamesheavey 30:e3d2f0ca416f 149 break;
jamesheavey 30:e3d2f0ca416f 150 }
jamesheavey 123:39740c246fc2 151
jamesheavey 94:8fa117200f6b 152 it_L = listofLasers.end();
jamesheavey 91:c01a736fb0d9 153
jamesheavey 124:d635e3154bf3 154 _cool_time = 0.75f; // reset cool time to 0.75 seconds
jamesheavey 91:c01a736fb0d9 155 } else {
jamesheavey 124:d635e3154bf3 156 _cool_time -= 0.125; // reduce cool down time by 1/8 of a second as fps is 8
jamesheavey 45:b1ac80481d4f 157 }
jamesheavey 91:c01a736fb0d9 158
jamesheavey 27:1b5038b0a7a2 159 }
jamesheavey 27:1b5038b0a7a2 160
jamesheavey 136:04a2724f90cf 161
jamesheavey 27:1b5038b0a7a2 162 void BreakoutEngine::draw(N5110 &lcd)
jamesheavey 27:1b5038b0a7a2 163 {
jamesheavey 124:d635e3154bf3 164 lcd.drawRect(0,GAP_TOP - 2,WIDTH,HEIGHT - GAP_TOP + 2,FILL_TRANSPARENT); // draw the game screen dimensions
jamesheavey 124:d635e3154bf3 165 print_scores(lcd); // print the score above the screen
jamesheavey 124:d635e3154bf3 166 _paddle.draw(lcd); // draw the paddle
jamesheavey 124:d635e3154bf3 167 _ball.draw(lcd); // draw the ball
jamesheavey 91:c01a736fb0d9 168
jamesheavey 124:d635e3154bf3 169 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { // draw all teh bricks
jamesheavey 39:a9bb03bef107 170 it_R->draw(lcd);
jamesheavey 34:07ded1f83c59 171 }
jamesheavey 124:d635e3154bf3 172 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { // draw all the lasers
jamesheavey 35:3a614d539a54 173 it_L->draw(lcd);
jamesheavey 35:3a614d539a54 174 }
jamesheavey 123:39740c246fc2 175
jamesheavey 124:d635e3154bf3 176 check_life_powerup(); // check if power up can be drawn, if so move on screen when random condition is met
jamesheavey 119:473cac51ddf0 177
jamesheavey 124:d635e3154bf3 178 _powerup.draw(lcd); // draw the powerup
jamesheavey 27:1b5038b0a7a2 179 }
jamesheavey 27:1b5038b0a7a2 180
jamesheavey 136:04a2724f90cf 181
jamesheavey 27:1b5038b0a7a2 182 void BreakoutEngine::update(Gamepad &pad)
jamesheavey 123:39740c246fc2 183 {
jamesheavey 124:d635e3154bf3 184 check_loss(pad); // check if the ball has gone above max y
jamesheavey 124:d635e3154bf3 185
jamesheavey 124:d635e3154bf3 186 _paddle.update(_d,_mag); // update paddle position
jamesheavey 124:d635e3154bf3 187 _ball.update(); // update ball position
jamesheavey 124:d635e3154bf3 188 _powerup.update(); // update powerup position
jamesheavey 91:c01a736fb0d9 189
jamesheavey 124:d635e3154bf3 190 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { // update all the lasers' positions
jamesheavey 35:3a614d539a54 191 it_L->update();
jamesheavey 35:3a614d539a54 192 }
jamesheavey 91:c01a736fb0d9 193
jamesheavey 124:d635e3154bf3 194 lives_leds(pad); // update the LEDs with the remaining lives
jamesheavey 91:c01a736fb0d9 195
jamesheavey 124:d635e3154bf3 196 check_wall_collisions(pad); // check ball and laser collisions with walls and ceiling
jamesheavey 124:d635e3154bf3 197 check_paddle_collisions(pad); // check ball collision with paddle
jamesheavey 124:d635e3154bf3 198 check_brick_collisions(pad); // check ball collision with bricks
jamesheavey 124:d635e3154bf3 199 check_laser_collisions(pad); // check all laser collisions with all bricks
jamesheavey 124:d635e3154bf3 200 check_powerup_collisions(pad); // check powerup collision with paddle
jamesheavey 136:04a2724f90cf 201 }
jamesheavey 91:c01a736fb0d9 202
jamesheavey 27:1b5038b0a7a2 203
jamesheavey 124:d635e3154bf3 204 void BreakoutEngine::lives_leds(Gamepad &pad) // converst the number of lives left into LEDs
jamesheavey 27:1b5038b0a7a2 205 {
jamesheavey 124:d635e3154bf3 206 if (_paddle.get_lives() == 0) {
jamesheavey 124:d635e3154bf3 207 pad.leds_off(); // all LEDs off
jamesheavey 27:1b5038b0a7a2 208 }
jamesheavey 91:c01a736fb0d9 209
jamesheavey 82:d1341d632890 210 else if (_paddle.get_lives() == 1) {
jamesheavey 27:1b5038b0a7a2 211 pad.leds_off();
jamesheavey 124:d635e3154bf3 212 pad.led(1,1); // 1 LED on
jamesheavey 27:1b5038b0a7a2 213 }
jamesheavey 91:c01a736fb0d9 214
jamesheavey 82:d1341d632890 215 else if (_paddle.get_lives() == 2) {
jamesheavey 27:1b5038b0a7a2 216 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 217 pad.led(1,1);
jamesheavey 124:d635e3154bf3 218 pad.led(2,1); // 2 LEDs on
jamesheavey 27:1b5038b0a7a2 219 }
jamesheavey 91:c01a736fb0d9 220
jamesheavey 82:d1341d632890 221 else if (_paddle.get_lives() == 3) {
jamesheavey 27:1b5038b0a7a2 222 pad.leds_off();
jamesheavey 27:1b5038b0a7a2 223 pad.led(1,1);
jamesheavey 27:1b5038b0a7a2 224 pad.led(2,1);
jamesheavey 124:d635e3154bf3 225 pad.led(3,1); // 3 LEDs on
jamesheavey 27:1b5038b0a7a2 226 }
jamesheavey 27:1b5038b0a7a2 227
jamesheavey 82:d1341d632890 228 else if (_paddle.get_lives() == 4) {
jamesheavey 27:1b5038b0a7a2 229 pad.leds_on();
jamesheavey 27:1b5038b0a7a2 230 pad.led(5,0);
jamesheavey 124:d635e3154bf3 231 pad.led(6,0); // 4 LEDs on
jamesheavey 27:1b5038b0a7a2 232 }
jamesheavey 91:c01a736fb0d9 233
jamesheavey 82:d1341d632890 234 else if (_paddle.get_lives() == 5) {
jamesheavey 27:1b5038b0a7a2 235 pad.leds_on();
jamesheavey 124:d635e3154bf3 236 pad.led(6,0); // 5 LEDs on
jamesheavey 27:1b5038b0a7a2 237 }
jamesheavey 91:c01a736fb0d9 238
jamesheavey 82:d1341d632890 239 else if (_paddle.get_lives() == 6) {
jamesheavey 124:d635e3154bf3 240 pad.leds_on(); // all LEDs on
jamesheavey 136:04a2724f90cf 241 //printf("all LEDs on \n");
jamesheavey 27:1b5038b0a7a2 242 }
jamesheavey 27:1b5038b0a7a2 243 }
jamesheavey 27:1b5038b0a7a2 244
jamesheavey 136:04a2724f90cf 245
jamesheavey 124:d635e3154bf3 246 void BreakoutEngine::check_wall_collisions(Gamepad &pad) // checks ball and laser collisions with the walls and ceiling
jamesheavey 27:1b5038b0a7a2 247 {
jamesheavey 27:1b5038b0a7a2 248 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 249 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 250 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 124:d635e3154bf3 251
jamesheavey 124:d635e3154bf3 252 // bounce off the ceiling
jamesheavey 40:ac53905346fb 253 if (ball_pos.y <= GAP_TOP - 1) { // 1 due to 1 pixel boundary
jamesheavey 124:d635e3154bf3 254 ball_pos.y = GAP_TOP - 1;
jamesheavey 27:1b5038b0a7a2 255 ball_velocity.y = -ball_velocity.y;
jamesheavey 124:d635e3154bf3 256 pad.tone(750.0,0.1);
jamesheavey 124:d635e3154bf3 257 }
jamesheavey 124:d635e3154bf3 258
jamesheavey 124:d635e3154bf3 259 // bounce off the left
jamesheavey 124:d635e3154bf3 260 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 124:d635e3154bf3 261 ball_pos.x = 1; // bounce off walls
jamesheavey 124:d635e3154bf3 262 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 263 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 264 }
jamesheavey 91:c01a736fb0d9 265
jamesheavey 124:d635e3154bf3 266 // bounce off the right
jamesheavey 27:1b5038b0a7a2 267 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 27:1b5038b0a7a2 268 // hit bottom
jamesheavey 27:1b5038b0a7a2 269 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 27:1b5038b0a7a2 270 ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 271 // audio feedback
jamesheavey 27:1b5038b0a7a2 272 pad.tone(750.0,0.1);
jamesheavey 27:1b5038b0a7a2 273 }
jamesheavey 27:1b5038b0a7a2 274
jamesheavey 27:1b5038b0a7a2 275 // update ball parameters
jamesheavey 27:1b5038b0a7a2 276 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 277 _ball.set_pos(ball_pos);
jamesheavey 91:c01a736fb0d9 278
jamesheavey 124:d635e3154bf3 279 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { // checks every laser
jamesheavey 41:fe383cbb51b2 280 if (
jamesheavey 124:d635e3154bf3 281 (it_L -> get_y() <= GAP_TOP - 2) // if it hits the ceiling
jamesheavey 124:d635e3154bf3 282 ) {
jamesheavey 124:d635e3154bf3 283 it_L -> set_posx(-10); // set its x coordinate off screen
jamesheavey 41:fe383cbb51b2 284 }
jamesheavey 41:fe383cbb51b2 285 }
jamesheavey 91:c01a736fb0d9 286
jamesheavey 41:fe383cbb51b2 287 }
jamesheavey 41:fe383cbb51b2 288
jamesheavey 136:04a2724f90cf 289
jamesheavey 27:1b5038b0a7a2 290 void BreakoutEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 291 {
jamesheavey 27:1b5038b0a7a2 292 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 293 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 294 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 27:1b5038b0a7a2 295
jamesheavey 125:7d9080be1b05 296 // read the paddle position
jamesheavey 92:2e6d88e44f56 297 Vector2D paddle_pos = _paddle.get_pos();
jamesheavey 27:1b5038b0a7a2 298
jamesheavey 125:7d9080be1b05 299 // check if ball overlaps with the paddle
jamesheavey 27:1b5038b0a7a2 300 if (
jamesheavey 92:2e6d88e44f56 301 (ball_pos.x >= paddle_pos.x) && //left
jamesheavey 92:2e6d88e44f56 302 (ball_pos.x <= paddle_pos.x + _paddle_width) && //right
jamesheavey 82:d1341d632890 303 (ball_pos.y >= _paddley) && //bottom
jamesheavey 82:d1341d632890 304 (ball_pos.y <= _paddley + _paddle_height) //top
jamesheavey 125:7d9080be1b05 305 ) {
jamesheavey 27:1b5038b0a7a2 306 pad.tone(1000.0,0.1);
jamesheavey 82:d1341d632890 307 ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 308 ball_velocity.y = -ball_velocity.y;
jamesheavey 125:7d9080be1b05 309
jamesheavey 125:7d9080be1b05 310 // below is a method to control the ball reflect angle based on where it hits the paddle
jamesheavey 125:7d9080be1b05 311 // however cannot be used as it does not work with collisions due to varying angles and speeds
jamesheavey 125:7d9080be1b05 312 // would work with greater pixel density in the screen
jamesheavey 91:c01a736fb0d9 313
jamesheavey 92:2e6d88e44f56 314 // 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 315 // ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 27:1b5038b0a7a2 316 // ball_velocity.y = -ball_velocity.y;
jamesheavey 27:1b5038b0a7a2 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 27:1b5038b0a7a2 320 // if (ball_velocity.x > 0) {
jamesheavey 27:1b5038b0a7a2 321 // ball_velocity.x = -ball_velocity.x;
jamesheavey 27:1b5038b0a7a2 322 // }
jamesheavey 91:c01a736fb0d9 323 // ball_pos.y = _paddley + _paddle_heigh - 1;
jamesheavey 91:c01a736fb0d9 324 // ball_velocity.y = -tan(ang);
jamesheavey 91:c01a736fb0d9 325 // }
jamesheavey 92:2e6d88e44f56 326 // else if (ball_pos.x >= paddle_pos.x + PADDLE_WIDTH/2) {
jamesheavey 92:2e6d88e44f56 327 // 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 328 // if (ball_velocity.x < 0) {
jamesheavey 91:c01a736fb0d9 329 // ball_velocity.x = -ball_velocity.x;
jamesheavey 91:c01a736fb0d9 330 // }
jamesheavey 91:c01a736fb0d9 331 // ball_pos.y = _paddley + _paddle_height - 1;
jamesheavey 91:c01a736fb0d9 332 // ball_velocity.y = -tan(ang);
jamesheavey 91:c01a736fb0d9 333 // }
jamesheavey 27:1b5038b0a7a2 334 }
jamesheavey 27:1b5038b0a7a2 335
jamesheavey 27:1b5038b0a7a2 336 // write new attributes
jamesheavey 27:1b5038b0a7a2 337 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 338 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 339 }
jamesheavey 27:1b5038b0a7a2 340
jamesheavey 27:1b5038b0a7a2 341 void BreakoutEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 27:1b5038b0a7a2 342 {
jamesheavey 27:1b5038b0a7a2 343 // read current ball attributes
jamesheavey 27:1b5038b0a7a2 344 Vector2D ball_pos = _ball.get_pos();
jamesheavey 27:1b5038b0a7a2 345 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 91:c01a736fb0d9 346
jamesheavey 91:c01a736fb0d9 347 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
jamesheavey 34:07ded1f83c59 348 if (
jamesheavey 39:a9bb03bef107 349 (ball_pos.x >= it_R -> get_x()) && //left
jamesheavey 39:a9bb03bef107 350 (ball_pos.x <= it_R -> get_x() + BRICK_WIDTH) && //right
jamesheavey 39:a9bb03bef107 351 (ball_pos.y >= it_R -> get_y()) && //bottom
jamesheavey 39:a9bb03bef107 352 (ball_pos.y <= it_R -> get_y() + BRICK_HEIGHT) //top
jamesheavey 125:7d9080be1b05 353 ) {
jamesheavey 125:7d9080be1b05 354 if (ball_velocity.y < 0) { // if velocity is negative, it has hit the bottom, therefore correct its pos to the bottom face
jamesheavey 48:966f2cf803ec 355 ball_pos.y = it_R -> get_y() + BRICK_HEIGHT;
jamesheavey 125:7d9080be1b05 356 } else { // else it has hit the top, correct its position to the top face
jamesheavey 125:7d9080be1b05 357 ball_pos.y = it_R -> get_y();
jamesheavey 48:966f2cf803ec 358 }
jamesheavey 125:7d9080be1b05 359 ball_velocity.y = -ball_velocity.y; // invert the balls y velocity
jamesheavey 34:07ded1f83c59 360 // audio feedback
jamesheavey 125:7d9080be1b05 361 pad.tone(1000.0,0.1);
jamesheavey 125:7d9080be1b05 362 it_R -> hit();
jamesheavey 125:7d9080be1b05 363 if(it_R-> hit() == true) { // hit the brick, remove 2 lives from it and check if destroyed
jamesheavey 125:7d9080be1b05 364 it_R -> set_posx(-100); // if destroyed, move off screen
jamesheavey 125:7d9080be1b05 365 dec_num_left(); // decrement the number of bricks left on screen
jamesheavey 67:c362df66fac9 366 }
jamesheavey 34:07ded1f83c59 367 }
jamesheavey 27:1b5038b0a7a2 368 }
jamesheavey 27:1b5038b0a7a2 369 // write new attributes
jamesheavey 27:1b5038b0a7a2 370 _ball.set_velocity(ball_velocity);
jamesheavey 27:1b5038b0a7a2 371 _ball.set_pos(ball_pos);
jamesheavey 27:1b5038b0a7a2 372 }
jamesheavey 36:cb73014d3a99 373
jamesheavey 136:04a2724f90cf 374
jamesheavey 32:4dba7a85dbb2 375 void BreakoutEngine::check_laser_collisions(Gamepad &pad)
jamesheavey 32:4dba7a85dbb2 376 {
jamesheavey 126:1ac594b5f91a 377 for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
jamesheavey 125:7d9080be1b05 378 for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { // check every laser against every brick
jamesheavey 36:cb73014d3a99 379 if (
jamesheavey 39:a9bb03bef107 380 (it_L -> get_x() >= it_R -> get_x()) && //left
jamesheavey 39:a9bb03bef107 381 (it_L -> get_x() <= it_R -> get_x() + BRICK_WIDTH) && //right
jamesheavey 39:a9bb03bef107 382 (it_L -> get_y() >= it_R -> get_y()) && //bottom
jamesheavey 114:280903dd7e06 383 (it_L -> get_y() <= it_R -> get_y() + BRICK_HEIGHT) //top
jamesheavey 125:7d9080be1b05 384 ) {
jamesheavey 125:7d9080be1b05 385 it_L -> set_posx(-10); // if they overlap/collide, move the laser off screen
jamesheavey 36:cb73014d3a99 386 // audio feedback
jamesheavey 125:7d9080be1b05 387 pad.tone(1000.0,0.1);
jamesheavey 125:7d9080be1b05 388 if(it_R-> hit() == true) { // remove a life from the brick, if its destroyed, move it off screen
jamesheavey 39:a9bb03bef107 389 it_R -> set_posx(-100);
jamesheavey 125:7d9080be1b05 390 dec_num_left(); // decrement the number of bricks remaining on screen
jamesheavey 36:cb73014d3a99 391 }
jamesheavey 36:cb73014d3a99 392 }
jamesheavey 34:07ded1f83c59 393 }
jamesheavey 34:07ded1f83c59 394 }
jamesheavey 36:cb73014d3a99 395 }
jamesheavey 32:4dba7a85dbb2 396
jamesheavey 114:280903dd7e06 397
jamesheavey 125:7d9080be1b05 398 void BreakoutEngine::check_powerup_collisions(Gamepad &pad) // checks powerup collisions with the paddle and the bottom of the screen
jamesheavey 114:280903dd7e06 399 {
jamesheavey 125:7d9080be1b05 400 // read the current position of the paddle
jamesheavey 125:7d9080be1b05 401 Vector2D paddle_pos = _paddle.get_pos();
jamesheavey 125:7d9080be1b05 402
jamesheavey 114:280903dd7e06 403 if (
jamesheavey 114:280903dd7e06 404 (_powerup.get_x() >= paddle_pos.x) && //left
jamesheavey 115:aee684f62361 405 (_powerup.get_x() + 8 <= paddle_pos.x + _paddle_width) && //right
jamesheavey 117:4eedd15f2c3d 406 (_powerup.get_y() + 8 >= paddle_pos.y) // top
jamesheavey 125:7d9080be1b05 407 ) {
jamesheavey 114:280903dd7e06 408 pad.tone(2500.0,0.1);
jamesheavey 125:7d9080be1b05 409 _powerup.set_posx(-50); // if the paddle and power up overlap, move the powerup off screen
jamesheavey 125:7d9080be1b05 410 _paddle.inc_life(); // increment the numebr of lives left on the paddle
jamesheavey 114:280903dd7e06 411 }
jamesheavey 123:39740c246fc2 412
jamesheavey 114:280903dd7e06 413 else if (
jamesheavey 114:280903dd7e06 414 (_powerup.get_y() +9 >= HEIGHT) // bottom of screen
jamesheavey 125:7d9080be1b05 415 ) {
jamesheavey 125:7d9080be1b05 416 _powerup.set_posx(-50); // if the powerup reaches the bottom of the screen, move it off screen
jamesheavey 114:280903dd7e06 417 }
jamesheavey 136:04a2724f90cf 418 }
jamesheavey 123:39740c246fc2 419
jamesheavey 114:280903dd7e06 420
jamesheavey 125:7d9080be1b05 421 bool BreakoutEngine::check_loss(Gamepad &pad) // check if the ball has hit the bottom of the screen
jamesheavey 27:1b5038b0a7a2 422 {
jamesheavey 125:7d9080be1b05 423 // read the current ball position
jamesheavey 27:1b5038b0a7a2 424 Vector2D ball_pos = _ball.get_pos();
jamesheavey 125:7d9080be1b05 425
jamesheavey 125:7d9080be1b05 426 if (ball_pos.y > HEIGHT) { // if it has gone off screen
jamesheavey 125:7d9080be1b05 427 _paddle.lose_life(); // decrement the number of lives left
jamesheavey 91:c01a736fb0d9 428
jamesheavey 125:7d9080be1b05 429 _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + (PADDLE_WIDTH/2)); // re initialise the ball above the paddle
jamesheavey 27:1b5038b0a7a2 430 pad.tone(1500.0,0.5);
jamesheavey 125:7d9080be1b05 431 return true; // return true, to be used to flash the screen in the main
jamesheavey 91:c01a736fb0d9 432 } else {
jamesheavey 125:7d9080be1b05 433 return false;
jamesheavey 27:1b5038b0a7a2 434 }
jamesheavey 27:1b5038b0a7a2 435 }
jamesheavey 27:1b5038b0a7a2 436
jamesheavey 136:04a2724f90cf 437
jamesheavey 125:7d9080be1b05 438 void BreakoutEngine::set_paddle_motion(bool tilt, float sens) // sets the motion options for the paddle
jamesheavey 105:4e7585d8e5e2 439 {
jamesheavey 105:4e7585d8e5e2 440 if (tilt == true) {
jamesheavey 125:7d9080be1b05 441 _paddle.set_tilt(); // sets tilt
jamesheavey 105:4e7585d8e5e2 442 } else {
jamesheavey 125:7d9080be1b05 443 _paddle.set_joy(); // sets joystick
jamesheavey 105:4e7585d8e5e2 444 }
jamesheavey 125:7d9080be1b05 445 _paddle.set_sens(sens); // sets sensitivity
jamesheavey 105:4e7585d8e5e2 446 }
jamesheavey 105:4e7585d8e5e2 447
jamesheavey 136:04a2724f90cf 448
jamesheavey 123:39740c246fc2 449 void BreakoutEngine::print_scores(N5110 &lcd)
jamesheavey 27:1b5038b0a7a2 450 {
jamesheavey 125:7d9080be1b05 451 int score = _prev_score + (18 - get_num_left())*100 * (_multiplier + 1); // current score depends on number left, the multiplier and the previous score
jamesheavey 125:7d9080be1b05 452 _score = score; // set the member variable _score
jamesheavey 126:1ac594b5f91a 453
jamesheavey 126:1ac594b5f91a 454 char buffer[14];
jamesheavey 126:1ac594b5f91a 455 sprintf(buffer,"%2d",score); // put score in buffer
jamesheavey 91:c01a736fb0d9 456 lcd.printString("SCORE: ",2,0);
jamesheavey 126:1ac594b5f91a 457 lcd.printString(buffer,WIDTH/2 -2,0); // print buffer on LCD
jamesheavey 27:1b5038b0a7a2 458 }
jamesheavey 62:64559062e0ec 459
jamesheavey 136:04a2724f90cf 460
jamesheavey 119:473cac51ddf0 461 void BreakoutEngine::check_life_powerup()
jamesheavey 123:39740c246fc2 462 {
jamesheavey 119:473cac51ddf0 463 srand(time(0)); // Initialize random number generator.
jamesheavey 126:1ac594b5f91a 464 int r1 = (rand() % 25) + 1;
jamesheavey 126:1ac594b5f91a 465 int r2 = (rand() % 25) + 1; // 2 random number between 1 and 26
jamesheavey 126:1ac594b5f91a 466 int rx = (rand() % 60) + 11; // generate a random x coordinate for the powerup to be place
jamesheavey 119:473cac51ddf0 467 //printf("r1 = %d",r1);
jamesheavey 119:473cac51ddf0 468 //printf("r2 = %d",r2);
jamesheavey 119:473cac51ddf0 469 //printf("rx = %d",rx);
jamesheavey 135:888ae932cd70 470 if ((get_paddle_lives() < 6) & (r1 == r2)) { // if lives are not max, and the random condition is met
jamesheavey 126:1ac594b5f91a 471 _powerup.set_posx(rx); // set the powerup x
jamesheavey 126:1ac594b5f91a 472 _powerup.set_posy(HEIGHT/2); // set the y at the middle of the screen
jamesheavey 119:473cac51ddf0 473 }
jamesheavey 119:473cac51ddf0 474 }
jamesheavey 119:473cac51ddf0 475
jamesheavey 136:04a2724f90cf 476
jamesheavey 91:c01a736fb0d9 477 int BreakoutEngine::get_prev_score()
jamesheavey 91:c01a736fb0d9 478 {
jamesheavey 126:1ac594b5f91a 479 return _prev_score; // return the previous score
jamesheavey 47:1d1a827be81b 480 }
jamesheavey 93:18f81996ea89 481
jamesheavey 136:04a2724f90cf 482
jamesheavey 91:c01a736fb0d9 483 void BreakoutEngine::set_prev_score(int prev_score)
jamesheavey 91:c01a736fb0d9 484 {
jamesheavey 126:1ac594b5f91a 485 _prev_score = prev_score; // set the member variable _prev_score
jamesheavey 64:c3426b417ad9 486 }
jamesheavey 93:18f81996ea89 487
jamesheavey 136:04a2724f90cf 488
jamesheavey 91:c01a736fb0d9 489 int BreakoutEngine::get_num_left()
jamesheavey 91:c01a736fb0d9 490 {
jamesheavey 126:1ac594b5f91a 491 return _number_left; // return the number of bricks left on screen
jamesheavey 27:1b5038b0a7a2 492 }
jamesheavey 93:18f81996ea89 493
jamesheavey 136:04a2724f90cf 494
jamesheavey 92:2e6d88e44f56 495 void BreakoutEngine::dec_num_left()
jamesheavey 91:c01a736fb0d9 496 {
jamesheavey 126:1ac594b5f91a 497 _number_left -= 1; // decrement the member variable _number_left when a brick is destroyed
jamesheavey 60:63d69184ec0a 498 }
jamesheavey 93:18f81996ea89 499
jamesheavey 136:04a2724f90cf 500
jamesheavey 91:c01a736fb0d9 501 void BreakoutEngine::reset_num_left()
jamesheavey 91:c01a736fb0d9 502 {
jamesheavey 126:1ac594b5f91a 503 _number_left = 18; // reset the number left to 18 when the game is reset
jamesheavey 62:64559062e0ec 504 }
jamesheavey 93:18f81996ea89 505
jamesheavey 136:04a2724f90cf 506
jamesheavey 91:c01a736fb0d9 507 int BreakoutEngine::get_score()
jamesheavey 91:c01a736fb0d9 508 {
jamesheavey 126:1ac594b5f91a 509 return _score; // return the member variable _score
jamesheavey 62:64559062e0ec 510 }
jamesheavey 93:18f81996ea89 511
jamesheavey 136:04a2724f90cf 512
jamesheavey 91:c01a736fb0d9 513 void BreakoutEngine::inc_mult()
jamesheavey 91:c01a736fb0d9 514 {
jamesheavey 126:1ac594b5f91a 515 _multiplier ++; // increment the _multiplier when continue is selected upon victory
jamesheavey 62:64559062e0ec 516 }
jamesheavey 93:18f81996ea89 517
jamesheavey 136:04a2724f90cf 518
jamesheavey 99:d8f1570faa05 519 int BreakoutEngine::get_mult()
jamesheavey 91:c01a736fb0d9 520 {
jamesheavey 126:1ac594b5f91a 521 return _multiplier; // retrieve the multiplier value
jamesheavey 99:d8f1570faa05 522 }
jamesheavey 99:d8f1570faa05 523
jamesheavey 136:04a2724f90cf 524
jamesheavey 135:888ae932cd70 525 void BreakoutEngine::reset_mult()
jamesheavey 99:d8f1570faa05 526 {
jamesheavey 126:1ac594b5f91a 527 _multiplier = 0; // reset the multiplier to 0
jamesheavey 62:64559062e0ec 528 }
jamesheavey 93:18f81996ea89 529
jamesheavey 136:04a2724f90cf 530
jamesheavey 92:2e6d88e44f56 531 void BreakoutEngine::inc_index()
jamesheavey 92:2e6d88e44f56 532 {
jamesheavey 126:1ac594b5f91a 533 _index ++; // increment the laser index
jamesheavey 92:2e6d88e44f56 534 }
jamesheavey 93:18f81996ea89 535
jamesheavey 136:04a2724f90cf 536
jamesheavey 92:2e6d88e44f56 537 void BreakoutEngine::reset_index()
jamesheavey 92:2e6d88e44f56 538 {
jamesheavey 126:1ac594b5f91a 539 _index = 0; // reset the laser index
jamesheavey 92:2e6d88e44f56 540 }
jamesheavey 114:280903dd7e06 541
jamesheavey 136:04a2724f90cf 542
jamesheavey 116:2793d31ca691 543 void BreakoutEngine::reset_paddle_lives()
jamesheavey 116:2793d31ca691 544 {
jamesheavey 116:2793d31ca691 545 _paddle.reset_lives(); // resets paddle lives
jamesheavey 126:1ac594b5f91a 546 }
jamesheavey 126:1ac594b5f91a 547
jamesheavey 136:04a2724f90cf 548
jamesheavey 135:888ae932cd70 549 int BreakoutEngine::get_paddle_lives()
jamesheavey 126:1ac594b5f91a 550 {
jamesheavey 126:1ac594b5f91a 551 return _paddle.get_lives(); // return the number of lives left
jamesheavey 126:1ac594b5f91a 552 }