FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Fri Apr 26 16:26:58 2019 +0000
Revision:
46:810b3a7fc387
Parent:
45:b1ac80481d4f
Child:
47:1d1a827be81b
cooldown calibrated

Who changed what in which revision?

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