ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17c2w

Dependencies:   mbed

Committer:
Mousky
Date:
Wed May 27 07:33:59 2020 +0000
Revision:
13:76bac89c447b
Parent:
12:2af7b4868033
Final version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mousky 12:2af7b4868033 1 #include "Engine.h"
eencae 5:3c9407e2fe55 2
Mousky 12:2af7b4868033 3 Engine::Engine()
eencae 5:3c9407e2fe55 4 {
eencae 5:3c9407e2fe55 5 }
eencae 5:3c9407e2fe55 6
Mousky 12:2af7b4868033 7 Engine::~Engine()
eencae 5:3c9407e2fe55 8 {
eencae 5:3c9407e2fe55 9 }
eencae 5:3c9407e2fe55 10
Mousky 12:2af7b4868033 11 void Engine::init(int paddle_width,int paddle_height,int ball_size,int ball2_size,int speed,int thing_size, int repaddle_size)
eencae 5:3c9407e2fe55 12 {
eencae 5:3c9407e2fe55 13 // initialise the game parameters
eencae 5:3c9407e2fe55 14 _paddle_width = paddle_width;
eencae 5:3c9407e2fe55 15 _paddle_height = paddle_height;
eencae 5:3c9407e2fe55 16 _ball_size = ball_size;
Mousky 12:2af7b4868033 17 _ball2_size = ball2_size; // add another ball when score reaches 5
Mousky 12:2af7b4868033 18 _thing_size = thing_size;
Mousky 12:2af7b4868033 19 _repaddle_size = repaddle_size;
eencae 5:3c9407e2fe55 20 _speed = speed;
Mousky 12:2af7b4868033 21
eencae 5:3c9407e2fe55 22 // x position on screen - WIDTH is defined in N5110.h
Mousky 12:2af7b4868033 23 _p1y = HEIGHT - GAP;
eencae 5:3c9407e2fe55 24 // puts paddles and ball in middle
Mousky 12:2af7b4868033 25 _p1.init(HEIGHT/2,_p1y,_paddle_width);
Mousky 12:2af7b4868033 26
eencae 5:3c9407e2fe55 27 _ball.init(_ball_size,_speed);
Mousky 12:2af7b4868033 28 _ball2.init(_ball2_size,_speed);
Mousky 12:2af7b4868033 29 _thing.init(_thing_size);
eencae 5:3c9407e2fe55 30 }
eencae 5:3c9407e2fe55 31
Mousky 12:2af7b4868033 32 void Engine::read_input(Gamepad &pad)
eencae 5:3c9407e2fe55 33 {
eencae 5:3c9407e2fe55 34 _d = pad.get_direction();
eencae 5:3c9407e2fe55 35 _mag = pad.get_mag();
eencae 5:3c9407e2fe55 36 }
eencae 5:3c9407e2fe55 37
Mousky 12:2af7b4868033 38 /////////////////////// Draw the graph ///////////////////////////////////////
Mousky 12:2af7b4868033 39
Mousky 12:2af7b4868033 40 void Engine::draw(N5110 &lcd)
eencae 5:3c9407e2fe55 41 {
eencae 5:3c9407e2fe55 42 // draw the elements in the LCD buffer
eencae 5:3c9407e2fe55 43 // pitch
eencae 5:3c9407e2fe55 44 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
Mousky 12:2af7b4868033 45 lcd.drawLine(20,30,70,30,FILL_BLACK); //the first reflection board.
Mousky 12:2af7b4868033 46 lcd.drawLine(25,22,45,22,FILL_BLACK); // the second reflection board.
Mousky 12:2af7b4868033 47 lcd.drawLine(55,25,75,25,FILL_BLACK); // the third reflection board.
Mousky 12:2af7b4868033 48
eencae 5:3c9407e2fe55 49 //score
eencae 5:3c9407e2fe55 50 print_scores(lcd);
Mousky 12:2af7b4868033 51 //draw paddles
eencae 5:3c9407e2fe55 52 _p1.draw(lcd);
Mousky 12:2af7b4868033 53 // draw ball1, thing and repaddle
eencae 5:3c9407e2fe55 54 _ball.draw(lcd);
Mousky 12:2af7b4868033 55 _ball2.draw(lcd);
Mousky 12:2af7b4868033 56 _thing.draw(lcd);
Mousky 12:2af7b4868033 57 /// _repaddle.draw(lcd);
eencae 5:3c9407e2fe55 58 }
eencae 5:3c9407e2fe55 59
Mousky 12:2af7b4868033 60 //////////////////////////////UPDATE/////////////////////////////////////////////
Mousky 12:2af7b4868033 61 void Engine::update(N5110 &lcd,Gamepad &pad)
eencae 5:3c9407e2fe55 62 {
eencae 5:3c9407e2fe55 63 check_goal(pad);
Mousky 12:2af7b4868033 64 check_ball2_goal(pad);
Mousky 12:2af7b4868033 65
eencae 5:3c9407e2fe55 66 // important to update paddles and ball before checking collisions so can
eencae 5:3c9407e2fe55 67 // correct for it before updating the display
eencae 5:3c9407e2fe55 68 _p1.update(_d,_mag);
Mousky 12:2af7b4868033 69
eencae 5:3c9407e2fe55 70 _ball.update();
Mousky 12:2af7b4868033 71 /////// check ball///////////////
eencae 5:3c9407e2fe55 72 check_wall_collision(pad);
eencae 5:3c9407e2fe55 73 check_paddle_collisions(pad);
Mousky 12:2af7b4868033 74 check_thing_collisions(pad);
Mousky 12:2af7b4868033 75 check_reflection(pad);
Mousky 12:2af7b4868033 76
Mousky 12:2af7b4868033 77 ///////////////Ball2///////////////
Mousky 12:2af7b4868033 78 _ball2.update();
Mousky 12:2af7b4868033 79 // get the scores
Mousky 12:2af7b4868033 80 int p_score = _p1.get_score();
Mousky 12:2af7b4868033 81 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 82 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 83 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 84 // if the score is bigger than 5, than add another ball
Mousky 12:2af7b4868033 85 if (p_score < 5) {
Mousky 12:2af7b4868033 86 ball2_pos.x = thing_pos.x;
Mousky 12:2af7b4868033 87 ball2_pos.y = thing_pos.y;
Mousky 12:2af7b4868033 88 ball2_velocity.x = 0;
Mousky 12:2af7b4868033 89 ball2_velocity.y = 0;
Mousky 12:2af7b4868033 90
Mousky 12:2af7b4868033 91 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 92 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 93 }
Mousky 12:2af7b4868033 94 // the another ball will begin to move in a random direction
Mousky 12:2af7b4868033 95 else if (p_score == 5) {
Mousky 12:2af7b4868033 96 ball2_velocity.x = _speed * (2*(rand() % 2) - 1);
Mousky 12:2af7b4868033 97 ball2_velocity.y = _speed * (2*(rand() % 2) - 1);
Mousky 12:2af7b4868033 98 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 99 while (_p1.get_score() == 5) {
Mousky 12:2af7b4868033 100 read_input(pad);
Mousky 12:2af7b4868033 101 check_goal(pad);
Mousky 12:2af7b4868033 102 check_ball2_goal(pad);
Mousky 12:2af7b4868033 103 _p1.update(_d,_mag);
Mousky 12:2af7b4868033 104 _ball.update();
Mousky 12:2af7b4868033 105 check_wall_collision(pad);
Mousky 12:2af7b4868033 106 check_paddle_collisions(pad);
Mousky 12:2af7b4868033 107 check_thing_collisions(pad);
Mousky 12:2af7b4868033 108 check_reflection(pad);
Mousky 12:2af7b4868033 109 _ball2.update();
Mousky 12:2af7b4868033 110 check_ball2(pad);
Mousky 12:2af7b4868033 111 lcd.clear();
Mousky 12:2af7b4868033 112 draw(lcd);
Mousky 12:2af7b4868033 113 lcd.refresh();
Mousky 12:2af7b4868033 114 wait(1.0f/8);
Mousky 12:2af7b4868033 115 }
Mousky 12:2af7b4868033 116 }
Mousky 12:2af7b4868033 117 check_ball2(pad); // check the states of ball2 for updating
eencae 5:3c9407e2fe55 118 }
eencae 5:3c9407e2fe55 119
Mousky 12:2af7b4868033 120 ////////////////////////////////////////////////////////////////////////////////
Mousky 12:2af7b4868033 121
Mousky 12:2af7b4868033 122 /////////////////////////////CHECK BALL1////////////////////////////////////////
Mousky 12:2af7b4868033 123
Mousky 12:2af7b4868033 124 // set the refrection of the board and speed up the ball
Mousky 12:2af7b4868033 125 // two ways of refrection: x-direction and y-direction
Mousky 12:2af7b4868033 126 void Engine::check_reflection(Gamepad &pad)
Mousky 12:2af7b4868033 127 {
Mousky 12:2af7b4868033 128 Vector2D ball_pos = _ball.get_pos();
Mousky 12:2af7b4868033 129 Vector2D ball_velocity = _ball.get_velocity();
Mousky 12:2af7b4868033 130 if ((ball_pos.x >= 20) && (ball_pos.x <= 70) && (ball_pos.y <= 31) && (ball_pos.y >= 29)) {
Mousky 12:2af7b4868033 131 ball_velocity.x = ball_velocity.x*2;
Mousky 12:2af7b4868033 132 // audio feedback
Mousky 12:2af7b4868033 133 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 134 }
Mousky 12:2af7b4868033 135 else if ((ball_pos.x >=25) && (ball_pos.x <= 45) && (ball_pos.y >= 21) && (ball_pos.y <= 23)){
Mousky 12:2af7b4868033 136 ball_velocity.y = ball_velocity.y*2;
Mousky 12:2af7b4868033 137 // audio feedback
Mousky 12:2af7b4868033 138 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 139 }
Mousky 12:2af7b4868033 140 else if ((ball_pos.x >=55) && (ball_pos.x <= 75) && (ball_pos.y >= 24) && (ball_pos.y <= 26)){
Mousky 12:2af7b4868033 141 ball_velocity.y = ball_velocity.y*2;
Mousky 12:2af7b4868033 142 // audio feedback
Mousky 12:2af7b4868033 143 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 144 }
Mousky 12:2af7b4868033 145 _ball.set_velocity(ball_velocity);
Mousky 12:2af7b4868033 146 _ball.set_pos(ball_pos);
Mousky 12:2af7b4868033 147 }
Mousky 12:2af7b4868033 148
Mousky 12:2af7b4868033 149 void Engine::check_wall_collision(Gamepad &pad)
eencae 5:3c9407e2fe55 150 {
eencae 5:3c9407e2fe55 151 // read current ball attributes
eencae 5:3c9407e2fe55 152 Vector2D ball_pos = _ball.get_pos();
eencae 5:3c9407e2fe55 153 Vector2D ball_velocity = _ball.get_velocity();
Mousky 12:2af7b4868033 154
Mousky 12:2af7b4868033 155 //adjust to the original speed
Mousky 12:2af7b4868033 156 if (abs(ball_velocity.x) > _speed) {
Mousky 12:2af7b4868033 157 ball_velocity.x = ball_velocity.x / abs(ball_velocity.x) * 2;
Mousky 12:2af7b4868033 158 }
Mousky 12:2af7b4868033 159 if (abs(ball_velocity.y) > _speed) {
Mousky 12:2af7b4868033 160 ball_velocity.y = ball_velocity.y / abs(ball_velocity.y) * 2;
Mousky 12:2af7b4868033 161 }
eencae 5:3c9407e2fe55 162
eencae 5:3c9407e2fe55 163 // check if hit top wall
eencae 5:3c9407e2fe55 164 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
eencae 5:3c9407e2fe55 165 ball_pos.y = 1; // bounce off ceiling without going off screen
eencae 5:3c9407e2fe55 166 ball_velocity.y = -ball_velocity.y;
eencae 5:3c9407e2fe55 167 // audio feedback
eencae 5:3c9407e2fe55 168 pad.tone(750.0,0.1);
eencae 5:3c9407e2fe55 169 }
eencae 5:3c9407e2fe55 170 // check if hit bottom wall
Mousky 12:2af7b4868033 171 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
Mousky 12:2af7b4868033 172 ball_pos.x = 1; // bounce off ceiling without going off screen
Mousky 12:2af7b4868033 173 ball_velocity.x = -ball_velocity.x;
eencae 5:3c9407e2fe55 174 // audio feedback
eencae 5:3c9407e2fe55 175 pad.tone(750.0,0.1);
eencae 5:3c9407e2fe55 176 }
Mousky 12:2af7b4868033 177 else if (ball_pos.x >= 84) { // 1 due to 1 pixel boundary
Mousky 12:2af7b4868033 178 ball_pos.x = 84; // bounce off ceiling without going off screen
Mousky 12:2af7b4868033 179 ball_velocity.x = -ball_velocity.x;
Mousky 12:2af7b4868033 180 // audio feedback
Mousky 12:2af7b4868033 181 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 182 }
eencae 5:3c9407e2fe55 183 // update ball parameters
eencae 5:3c9407e2fe55 184 _ball.set_velocity(ball_velocity);
eencae 5:3c9407e2fe55 185 _ball.set_pos(ball_pos);
eencae 5:3c9407e2fe55 186 }
eencae 5:3c9407e2fe55 187
Mousky 12:2af7b4868033 188 void Engine::check_paddle_collisions(Gamepad &pad)
eencae 5:3c9407e2fe55 189 {
eencae 5:3c9407e2fe55 190 // read current ball attributes
eencae 5:3c9407e2fe55 191 Vector2D ball_pos = _ball.get_pos();
eencae 5:3c9407e2fe55 192 Vector2D ball_velocity = _ball.get_velocity();
eencae 5:3c9407e2fe55 193 Vector2D p1_pos = _p1.get_pos();
Mousky 12:2af7b4868033 194
Mousky 12:2af7b4868033 195 //adjust to the original speed
Mousky 12:2af7b4868033 196 if (abs(ball_velocity.x) > _speed) {
Mousky 12:2af7b4868033 197 ball_velocity.x = ball_velocity.x / abs(ball_velocity.x) * 2;
Mousky 12:2af7b4868033 198 }
Mousky 12:2af7b4868033 199 if (abs(ball_velocity.y) > _speed) {
Mousky 12:2af7b4868033 200 ball_velocity.y = ball_velocity.y / abs(ball_velocity.y) * 2;
Mousky 12:2af7b4868033 201 }
Mousky 12:2af7b4868033 202
eencae 5:3c9407e2fe55 203 // see if ball has hit the paddle by checking for overlaps
eencae 5:3c9407e2fe55 204 if (
eencae 5:3c9407e2fe55 205 (ball_pos.y >= p1_pos.y) && //top
Mousky 12:2af7b4868033 206 (ball_pos.y <= p1_pos.y + 2) && //bottom
Mousky 12:2af7b4868033 207 (ball_pos.x >= p1_pos.x-4) && //left
Mousky 12:2af7b4868033 208 (ball_pos.x <= p1_pos.x + 10) //right
eencae 5:3c9407e2fe55 209 ) {
eencae 5:3c9407e2fe55 210 // if it has, fix position and reflect x velocity
Mousky 12:2af7b4868033 211 ball_pos.x = p1_pos.x ;
Mousky 12:2af7b4868033 212 ball_velocity.y = -ball_velocity.y;
eencae 5:3c9407e2fe55 213 // audio feedback
eencae 5:3c9407e2fe55 214 pad.tone(1000.0,0.1);
eencae 5:3c9407e2fe55 215 }
eencae 5:3c9407e2fe55 216 // write new attributes
eencae 5:3c9407e2fe55 217 _ball.set_velocity(ball_velocity);
eencae 5:3c9407e2fe55 218 _ball.set_pos(ball_pos);
eencae 5:3c9407e2fe55 219 }
eencae 5:3c9407e2fe55 220
Mousky 12:2af7b4868033 221 void Engine::check_thing_collisions(Gamepad &pad)
Mousky 12:2af7b4868033 222 {
Mousky 12:2af7b4868033 223 // read current ball attributes
Mousky 12:2af7b4868033 224 Vector2D ball_pos = _ball.get_pos();
Mousky 12:2af7b4868033 225 Vector2D ball_velocity = _ball.get_velocity();
Mousky 12:2af7b4868033 226 // read current thing position
Mousky 12:2af7b4868033 227 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 228
Mousky 12:2af7b4868033 229 //adjust to the original speed
Mousky 12:2af7b4868033 230 if (abs(ball_velocity.x) > _speed) {
Mousky 12:2af7b4868033 231 ball_velocity.x = ball_velocity.x / abs(ball_velocity.x) * 2;
Mousky 12:2af7b4868033 232 }
Mousky 12:2af7b4868033 233 if (abs(ball_velocity.y) > _speed) {
Mousky 12:2af7b4868033 234 ball_velocity.y = ball_velocity.y / abs(ball_velocity.y) * 2;
Mousky 12:2af7b4868033 235 }
Mousky 12:2af7b4868033 236
Mousky 12:2af7b4868033 237 // see if ball has hit the thing by checking for overlaps
Mousky 12:2af7b4868033 238 if (
Mousky 12:2af7b4868033 239 (ball_pos.y >= thing_pos.y - 1) && //top
Mousky 12:2af7b4868033 240 (ball_pos.y <= thing_pos.y + 1) && //bottom
Mousky 12:2af7b4868033 241 (ball_pos.x >= thing_pos.x - 3) && //left
Mousky 12:2af7b4868033 242 (ball_pos.x <= thing_pos.x + 10) //right
Mousky 12:2af7b4868033 243 ) {
Mousky 12:2af7b4868033 244 // if it has, fix position and reflect x velocity
Mousky 12:2af7b4868033 245 ball_pos.x = thing_pos.x ;
Mousky 12:2af7b4868033 246 ball_velocity.y = -ball_velocity.y;
Mousky 12:2af7b4868033 247 // audio feedback
Mousky 12:2af7b4868033 248 pad.tone(1000.0,0.1);
Mousky 12:2af7b4868033 249 }
Mousky 12:2af7b4868033 250 // write new attributes
Mousky 12:2af7b4868033 251 _ball.set_velocity(ball_velocity);
Mousky 12:2af7b4868033 252 _ball.set_pos(ball_pos);
Mousky 12:2af7b4868033 253 }
Mousky 12:2af7b4868033 254
Mousky 12:2af7b4868033 255 void Engine::check_goal(Gamepad &pad)
eencae 5:3c9407e2fe55 256 {
eencae 5:3c9407e2fe55 257 Vector2D ball_pos = _ball.get_pos();
Mousky 12:2af7b4868033 258 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 259 if (
Mousky 12:2af7b4868033 260 (ball_pos.y >= thing_pos.y - 1) && //top
Mousky 12:2af7b4868033 261 (ball_pos.y <= thing_pos.y + 1) && //bottom
Mousky 12:2af7b4868033 262 (ball_pos.x >= thing_pos.x - 3) && //left
Mousky 12:2af7b4868033 263 (ball_pos.x <= thing_pos.x + 10) //right
Mousky 12:2af7b4868033 264 ) {
Mousky 12:2af7b4868033 265 _p1.add_score();
Mousky 12:2af7b4868033 266 _thing.init(_thing_size);
eencae 5:3c9407e2fe55 267 pad.tone(1500.0,0.5);
eencae 5:3c9407e2fe55 268 pad.leds_on();
eencae 5:3c9407e2fe55 269 wait(0.5);
Mousky 12:2af7b4868033 270 pad.leds_off();
eencae 5:3c9407e2fe55 271 }
Mousky 12:2af7b4868033 272 if (ball_pos.y >= HEIGHT) {
eencae 5:3c9407e2fe55 273 _ball.init(_ball_size,_speed);
eencae 5:3c9407e2fe55 274 pad.tone(1500.0,0.5);
eencae 5:3c9407e2fe55 275 pad.leds_on();
eencae 5:3c9407e2fe55 276 wait(0.5);
eencae 5:3c9407e2fe55 277 pad.leds_off();
eencae 5:3c9407e2fe55 278 }
eencae 5:3c9407e2fe55 279 }
eencae 5:3c9407e2fe55 280
Mousky 12:2af7b4868033 281 void Engine::print_scores(N5110 &lcd)
eencae 5:3c9407e2fe55 282 {
eencae 5:3c9407e2fe55 283 // get scores from paddles
eencae 5:3c9407e2fe55 284 int p1_score = _p1.get_score();
eencae 5:3c9407e2fe55 285 // print to LCD i
eencae 5:3c9407e2fe55 286 char buffer1[14];
eencae 5:3c9407e2fe55 287 sprintf(buffer1,"%2d",p1_score);
Mousky 12:2af7b4868033 288 lcd.printString(buffer1,60,1); // print at top-right corner
Mousky 12:2af7b4868033 289 }
Mousky 12:2af7b4868033 290
Mousky 12:2af7b4868033 291 /////////////////////////////////////////////////////////////////////////////////
Mousky 12:2af7b4868033 292 //////////////////// Check Ball2 ////////////////////////////////////
Mousky 12:2af7b4868033 293 void Engine::check_ball2(Gamepad &pad)
Mousky 12:2af7b4868033 294 {
Mousky 12:2af7b4868033 295 check_ball2_wall_collision(pad);
Mousky 12:2af7b4868033 296 check_ball2_paddle_collisions(pad);
Mousky 12:2af7b4868033 297 check_ball2_thing_collisions(pad);
Mousky 12:2af7b4868033 298 check_ball2_reflection(pad);
Mousky 12:2af7b4868033 299 }
Mousky 12:2af7b4868033 300
Mousky 12:2af7b4868033 301
Mousky 12:2af7b4868033 302 ////////////// Check the states of the second ball /////////////////////////////
Mousky 12:2af7b4868033 303 void Engine::check_ball2_reflection(Gamepad &pad)
Mousky 12:2af7b4868033 304 {
Mousky 12:2af7b4868033 305 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 306 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 307 if ((ball2_pos.x >= 20) && (ball2_pos.x <= 70) && (ball2_pos.y >= 29)&& (ball2_pos.y <= 31)) {
Mousky 12:2af7b4868033 308 ball2_velocity.x = ball2_velocity.x*2;
Mousky 12:2af7b4868033 309 // audio feedback
Mousky 12:2af7b4868033 310 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 311 }
Mousky 12:2af7b4868033 312 else if ((ball2_pos.x >=25) && (ball2_pos.x <= 45) && (ball2_pos.y >= 21) && (ball2_pos.y <= 23)) {
Mousky 12:2af7b4868033 313 ball2_velocity.y = ball2_velocity.y*2;
Mousky 12:2af7b4868033 314 // audio feedback
Mousky 12:2af7b4868033 315 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 316 }
Mousky 12:2af7b4868033 317 else if ((ball2_pos.x >=55) && (ball2_pos.x <= 75) && (ball2_pos.y >= 24) && (ball2_pos.y <= 26)) {
Mousky 12:2af7b4868033 318 ball2_velocity.y = ball2_velocity.y*2;
Mousky 12:2af7b4868033 319 // audio feedback
Mousky 12:2af7b4868033 320 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 321 }
Mousky 12:2af7b4868033 322 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 323 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 324 }
Mousky 12:2af7b4868033 325
Mousky 12:2af7b4868033 326
Mousky 12:2af7b4868033 327 void Engine::check_ball2_wall_collision(Gamepad &pad)
Mousky 12:2af7b4868033 328 {
Mousky 12:2af7b4868033 329 // read current ball attributes
Mousky 12:2af7b4868033 330 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 331 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 332
Mousky 12:2af7b4868033 333 //adjust to the original speed
Mousky 12:2af7b4868033 334 if (abs(ball2_velocity.x) > _speed) {
Mousky 12:2af7b4868033 335 ball2_velocity.x = ball2_velocity.x / abs(ball2_velocity.x) * 2;
Mousky 12:2af7b4868033 336 }
Mousky 12:2af7b4868033 337 if (abs(ball2_velocity.y) > _speed) {
Mousky 12:2af7b4868033 338 ball2_velocity.y = ball2_velocity.y / abs(ball2_velocity.y) * 2;
Mousky 12:2af7b4868033 339 }
Mousky 12:2af7b4868033 340
Mousky 12:2af7b4868033 341 // check if hit top wall
Mousky 12:2af7b4868033 342 if (ball2_pos.y <= 1) { // 1 due to 1 pixel boundary
Mousky 12:2af7b4868033 343 ball2_pos.y = 1; // bounce off ceiling without going off screen
Mousky 12:2af7b4868033 344 ball2_velocity.y = -ball2_velocity.y;
Mousky 12:2af7b4868033 345 // audio feedback
Mousky 12:2af7b4868033 346 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 347 }
Mousky 12:2af7b4868033 348 // check if hit bottom wall
Mousky 12:2af7b4868033 349 else if (ball2_pos.x <= 1) { // 1 due to 1 pixel boundary
Mousky 12:2af7b4868033 350 ball2_pos.x = 1; // bounce off ceiling without going off screen
Mousky 12:2af7b4868033 351 ball2_velocity.x = -ball2_velocity.x;
Mousky 12:2af7b4868033 352 // audio feedback
Mousky 12:2af7b4868033 353 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 354 }
Mousky 12:2af7b4868033 355 else if (ball2_pos.x >= 84) { // 1 due to 1 pixel boundary
Mousky 12:2af7b4868033 356 ball2_pos.x = 84; // bounce off ceiling without going off screen
Mousky 12:2af7b4868033 357 ball2_velocity.x = -ball2_velocity.x;
Mousky 12:2af7b4868033 358 // audio feedback
Mousky 12:2af7b4868033 359 pad.tone(750.0,0.1);
Mousky 12:2af7b4868033 360 }
Mousky 12:2af7b4868033 361 // update ball parameters
Mousky 12:2af7b4868033 362 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 363 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 364 }
Mousky 12:2af7b4868033 365
Mousky 12:2af7b4868033 366 void Engine::check_ball2_paddle_collisions(Gamepad &pad)
Mousky 12:2af7b4868033 367 {
Mousky 12:2af7b4868033 368 // read current ball attributes
Mousky 12:2af7b4868033 369 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 370 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 371 Vector2D p1_pos = _p1.get_pos();
Mousky 12:2af7b4868033 372
Mousky 12:2af7b4868033 373 //adjust to the original speed
Mousky 12:2af7b4868033 374 if (abs(ball2_velocity.x) > _speed) {
Mousky 12:2af7b4868033 375 ball2_velocity.x = ball2_velocity.x / abs(ball2_velocity.x) * 2;
Mousky 12:2af7b4868033 376 }
Mousky 12:2af7b4868033 377 if (abs(ball2_velocity.y) > _speed) {
Mousky 12:2af7b4868033 378 ball2_velocity.y = ball2_velocity.y / abs(ball2_velocity.y) * 2;
Mousky 12:2af7b4868033 379 }
Mousky 12:2af7b4868033 380
Mousky 12:2af7b4868033 381 // see if ball has hit the paddle by checking for overlaps
Mousky 12:2af7b4868033 382 if (
Mousky 12:2af7b4868033 383 (ball2_pos.y >= p1_pos.y) && //top
Mousky 12:2af7b4868033 384 (ball2_pos.y <= p1_pos.y + 2) && //bottom
Mousky 12:2af7b4868033 385 (ball2_pos.x >= p1_pos.x-4) && //left
Mousky 12:2af7b4868033 386 (ball2_pos.x <= p1_pos.x + 10) //right
Mousky 12:2af7b4868033 387 ) {
Mousky 12:2af7b4868033 388 // if it has, fix position and reflect x velocity
Mousky 12:2af7b4868033 389 ball2_pos.x = p1_pos.x ;
Mousky 12:2af7b4868033 390 ball2_velocity.y = -ball2_velocity.y;
Mousky 12:2af7b4868033 391 // audio feedback
Mousky 12:2af7b4868033 392 pad.tone(1000.0,0.1);
Mousky 12:2af7b4868033 393 }
Mousky 12:2af7b4868033 394 // write new attributes
Mousky 12:2af7b4868033 395 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 396 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 397 }
Mousky 12:2af7b4868033 398
Mousky 12:2af7b4868033 399 void Engine::check_ball2_thing_collisions(Gamepad &pad)
Mousky 12:2af7b4868033 400 {
Mousky 12:2af7b4868033 401 // read current ball attributes
Mousky 12:2af7b4868033 402 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 403 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 404 // read current thing position
Mousky 12:2af7b4868033 405 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 406
Mousky 12:2af7b4868033 407 //adjust to the original speed
Mousky 12:2af7b4868033 408 if (abs(ball2_velocity.x) > _speed) {
Mousky 12:2af7b4868033 409 ball2_velocity.x = ball2_velocity.x / abs(ball2_velocity.x) * _speed;
Mousky 12:2af7b4868033 410 }
Mousky 12:2af7b4868033 411 if (abs(ball2_velocity.y) > _speed) {
Mousky 12:2af7b4868033 412 ball2_velocity.y = ball2_velocity.y / abs(ball2_velocity.y) * _speed;
Mousky 12:2af7b4868033 413 }
Mousky 12:2af7b4868033 414
Mousky 12:2af7b4868033 415 // see if ball has hit the thing by checking for overlaps
Mousky 12:2af7b4868033 416 if (
Mousky 12:2af7b4868033 417 (ball2_pos.y >= thing_pos.y - 1) && //top
Mousky 12:2af7b4868033 418 (ball2_pos.y <= thing_pos.y + 1) && //bottom
Mousky 12:2af7b4868033 419 (ball2_pos.x >= thing_pos.x - 3) && //left
Mousky 12:2af7b4868033 420 (ball2_pos.x <= thing_pos.x + 10) //right
Mousky 12:2af7b4868033 421 ) {
Mousky 12:2af7b4868033 422 // if it has, fix position and reflect x velocity
Mousky 12:2af7b4868033 423 ball2_pos.x = thing_pos.x ;
Mousky 12:2af7b4868033 424 ball2_velocity.y = -ball2_velocity.y;
Mousky 12:2af7b4868033 425 // audio feedback
Mousky 12:2af7b4868033 426 pad.tone(1000.0,0.1);
Mousky 12:2af7b4868033 427 }
Mousky 12:2af7b4868033 428 // write new attributes
Mousky 12:2af7b4868033 429 _ball2.set_velocity(ball2_velocity);
Mousky 12:2af7b4868033 430 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 431 }
Mousky 12:2af7b4868033 432
Mousky 12:2af7b4868033 433 void Engine::check_ball2_goal(Gamepad &pad)
Mousky 12:2af7b4868033 434 {
Mousky 12:2af7b4868033 435 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 436 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 437 if (
Mousky 12:2af7b4868033 438 (ball2_pos.y >= thing_pos.y - 1) && //top
Mousky 12:2af7b4868033 439 (ball2_pos.y <= thing_pos.y + 1) && //bottom
Mousky 12:2af7b4868033 440 (ball2_pos.x >= thing_pos.x - 3) && //left
Mousky 12:2af7b4868033 441 (ball2_pos.x <= thing_pos.x + 10) //right
Mousky 12:2af7b4868033 442 ) {
Mousky 12:2af7b4868033 443 _p1.add_score();
Mousky 12:2af7b4868033 444 _thing.init(_thing_size);
Mousky 12:2af7b4868033 445 pad.tone(1500.0,0.5);
Mousky 12:2af7b4868033 446 pad.leds_on();
Mousky 12:2af7b4868033 447 wait(0.5);
Mousky 12:2af7b4868033 448 pad.leds_off();
Mousky 12:2af7b4868033 449 }
Mousky 12:2af7b4868033 450 if (ball2_pos.y >= HEIGHT) {
Mousky 12:2af7b4868033 451 _ball2.init(_ball2_size,_speed);
Mousky 12:2af7b4868033 452 pad.tone(1500.0,0.5);
Mousky 12:2af7b4868033 453 pad.leds_on();
Mousky 12:2af7b4868033 454 wait(0.5);
Mousky 12:2af7b4868033 455 pad.leds_off();
Mousky 12:2af7b4868033 456 }
Mousky 12:2af7b4868033 457 }
Mousky 12:2af7b4868033 458
Mousky 12:2af7b4868033 459
Mousky 12:2af7b4868033 460 ////////////////////// The World //////////////////////////////////
Mousky 12:2af7b4868033 461 /* Every time we get 6 socres , the stand power of the world is triggered
Mousky 12:2af7b4868033 462 * The function of the world is used to pause the first ball for a while
Mousky 12:2af7b4868033 463 */
Mousky 12:2af7b4868033 464
Mousky 12:2af7b4868033 465
Mousky 12:2af7b4868033 466 void Engine::The_world(N5110 &lcd, Gamepad &pad){
Mousky 12:2af7b4868033 467
Mousky 12:2af7b4868033 468 Vector2D ball_pos = _ball.get_pos();
Mousky 12:2af7b4868033 469 Vector2D ball_velocity = _ball.get_velocity();
Mousky 12:2af7b4868033 470 // check the score whether reach the goal
Mousky 12:2af7b4868033 471 while ((_p1.get_score() % 6 == 0) && (_p1.get_score() != 0)){
Mousky 12:2af7b4868033 472 //set the sound of the world
Mousky 12:2af7b4868033 473 pad.tone(500.0,0.2);
Mousky 12:2af7b4868033 474 wait(0.2);
Mousky 12:2af7b4868033 475 pad.tone(800.0,0.2);
Mousky 12:2af7b4868033 476 pad.tone(1300.0,0.3);
Mousky 12:2af7b4868033 477 pad.tone(1600.0,0.3);
Mousky 12:2af7b4868033 478
Mousky 12:2af7b4868033 479 // get the state of ball 1 before begin the world
Mousky 12:2af7b4868033 480 // make the ball 2 keep moving
Mousky 12:2af7b4868033 481 while((_p1.get_score() % 6 == 0) && (_p1.get_score() != 0)){
Mousky 12:2af7b4868033 482 read_input(pad);
Mousky 12:2af7b4868033 483 check_ball2_goal(pad);
Mousky 12:2af7b4868033 484 _p1.update(_d,_mag);
Mousky 12:2af7b4868033 485 _ball2.update();
Mousky 12:2af7b4868033 486 check_ball2(pad);
Mousky 12:2af7b4868033 487
Mousky 12:2af7b4868033 488 // reflesh the lcd and make pause
Mousky 12:2af7b4868033 489 lcd.clear();
Mousky 12:2af7b4868033 490 draw(lcd);
Mousky 12:2af7b4868033 491 lcd.refresh();
Mousky 12:2af7b4868033 492 wait(1.0f/8);
Mousky 12:2af7b4868033 493
Mousky 12:2af7b4868033 494 }
Mousky 12:2af7b4868033 495
Mousky 12:2af7b4868033 496 // restore the state of ball 1 after the world
Mousky 12:2af7b4868033 497 _ball.set_velocity(ball_velocity);
Mousky 12:2af7b4868033 498 }
Mousky 12:2af7b4868033 499 }
Mousky 12:2af7b4868033 500
Mousky 12:2af7b4868033 501
Mousky 12:2af7b4868033 502 ////////////////////// Bite the Dust /////////////////////////////////
Mousky 12:2af7b4868033 503 /* When the socre we get reaches 8, the stand power of bite the dust is triggered
Mousky 12:2af7b4868033 504 * The function of bite the dust is time backing tracking
Mousky 12:2af7b4868033 505 */// The second ball will go back to the initial state
Mousky 12:2af7b4868033 506
Mousky 12:2af7b4868033 507 void Engine::bite_dust(N5110 &lcd, Gamepad &pad){
Mousky 12:2af7b4868033 508 // check the score whether reach the goal
Mousky 12:2af7b4868033 509 while ((_p1.get_score() % 8 == 0) && (_p1.get_score() != 0)){
Mousky 12:2af7b4868033 510 //set the sound of the world
Mousky 12:2af7b4868033 511 pad.tone(1000.0,0.2);
Mousky 12:2af7b4868033 512 wait(0.1);
Mousky 12:2af7b4868033 513 pad.tone(1000.0,0.2);
Mousky 12:2af7b4868033 514 wait(0.1);
Mousky 12:2af7b4868033 515 pad.tone(600.0,0.3);
Mousky 12:2af7b4868033 516
Mousky 12:2af7b4868033 517 // get the state of ball 2
Mousky 12:2af7b4868033 518 Vector2D ball2_pos = _ball2.get_pos();
Mousky 12:2af7b4868033 519 Vector2D ball2_velocity = _ball2.get_velocity();
Mousky 12:2af7b4868033 520 Vector2D thing_pos = _thing.get_pos();
Mousky 12:2af7b4868033 521
Mousky 12:2af7b4868033 522
Mousky 12:2af7b4868033 523 int distance_x = floor((ball2_pos.x - thing_pos.x)/3.0);
Mousky 12:2af7b4868033 524 int distance_y = floor((ball2_pos.y - thing_pos.y)/3.0);
Mousky 12:2af7b4868033 525 for (int i = 1; i <= 3; i ++){
Mousky 12:2af7b4868033 526 ball2_pos.x = ball2_pos.x - distance_x;
Mousky 12:2af7b4868033 527 ball2_pos.y = ball2_pos.y - distance_y;
Mousky 12:2af7b4868033 528 _ball2.set_pos(ball2_pos);
Mousky 12:2af7b4868033 529
Mousky 12:2af7b4868033 530 // reflesh the lcd and make pause
Mousky 12:2af7b4868033 531 lcd.clear();
Mousky 12:2af7b4868033 532 draw(lcd);
Mousky 12:2af7b4868033 533 lcd.refresh();
Mousky 12:2af7b4868033 534 wait(0.5);
Mousky 12:2af7b4868033 535 }
Mousky 12:2af7b4868033 536 while((_p1.get_score() % 8 == 0) && (_p1.get_score() != 0)){
Mousky 12:2af7b4868033 537 int p_score_b = _p1.get_score();
Mousky 12:2af7b4868033 538 read_input(pad);
Mousky 12:2af7b4868033 539 update(lcd,pad);
Mousky 12:2af7b4868033 540 lcd.clear();
Mousky 12:2af7b4868033 541 draw(lcd);
Mousky 12:2af7b4868033 542 lcd.refresh();
Mousky 12:2af7b4868033 543 wait(1.0f/8);
Mousky 12:2af7b4868033 544 }
Mousky 12:2af7b4868033 545 }
eencae 5:3c9407e2fe55 546 }