FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 19:24:47 2019 +0000
Revision:
125:7d9080be1b05
Parent:
124:d635e3154bf3
Child:
126:1ac594b5f91a
cp

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