Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BreakoutEngine/BreakoutEngine.cpp@99:d8f1570faa05, 2019-05-07 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue May 07 16:56:39 2019 +0000
- Revision:
- 99:d8f1570faa05
- Parent:
- 94:8fa117200f6b
- Child:
- 105:4e7585d8e5e2
bonus score based on time
Who changed what in which revision?
| User | Revision | Line number | New 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 | 93:18f81996ea89 | 25 | _multiplier = 1; // 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 | 93:18f81996ea89 | 91 | _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 | 92 | |
| jamesheavey | 93:18f81996ea89 | 93 | int pointer = 0; // tracks the rows, if 6 bricks are placed, it moves to the next row |
| jamesheavey | 91:c01a736fb0d9 | 94 | for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { |
| jamesheavey | 93:18f81996ea89 | 95 | if (pointer <= 5) { // 6 bricks in the first row |
| jamesheavey | 60:63d69184ec0a | 96 | it_R -> set_posx((pointer * 13) + 3); |
| jamesheavey | 91:c01a736fb0d9 | 97 | } else if (pointer <= 11) { |
| jamesheavey | 93:18f81996ea89 | 98 | it_R -> set_posx(((pointer-6) * 13) + 3); // pointer - 6 to offset it back to the first column of bricks |
| jamesheavey | 91:c01a736fb0d9 | 99 | } else if (pointer <= 17) { |
| jamesheavey | 93:18f81996ea89 | 100 | it_R -> set_posx(((pointer-12) * 13) + 3); // pointer - 12 to offset it back to the first column of bricks |
| jamesheavey | 60:63d69184ec0a | 101 | } |
| jamesheavey | 91:c01a736fb0d9 | 102 | |
| jamesheavey | 93:18f81996ea89 | 103 | it_R -> reset_lives(_multiplier); // increases the bricks number of lives by the multiplier + their _base_lives |
| jamesheavey | 93:18f81996ea89 | 104 | pointer ++; // increment the pointer |
| jamesheavey | 60:63d69184ec0a | 105 | } |
| jamesheavey | 91:c01a736fb0d9 | 106 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { |
| jamesheavey | 93:18f81996ea89 | 107 | it_L -> set_posx(-10); // moves all the lasers off screen |
| jamesheavey | 84:6483503a72fc | 108 | } |
| jamesheavey | 60:63d69184ec0a | 109 | } |
| jamesheavey | 91:c01a736fb0d9 | 110 | |
| jamesheavey | 60:63d69184ec0a | 111 | |
| jamesheavey | 72:7254d2a8a1cd | 112 | void BreakoutEngine::read_input(Gamepad &pad, bool tilt, float sens) |
| jamesheavey | 27:1b5038b0a7a2 | 113 | { |
| jamesheavey | 27:1b5038b0a7a2 | 114 | _d = pad.get_direction(); |
| jamesheavey | 27:1b5038b0a7a2 | 115 | _mag = pad.get_mag(); |
| jamesheavey | 91:c01a736fb0d9 | 116 | |
| jamesheavey | 72:7254d2a8a1cd | 117 | if (tilt == true) { |
| jamesheavey | 82:d1341d632890 | 118 | _paddle.set_tilt(); |
| jamesheavey | 91:c01a736fb0d9 | 119 | } else { |
| jamesheavey | 82:d1341d632890 | 120 | _paddle.set_joy(); |
| jamesheavey | 27:1b5038b0a7a2 | 121 | } |
| jamesheavey | 91:c01a736fb0d9 | 122 | |
| jamesheavey | 82:d1341d632890 | 123 | _paddle.set_sens(sens); |
| jamesheavey | 91:c01a736fb0d9 | 124 | |
| jamesheavey | 45:b1ac80481d4f | 125 | if (pad.check_event(Gamepad::B_PRESSED) && _cool_time <= 0) { // max of 3 lasers on screen at once |
| jamesheavey | 91:c01a736fb0d9 | 126 | |
| jamesheavey | 82:d1341d632890 | 127 | Vector2D p_pos = _paddle.get_pos(); |
| jamesheavey | 37:dd1538ae6534 | 128 | it_L = listofLasers.begin(); |
| jamesheavey | 91:c01a736fb0d9 | 129 | switch(_index) { |
| jamesheavey | 30:e3d2f0ca416f | 130 | case 0: |
| jamesheavey | 37:dd1538ae6534 | 131 | advance(it_L, 0); |
| 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 1: |
| jamesheavey | 37:dd1538ae6534 | 137 | advance(it_L, 1); |
| 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 | inc_index(); |
| jamesheavey | 30:e3d2f0ca416f | 141 | break; |
| jamesheavey | 30:e3d2f0ca416f | 142 | case 2: |
| jamesheavey | 37:dd1538ae6534 | 143 | advance(it_L, 2); |
| jamesheavey | 37:dd1538ae6534 | 144 | it_L -> set_posx(p_pos.x+7); |
| jamesheavey | 37:dd1538ae6534 | 145 | it_L -> set_posy(p_pos.y); |
| jamesheavey | 30:e3d2f0ca416f | 146 | reset_index(); |
| jamesheavey | 30:e3d2f0ca416f | 147 | break; |
| jamesheavey | 30:e3d2f0ca416f | 148 | } |
| jamesheavey | 94:8fa117200f6b | 149 | |
| jamesheavey | 94:8fa117200f6b | 150 | it_L = listofLasers.end(); |
| jamesheavey | 91:c01a736fb0d9 | 151 | |
| jamesheavey | 46:810b3a7fc387 | 152 | _cool_time = 0.75f; |
| jamesheavey | 91:c01a736fb0d9 | 153 | } else { |
| jamesheavey | 45:b1ac80481d4f | 154 | _cool_time -= 0.125; // 1/8 as fps is 8 |
| jamesheavey | 45:b1ac80481d4f | 155 | } |
| jamesheavey | 91:c01a736fb0d9 | 156 | |
| jamesheavey | 27:1b5038b0a7a2 | 157 | } |
| jamesheavey | 27:1b5038b0a7a2 | 158 | |
| jamesheavey | 27:1b5038b0a7a2 | 159 | void BreakoutEngine::draw(N5110 &lcd) |
| jamesheavey | 27:1b5038b0a7a2 | 160 | { |
| jamesheavey | 27:1b5038b0a7a2 | 161 | // draw the elements in the LCD buffer |
| jamesheavey | 27:1b5038b0a7a2 | 162 | // pitch |
| jamesheavey | 40:ac53905346fb | 163 | lcd.drawRect(0,GAP_TOP - 2,WIDTH,HEIGHT - GAP_TOP + 2,FILL_TRANSPARENT); |
| jamesheavey | 27:1b5038b0a7a2 | 164 | //score |
| jamesheavey | 40:ac53905346fb | 165 | print_scores(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 166 | // paddles |
| jamesheavey | 82:d1341d632890 | 167 | _paddle.draw(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 168 | // ball |
| jamesheavey | 27:1b5038b0a7a2 | 169 | _ball.draw(lcd); |
| jamesheavey | 91:c01a736fb0d9 | 170 | |
| jamesheavey | 27:1b5038b0a7a2 | 171 | //print_scores(lcd); |
| jamesheavey | 91:c01a736fb0d9 | 172 | |
| jamesheavey | 91:c01a736fb0d9 | 173 | for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { |
| jamesheavey | 39:a9bb03bef107 | 174 | it_R->draw(lcd); |
| jamesheavey | 34:07ded1f83c59 | 175 | } |
| jamesheavey | 91:c01a736fb0d9 | 176 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { |
| jamesheavey | 35:3a614d539a54 | 177 | it_L->draw(lcd); |
| jamesheavey | 35:3a614d539a54 | 178 | } |
| jamesheavey | 27:1b5038b0a7a2 | 179 | |
| jamesheavey | 27:1b5038b0a7a2 | 180 | } |
| jamesheavey | 27:1b5038b0a7a2 | 181 | |
| jamesheavey | 27:1b5038b0a7a2 | 182 | void BreakoutEngine::update(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 183 | { |
| jamesheavey | 81:735e5ee2c92a | 184 | check_loss(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 185 | // important to update paddles and ball before checking collisions so can |
| jamesheavey | 27:1b5038b0a7a2 | 186 | // correct for it before updating the display |
| jamesheavey | 82:d1341d632890 | 187 | _paddle.update(_d,_mag); |
| jamesheavey | 27:1b5038b0a7a2 | 188 | _ball.update(); |
| jamesheavey | 91:c01a736fb0d9 | 189 | |
| jamesheavey | 91:c01a736fb0d9 | 190 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { |
| jamesheavey | 35:3a614d539a54 | 191 | it_L->update(); |
| jamesheavey | 35:3a614d539a54 | 192 | } |
| jamesheavey | 91:c01a736fb0d9 | 193 | |
| jamesheavey | 27:1b5038b0a7a2 | 194 | lives_leds(pad); |
| jamesheavey | 91:c01a736fb0d9 | 195 | |
| jamesheavey | 42:347c20a16ee6 | 196 | check_wall_collisions(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 197 | check_paddle_collisions(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 198 | check_brick_collisions(pad); |
| jamesheavey | 36:cb73014d3a99 | 199 | check_laser_collisions(pad); |
| jamesheavey | 91:c01a736fb0d9 | 200 | |
| jamesheavey | 91:c01a736fb0d9 | 201 | |
| jamesheavey | 27:1b5038b0a7a2 | 202 | } |
| jamesheavey | 27:1b5038b0a7a2 | 203 | |
| jamesheavey | 27:1b5038b0a7a2 | 204 | void BreakoutEngine::lives_leds(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 205 | { |
| jamesheavey | 82:d1341d632890 | 206 | if (_paddle.get_lives() == 0) { |
| jamesheavey | 27:1b5038b0a7a2 | 207 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 208 | } |
| jamesheavey | 91:c01a736fb0d9 | 209 | |
| jamesheavey | 82:d1341d632890 | 210 | else if (_paddle.get_lives() == 1) { |
| 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 | } |
| jamesheavey | 91:c01a736fb0d9 | 215 | |
| jamesheavey | 82:d1341d632890 | 216 | else if (_paddle.get_lives() == 2) { |
| jamesheavey | 27:1b5038b0a7a2 | 217 | //turn leftmost led on |
| jamesheavey | 27:1b5038b0a7a2 | 218 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 219 | pad.led(1,1); |
| jamesheavey | 27:1b5038b0a7a2 | 220 | pad.led(2,1); |
| jamesheavey | 27:1b5038b0a7a2 | 221 | } |
| jamesheavey | 91:c01a736fb0d9 | 222 | |
| jamesheavey | 82:d1341d632890 | 223 | else if (_paddle.get_lives() == 3) { |
| jamesheavey | 27:1b5038b0a7a2 | 224 | //turn leftmost led on |
| jamesheavey | 27:1b5038b0a7a2 | 225 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 226 | pad.led(1,1); |
| jamesheavey | 27:1b5038b0a7a2 | 227 | pad.led(2,1); |
| jamesheavey | 27:1b5038b0a7a2 | 228 | pad.led(3,1); |
| jamesheavey | 27:1b5038b0a7a2 | 229 | } |
| jamesheavey | 27:1b5038b0a7a2 | 230 | |
| jamesheavey | 82:d1341d632890 | 231 | else if (_paddle.get_lives() == 4) { |
| jamesheavey | 27:1b5038b0a7a2 | 232 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 233 | pad.led(5,0); |
| jamesheavey | 27:1b5038b0a7a2 | 234 | pad.led(6,0); |
| jamesheavey | 27:1b5038b0a7a2 | 235 | } |
| jamesheavey | 91:c01a736fb0d9 | 236 | |
| jamesheavey | 82:d1341d632890 | 237 | else if (_paddle.get_lives() == 5) { |
| jamesheavey | 27:1b5038b0a7a2 | 238 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 239 | pad.led(6,0); |
| jamesheavey | 27:1b5038b0a7a2 | 240 | } |
| jamesheavey | 91:c01a736fb0d9 | 241 | |
| jamesheavey | 82:d1341d632890 | 242 | else if (_paddle.get_lives() == 6) { |
| jamesheavey | 27:1b5038b0a7a2 | 243 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 244 | } |
| jamesheavey | 27:1b5038b0a7a2 | 245 | } |
| jamesheavey | 27:1b5038b0a7a2 | 246 | |
| jamesheavey | 42:347c20a16ee6 | 247 | void BreakoutEngine::check_wall_collisions(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 248 | { |
| jamesheavey | 27:1b5038b0a7a2 | 249 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 250 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 251 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 27:1b5038b0a7a2 | 252 | |
| jamesheavey | 40:ac53905346fb | 253 | if (ball_pos.y <= GAP_TOP - 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 40:ac53905346fb | 254 | ball_pos.y = GAP_TOP - 1; // bounce off ceiling without going off screen |
| jamesheavey | 27:1b5038b0a7a2 | 255 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 27:1b5038b0a7a2 | 256 | // audio feedback |
| jamesheavey | 27:1b5038b0a7a2 | 257 | pad.tone(750.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 258 | } |
| jamesheavey | 91:c01a736fb0d9 | 259 | |
| jamesheavey | 27:1b5038b0a7a2 | 260 | else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 27:1b5038b0a7a2 | 261 | ball_pos.x = 1; // bounce off ceiling without 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 | 91:c01a736fb0d9 | 266 | |
| jamesheavey | 27:1b5038b0a7a2 | 267 | // check if hit bottom wall |
| jamesheavey | 27:1b5038b0a7a2 | 268 | else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47 |
| jamesheavey | 27:1b5038b0a7a2 | 269 | // hit bottom |
| jamesheavey | 27:1b5038b0a7a2 | 270 | ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen |
| jamesheavey | 27:1b5038b0a7a2 | 271 | ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 272 | // audio feedback |
| jamesheavey | 27:1b5038b0a7a2 | 273 | pad.tone(750.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 274 | } |
| jamesheavey | 27:1b5038b0a7a2 | 275 | |
| jamesheavey | 27:1b5038b0a7a2 | 276 | // update ball parameters |
| jamesheavey | 27:1b5038b0a7a2 | 277 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 27:1b5038b0a7a2 | 278 | _ball.set_pos(ball_pos); |
| jamesheavey | 91:c01a736fb0d9 | 279 | |
| jamesheavey | 91:c01a736fb0d9 | 280 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { |
| jamesheavey | 41:fe383cbb51b2 | 281 | if ( |
| jamesheavey | 42:347c20a16ee6 | 282 | (it_L -> get_y() <= GAP_TOP - 2) |
| jamesheavey | 41:fe383cbb51b2 | 283 | ) { // 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 | 284 | // if it has, fix position and reflect x velocity |
| jamesheavey | 41:fe383cbb51b2 | 285 | it_L -> set_posx(-10); |
| jamesheavey | 41:fe383cbb51b2 | 286 | } |
| jamesheavey | 41:fe383cbb51b2 | 287 | } |
| jamesheavey | 91:c01a736fb0d9 | 288 | |
| jamesheavey | 41:fe383cbb51b2 | 289 | } |
| jamesheavey | 41:fe383cbb51b2 | 290 | |
| jamesheavey | 27:1b5038b0a7a2 | 291 | void BreakoutEngine::check_paddle_collisions(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 292 | { |
| jamesheavey | 27:1b5038b0a7a2 | 293 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 294 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 295 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 27:1b5038b0a7a2 | 296 | |
| jamesheavey | 92:2e6d88e44f56 | 297 | // check paddle first |
| jamesheavey | 92:2e6d88e44f56 | 298 | Vector2D paddle_pos = _paddle.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 299 | |
| jamesheavey | 27:1b5038b0a7a2 | 300 | // see if ball has hit the paddle by checking for overlaps |
| jamesheavey | 27:1b5038b0a7a2 | 301 | if ( |
| jamesheavey | 92:2e6d88e44f56 | 302 | (ball_pos.x >= paddle_pos.x) && //left |
| jamesheavey | 92:2e6d88e44f56 | 303 | (ball_pos.x <= paddle_pos.x + _paddle_width) && //right |
| jamesheavey | 82:d1341d632890 | 304 | (ball_pos.y >= _paddley) && //bottom |
| jamesheavey | 82:d1341d632890 | 305 | (ball_pos.y <= _paddley + _paddle_height) //top |
| jamesheavey | 27:1b5038b0a7a2 | 306 | ) { // 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 | 307 | // if it has, fix position and reflect x velocity |
| jamesheavey | 27:1b5038b0a7a2 | 308 | pad.tone(1000.0,0.1); |
| jamesheavey | 82:d1341d632890 | 309 | ball_pos.y = _paddley + _paddle_height - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 310 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 91:c01a736fb0d9 | 311 | |
| jamesheavey | 92:2e6d88e44f56 | 312 | // 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 | 313 | // ball_pos.y = _paddley + _paddle_height - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 314 | // ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 27:1b5038b0a7a2 | 315 | // } |
| jamesheavey | 92:2e6d88e44f56 | 316 | // else if (ball_pos.x <= paddle_pos.x + PADDLE_WIDTH/2) { |
| jamesheavey | 92:2e6d88e44f56 | 317 | // 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 | 318 | // if (ball_velocity.x > 0) { |
| jamesheavey | 27:1b5038b0a7a2 | 319 | // ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 320 | // } |
| jamesheavey | 91:c01a736fb0d9 | 321 | // ball_pos.y = _paddley + _paddle_heigh - 1; |
| jamesheavey | 91:c01a736fb0d9 | 322 | // ball_velocity.y = -tan(ang); |
| jamesheavey | 91:c01a736fb0d9 | 323 | // } |
| jamesheavey | 92:2e6d88e44f56 | 324 | // else if (ball_pos.x >= paddle_pos.x + PADDLE_WIDTH/2) { |
| jamesheavey | 92:2e6d88e44f56 | 325 | // 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 | 326 | // if (ball_velocity.x < 0) { |
| jamesheavey | 91:c01a736fb0d9 | 327 | // ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 91:c01a736fb0d9 | 328 | // } |
| jamesheavey | 91:c01a736fb0d9 | 329 | // ball_pos.y = _paddley + _paddle_height - 1; |
| jamesheavey | 91:c01a736fb0d9 | 330 | // ball_velocity.y = -tan(ang); |
| jamesheavey | 91:c01a736fb0d9 | 331 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 332 | } |
| jamesheavey | 27:1b5038b0a7a2 | 333 | |
| jamesheavey | 27:1b5038b0a7a2 | 334 | // write new attributes |
| jamesheavey | 27:1b5038b0a7a2 | 335 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 27:1b5038b0a7a2 | 336 | _ball.set_pos(ball_pos); |
| jamesheavey | 27:1b5038b0a7a2 | 337 | } |
| jamesheavey | 27:1b5038b0a7a2 | 338 | |
| jamesheavey | 27:1b5038b0a7a2 | 339 | void BreakoutEngine::check_brick_collisions(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 340 | { |
| jamesheavey | 27:1b5038b0a7a2 | 341 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 342 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 343 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 91:c01a736fb0d9 | 344 | |
| jamesheavey | 91:c01a736fb0d9 | 345 | for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { |
| jamesheavey | 34:07ded1f83c59 | 346 | if ( |
| jamesheavey | 39:a9bb03bef107 | 347 | (ball_pos.x >= it_R -> get_x()) && //left |
| jamesheavey | 39:a9bb03bef107 | 348 | (ball_pos.x <= it_R -> get_x() + BRICK_WIDTH) && //right |
| jamesheavey | 39:a9bb03bef107 | 349 | (ball_pos.y >= it_R -> get_y()) && //bottom |
| jamesheavey | 39:a9bb03bef107 | 350 | (ball_pos.y <= it_R -> get_y() + BRICK_HEIGHT) //top |
| jamesheavey | 34:07ded1f83c59 | 351 | ) { // 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 | 352 | // if it has, fix position and reflect x velocity |
| jamesheavey | 91:c01a736fb0d9 | 353 | if (ball_velocity.y < 0) { |
| jamesheavey | 48:966f2cf803ec | 354 | ball_pos.y = it_R -> get_y() + BRICK_HEIGHT; |
| jamesheavey | 91:c01a736fb0d9 | 355 | } else { |
| jamesheavey | 48:966f2cf803ec | 356 | ball_pos.y = it_R -> get_y(); |
| jamesheavey | 48:966f2cf803ec | 357 | } |
| jamesheavey | 34:07ded1f83c59 | 358 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 34:07ded1f83c59 | 359 | // audio feedback |
| jamesheavey | 34:07ded1f83c59 | 360 | pad.tone(1000.0,0.1); |
| jamesheavey | 39:a9bb03bef107 | 361 | it_R -> hit(); |
| jamesheavey | 34:07ded1f83c59 | 362 | //delete _brick11; |
| jamesheavey | 69:db1354676fde | 363 | if(it_R-> hit() == true) { |
| jamesheavey | 67:c362df66fac9 | 364 | it_R -> set_posx(-100); |
| jamesheavey | 92:2e6d88e44f56 | 365 | dec_num_left(); |
| 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 | 32:4dba7a85dbb2 | 374 | void BreakoutEngine::check_laser_collisions(Gamepad &pad) |
| jamesheavey | 32:4dba7a85dbb2 | 375 | { |
| jamesheavey | 32:4dba7a85dbb2 | 376 | // read current ball attributes |
| jamesheavey | 92:2e6d88e44f56 | 377 | // check paddle first |
| jamesheavey | 91:c01a736fb0d9 | 378 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) { |
| jamesheavey | 91:c01a736fb0d9 | 379 | for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) { |
| jamesheavey | 36:cb73014d3a99 | 380 | if ( |
| jamesheavey | 39:a9bb03bef107 | 381 | (it_L -> get_x() >= it_R -> get_x()) && //left |
| jamesheavey | 39:a9bb03bef107 | 382 | (it_L -> get_x() <= it_R -> get_x() + BRICK_WIDTH) && //right |
| jamesheavey | 39:a9bb03bef107 | 383 | (it_L -> get_y() >= it_R -> get_y()) && //bottom |
| jamesheavey | 39:a9bb03bef107 | 384 | (it_L -> get_y()<= it_R -> get_y() + BRICK_HEIGHT) //top |
| jamesheavey | 36:cb73014d3a99 | 385 | ) { // 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 | 386 | // if it has, fix position and reflect x velocity |
| jamesheavey | 37:dd1538ae6534 | 387 | it_L -> set_posx(-10); |
| jamesheavey | 36:cb73014d3a99 | 388 | // audio feedback |
| jamesheavey | 36:cb73014d3a99 | 389 | pad.tone(1000.0,0.1); |
| jamesheavey | 69:db1354676fde | 390 | if(it_R-> hit() == true) { |
| jamesheavey | 39:a9bb03bef107 | 391 | it_R -> set_posx(-100); |
| jamesheavey | 92:2e6d88e44f56 | 392 | dec_num_left(); |
| jamesheavey | 36:cb73014d3a99 | 393 | } |
| jamesheavey | 36:cb73014d3a99 | 394 | } |
| jamesheavey | 34:07ded1f83c59 | 395 | } |
| jamesheavey | 34:07ded1f83c59 | 396 | } |
| jamesheavey | 36:cb73014d3a99 | 397 | } |
| jamesheavey | 32:4dba7a85dbb2 | 398 | |
| jamesheavey | 81:735e5ee2c92a | 399 | bool BreakoutEngine::check_loss(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 400 | { |
| jamesheavey | 27:1b5038b0a7a2 | 401 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 92:2e6d88e44f56 | 402 | // paddle has scored |
| jamesheavey | 27:1b5038b0a7a2 | 403 | if (ball_pos.y > HEIGHT) { |
| jamesheavey | 82:d1341d632890 | 404 | _paddle.lose_life(); |
| jamesheavey | 27:1b5038b0a7a2 | 405 | //lose_screen(); // go to loss screen then initialise again |
| jamesheavey | 91:c01a736fb0d9 | 406 | |
| jamesheavey | 93:18f81996ea89 | 407 | _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + (PADDLE_WIDTH/2)); |
| jamesheavey | 27:1b5038b0a7a2 | 408 | pad.tone(1500.0,0.5); |
| jamesheavey | 27:1b5038b0a7a2 | 409 | return true; |
| jamesheavey | 91:c01a736fb0d9 | 410 | } else { |
| jamesheavey | 27:1b5038b0a7a2 | 411 | return false; |
| jamesheavey | 27:1b5038b0a7a2 | 412 | } |
| jamesheavey | 27:1b5038b0a7a2 | 413 | } |
| jamesheavey | 27:1b5038b0a7a2 | 414 | |
| jamesheavey | 34:07ded1f83c59 | 415 | void BreakoutEngine::print_scores(N5110 &lcd) //maybe add to oneless |
| jamesheavey | 27:1b5038b0a7a2 | 416 | { |
| jamesheavey | 27:1b5038b0a7a2 | 417 | // get scores from paddles |
| jamesheavey | 64:c3426b417ad9 | 418 | int score = _prev_score + (18 - get_num_left())*100 * _multiplier; //maybe add a previous score so the score carries over through victory screens. add hi score fix so restart restarts from 321 |
| jamesheavey | 91:c01a736fb0d9 | 419 | // also, add a multiplier of 2 for every contiue on victory |
| jamesheavey | 91:c01a736fb0d9 | 420 | _score = score; |
| jamesheavey | 91:c01a736fb0d9 | 421 | |
| jamesheavey | 27:1b5038b0a7a2 | 422 | // print to LCD i |
| jamesheavey | 27:1b5038b0a7a2 | 423 | char buffer1[14]; |
| jamesheavey | 64:c3426b417ad9 | 424 | sprintf(buffer1,"%2d",score); |
| jamesheavey | 91:c01a736fb0d9 | 425 | lcd.printString("SCORE: ",2,0); |
| jamesheavey | 64:c3426b417ad9 | 426 | lcd.printString(buffer1,WIDTH/2 -2,0); // font is 8 wide, so leave 4 pixel gap from middle assuming two digits |
| jamesheavey | 27:1b5038b0a7a2 | 427 | } |
| jamesheavey | 62:64559062e0ec | 428 | |
| jamesheavey | 93:18f81996ea89 | 429 | int BreakoutEngine::get_lives() |
| jamesheavey | 93:18f81996ea89 | 430 | { |
| jamesheavey | 93:18f81996ea89 | 431 | return _paddle.get_lives(); |
| jamesheavey | 93:18f81996ea89 | 432 | } |
| jamesheavey | 62:64559062e0ec | 433 | |
| jamesheavey | 91:c01a736fb0d9 | 434 | int BreakoutEngine::get_prev_score() |
| jamesheavey | 91:c01a736fb0d9 | 435 | { |
| jamesheavey | 47:1d1a827be81b | 436 | return _prev_score; |
| jamesheavey | 47:1d1a827be81b | 437 | } |
| jamesheavey | 93:18f81996ea89 | 438 | |
| jamesheavey | 91:c01a736fb0d9 | 439 | void BreakoutEngine::set_prev_score(int prev_score) |
| jamesheavey | 91:c01a736fb0d9 | 440 | { |
| jamesheavey | 64:c3426b417ad9 | 441 | _prev_score = prev_score; |
| jamesheavey | 64:c3426b417ad9 | 442 | } |
| jamesheavey | 93:18f81996ea89 | 443 | |
| jamesheavey | 91:c01a736fb0d9 | 444 | int BreakoutEngine::get_num_left() |
| jamesheavey | 91:c01a736fb0d9 | 445 | { |
| jamesheavey | 27:1b5038b0a7a2 | 446 | return _number_left; |
| jamesheavey | 27:1b5038b0a7a2 | 447 | } |
| jamesheavey | 93:18f81996ea89 | 448 | |
| jamesheavey | 92:2e6d88e44f56 | 449 | void BreakoutEngine::dec_num_left() |
| jamesheavey | 91:c01a736fb0d9 | 450 | { |
| jamesheavey | 27:1b5038b0a7a2 | 451 | _number_left -= 1; |
| jamesheavey | 60:63d69184ec0a | 452 | } |
| jamesheavey | 93:18f81996ea89 | 453 | |
| jamesheavey | 91:c01a736fb0d9 | 454 | void BreakoutEngine::reset_num_left() |
| jamesheavey | 91:c01a736fb0d9 | 455 | { |
| jamesheavey | 60:63d69184ec0a | 456 | _number_left = 18; |
| jamesheavey | 62:64559062e0ec | 457 | } |
| jamesheavey | 93:18f81996ea89 | 458 | |
| jamesheavey | 91:c01a736fb0d9 | 459 | int BreakoutEngine::get_score() |
| jamesheavey | 91:c01a736fb0d9 | 460 | { |
| jamesheavey | 64:c3426b417ad9 | 461 | return _score; |
| jamesheavey | 62:64559062e0ec | 462 | } |
| jamesheavey | 93:18f81996ea89 | 463 | |
| jamesheavey | 91:c01a736fb0d9 | 464 | void BreakoutEngine::inc_mult() |
| jamesheavey | 91:c01a736fb0d9 | 465 | { |
| jamesheavey | 62:64559062e0ec | 466 | _multiplier ++; |
| jamesheavey | 62:64559062e0ec | 467 | } |
| jamesheavey | 93:18f81996ea89 | 468 | |
| jamesheavey | 99:d8f1570faa05 | 469 | int BreakoutEngine::get_mult() |
| jamesheavey | 91:c01a736fb0d9 | 470 | { |
| jamesheavey | 99:d8f1570faa05 | 471 | return _multiplier; |
| jamesheavey | 99:d8f1570faa05 | 472 | } |
| jamesheavey | 99:d8f1570faa05 | 473 | |
| jamesheavey | 99:d8f1570faa05 | 474 | void BreakoutEngine::set_mult_one() |
| jamesheavey | 99:d8f1570faa05 | 475 | { |
| jamesheavey | 99:d8f1570faa05 | 476 | _multiplier = 1; |
| jamesheavey | 62:64559062e0ec | 477 | } |
| jamesheavey | 93:18f81996ea89 | 478 | |
| jamesheavey | 92:2e6d88e44f56 | 479 | void BreakoutEngine::inc_index() |
| jamesheavey | 92:2e6d88e44f56 | 480 | { |
| jamesheavey | 92:2e6d88e44f56 | 481 | _index ++; |
| jamesheavey | 92:2e6d88e44f56 | 482 | } |
| jamesheavey | 93:18f81996ea89 | 483 | |
| jamesheavey | 92:2e6d88e44f56 | 484 | void BreakoutEngine::reset_index() |
| jamesheavey | 92:2e6d88e44f56 | 485 | { |
| jamesheavey | 92:2e6d88e44f56 | 486 | _index = 0; |
| jamesheavey | 92:2e6d88e44f56 | 487 | } |