James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Mon May 06 13:02:03 2019 +0000
Revision:
75:d96b177585aa
Parent:
72:7254d2a8a1cd
Child:
76:52410264a72d
pre ball init over paddle cp

Who changed what in which revision?

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