Simple Breakout game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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