James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Sun Apr 21 20:44:24 2019 +0000
Revision:
9:f6f0f39538c7
Parent:
8:1ab6d90c4d60
Child:
10:9f445faa892c
added lives leds, need to make a lose screen when lives = 0 and need to sort collision detection on brick

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 #include "PongEngine.h"
jamesheavey 0:7d4d2023ed9c 2
jamesheavey 7:ef162a6232ca 3
jamesheavey 7:ef162a6232ca 4
jamesheavey 0:7d4d2023ed9c 5 PongEngine::PongEngine()
jamesheavey 0:7d4d2023ed9c 6 {
jamesheavey 0:7d4d2023ed9c 7
jamesheavey 0:7d4d2023ed9c 8 }
jamesheavey 0:7d4d2023ed9c 9
jamesheavey 0:7d4d2023ed9c 10 PongEngine::~PongEngine()
jamesheavey 0:7d4d2023ed9c 11 {
jamesheavey 0:7d4d2023ed9c 12
jamesheavey 0:7d4d2023ed9c 13 }
jamesheavey 0:7d4d2023ed9c 14
jamesheavey 0:7d4d2023ed9c 15 void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
jamesheavey 0:7d4d2023ed9c 16 {
jamesheavey 0:7d4d2023ed9c 17 // initialise the game parameters
jamesheavey 0:7d4d2023ed9c 18 _paddle_width = paddle_width;
jamesheavey 0:7d4d2023ed9c 19 _paddle_height = paddle_height;
jamesheavey 0:7d4d2023ed9c 20 _ball_size = ball_size;
jamesheavey 0:7d4d2023ed9c 21 _speed = speed;
jamesheavey 0:7d4d2023ed9c 22
jamesheavey 1:e18615d46071 23 // y position on screen - WIDTH is defined in N5110.h
jamesheavey 1:e18615d46071 24 _p1y = HEIGHT - GAP;
jamesheavey 0:7d4d2023ed9c 25
jamesheavey 0:7d4d2023ed9c 26 // puts paddles and ball in middle
jamesheavey 1:e18615d46071 27 _p1.init(_p1y,_paddle_height,_paddle_width);
jamesheavey 0:7d4d2023ed9c 28 _ball.init(_ball_size,_speed);
jamesheavey 2:b3ca50f2e85d 29
jamesheavey 7:ef162a6232ca 30 _brick11.init(3,GAP+1,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
jamesheavey 7:ef162a6232ca 31 _brick12.init(16,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 32 _brick13.init(29,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 33 _brick14.init(42,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 34 _brick15.init(55,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 35 _brick16.init(68,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 36
jamesheavey 7:ef162a6232ca 37 _brick21.init(3,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
jamesheavey 7:ef162a6232ca 38 _brick22.init(16,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 39 _brick23.init(29,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 40 _brick24.init(42,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 41 _brick25.init(55,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 42 _brick26.init(68,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 43
jamesheavey 7:ef162a6232ca 44 _brick31.init(3,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
jamesheavey 7:ef162a6232ca 45 _brick32.init(16,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 46 _brick33.init(29,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 47 _brick34.init(42,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 48 _brick35.init(55,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 49 _brick36.init(68,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 7:ef162a6232ca 50
jamesheavey 7:ef162a6232ca 51 listofBricks.push_back(_brick11);
jamesheavey 7:ef162a6232ca 52 listofBricks.push_back(_brick12);
jamesheavey 7:ef162a6232ca 53 listofBricks.push_back(_brick13);
jamesheavey 7:ef162a6232ca 54 listofBricks.push_back(_brick14);
jamesheavey 7:ef162a6232ca 55 listofBricks.push_back(_brick15);
jamesheavey 7:ef162a6232ca 56 listofBricks.push_back(_brick16);
jamesheavey 7:ef162a6232ca 57
jamesheavey 7:ef162a6232ca 58 listofBricks.push_back(_brick21);
jamesheavey 7:ef162a6232ca 59 listofBricks.push_back(_brick22);
jamesheavey 7:ef162a6232ca 60 listofBricks.push_back(_brick23);
jamesheavey 7:ef162a6232ca 61 listofBricks.push_back(_brick24);
jamesheavey 7:ef162a6232ca 62 listofBricks.push_back(_brick25);
jamesheavey 7:ef162a6232ca 63 listofBricks.push_back(_brick26);
jamesheavey 7:ef162a6232ca 64
jamesheavey 7:ef162a6232ca 65 listofBricks.push_back(_brick31);
jamesheavey 7:ef162a6232ca 66 listofBricks.push_back(_brick32);
jamesheavey 7:ef162a6232ca 67 listofBricks.push_back(_brick33);
jamesheavey 7:ef162a6232ca 68 listofBricks.push_back(_brick34);
jamesheavey 7:ef162a6232ca 69 listofBricks.push_back(_brick35);
jamesheavey 7:ef162a6232ca 70 listofBricks.push_back(_brick36);
jamesheavey 0:7d4d2023ed9c 71 }
jamesheavey 0:7d4d2023ed9c 72
jamesheavey 0:7d4d2023ed9c 73 void PongEngine::read_input(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 74 {
jamesheavey 0:7d4d2023ed9c 75 _d = pad.get_direction();
jamesheavey 0:7d4d2023ed9c 76 _mag = pad.get_mag();
jamesheavey 2:b3ca50f2e85d 77 //_pause = pad.check_event(Gamepad::START_PRESSED); // == false then system("PAUSE") or cin.get()
jamesheavey 0:7d4d2023ed9c 78 }
jamesheavey 0:7d4d2023ed9c 79
jamesheavey 0:7d4d2023ed9c 80 void PongEngine::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 81 {
jamesheavey 0:7d4d2023ed9c 82 // draw the elements in the LCD buffer
jamesheavey 0:7d4d2023ed9c 83 // pitch
jamesheavey 0:7d4d2023ed9c 84 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
jamesheavey 0:7d4d2023ed9c 85 //score
jamesheavey 0:7d4d2023ed9c 86 print_scores(lcd);
jamesheavey 0:7d4d2023ed9c 87 // paddles
jamesheavey 0:7d4d2023ed9c 88 _p1.draw(lcd);
jamesheavey 0:7d4d2023ed9c 89 // ball
jamesheavey 0:7d4d2023ed9c 90 _ball.draw(lcd);
jamesheavey 2:b3ca50f2e85d 91
jamesheavey 7:ef162a6232ca 92 _brick11.draw(lcd);
jamesheavey 7:ef162a6232ca 93 _brick12.draw(lcd);
jamesheavey 7:ef162a6232ca 94 _brick13.draw(lcd);
jamesheavey 7:ef162a6232ca 95 _brick14.draw(lcd);
jamesheavey 7:ef162a6232ca 96 _brick15.draw(lcd);
jamesheavey 7:ef162a6232ca 97 _brick16.draw(lcd);
jamesheavey 7:ef162a6232ca 98
jamesheavey 7:ef162a6232ca 99 _brick21.draw(lcd);
jamesheavey 7:ef162a6232ca 100 _brick22.draw(lcd);
jamesheavey 7:ef162a6232ca 101 _brick23.draw(lcd);
jamesheavey 7:ef162a6232ca 102 _brick24.draw(lcd);
jamesheavey 7:ef162a6232ca 103 _brick25.draw(lcd);
jamesheavey 7:ef162a6232ca 104 _brick26.draw(lcd);
jamesheavey 7:ef162a6232ca 105
jamesheavey 7:ef162a6232ca 106 _brick31.draw(lcd);
jamesheavey 7:ef162a6232ca 107 _brick32.draw(lcd);
jamesheavey 7:ef162a6232ca 108 _brick33.draw(lcd);
jamesheavey 7:ef162a6232ca 109 _brick34.draw(lcd);
jamesheavey 7:ef162a6232ca 110 _brick35.draw(lcd);
jamesheavey 7:ef162a6232ca 111 _brick36.draw(lcd);
jamesheavey 7:ef162a6232ca 112
jamesheavey 7:ef162a6232ca 113
jamesheavey 7:ef162a6232ca 114
jamesheavey 7:ef162a6232ca 115 // for (it = listofBricks.begin(); it != listofBricks.end(); it++)
jamesheavey 7:ef162a6232ca 116 // {
jamesheavey 7:ef162a6232ca 117 // int x = it->_x;
jamesheavey 7:ef162a6232ca 118 // int y = it->_y;
jamesheavey 7:ef162a6232ca 119
jamesheavey 7:ef162a6232ca 120 // lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT);
jamesheavey 7:ef162a6232ca 121 // }
jamesheavey 6:7f79f320b827 122
jamesheavey 2:b3ca50f2e85d 123
jamesheavey 2:b3ca50f2e85d 124
jamesheavey 2:b3ca50f2e85d 125
jamesheavey 2:b3ca50f2e85d 126
jamesheavey 2:b3ca50f2e85d 127 // for(int i = GAP+1; i <= WIDTH; i+= WIDTH_BRICK + 1){
jamesheavey 7:ef162a6232ca 128 // for(int j = GAP+1; j <= (HEIGHT_BRICK + 1)*3; j+= HEIGHT_BRICK + 1){
jamesheavey 7:ef162a6232ca 129 // Vector2D _grid[i][j]= _brick.init(i, j,WIDTH_BRICK,HEIGHT_BRICK);
jamesheavey 2:b3ca50f2e85d 130 // }
jamesheavey 2:b3ca50f2e85d 131 // }
jamesheavey 0:7d4d2023ed9c 132 }
jamesheavey 0:7d4d2023ed9c 133
jamesheavey 2:b3ca50f2e85d 134
jamesheavey 0:7d4d2023ed9c 135 void PongEngine::update(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 136 {
jamesheavey 0:7d4d2023ed9c 137 check_goal(pad);
jamesheavey 0:7d4d2023ed9c 138 // important to update paddles and ball before checking collisions so can
jamesheavey 0:7d4d2023ed9c 139 // correct for it before updating the display
jamesheavey 0:7d4d2023ed9c 140 _p1.update(_d,_mag);
jamesheavey 0:7d4d2023ed9c 141 _ball.update();
jamesheavey 9:f6f0f39538c7 142
jamesheavey 9:f6f0f39538c7 143 lives_leds(pad);
jamesheavey 9:f6f0f39538c7 144
jamesheavey 0:7d4d2023ed9c 145 check_wall_collision(pad);
jamesheavey 0:7d4d2023ed9c 146 check_paddle_collisions(pad);
jamesheavey 4:6bd488b5b31a 147 check_brick_collisions(pad);
jamesheavey 0:7d4d2023ed9c 148 }
jamesheavey 0:7d4d2023ed9c 149
jamesheavey 9:f6f0f39538c7 150 void PongEngine::lives_leds(Gamepad &pad)
jamesheavey 9:f6f0f39538c7 151 {
jamesheavey 9:f6f0f39538c7 152 if (_p1.get_lives() == 0) {
jamesheavey 9:f6f0f39538c7 153 pad.leds_off();
jamesheavey 9:f6f0f39538c7 154 }
jamesheavey 9:f6f0f39538c7 155
jamesheavey 9:f6f0f39538c7 156 //else if (_p1.get_lives() == 1) {
jamesheavey 9:f6f0f39538c7 157 //turn leftmost led on
jamesheavey 9:f6f0f39538c7 158 //}
jamesheavey 9:f6f0f39538c7 159
jamesheavey 9:f6f0f39538c7 160 else if (_p1.get_lives() == 6) {
jamesheavey 9:f6f0f39538c7 161 pad.leds_off();
jamesheavey 9:f6f0f39538c7 162 }
jamesheavey 9:f6f0f39538c7 163 }
jamesheavey 9:f6f0f39538c7 164
jamesheavey 0:7d4d2023ed9c 165 void PongEngine::check_wall_collision(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 166 {
jamesheavey 0:7d4d2023ed9c 167 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 168 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 169 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 170
jamesheavey 0:7d4d2023ed9c 171 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 172 ball_pos.y = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 173 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 174 // audio feedback
jamesheavey 0:7d4d2023ed9c 175 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 176 }
jamesheavey 0:7d4d2023ed9c 177
jamesheavey 0:7d4d2023ed9c 178 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 179 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 180 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 181 // audio feedback
jamesheavey 0:7d4d2023ed9c 182 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 183 }
jamesheavey 0:7d4d2023ed9c 184
jamesheavey 0:7d4d2023ed9c 185 // check if hit bottom wall
jamesheavey 0:7d4d2023ed9c 186 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 0:7d4d2023ed9c 187 // hit bottom
jamesheavey 0:7d4d2023ed9c 188 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 0:7d4d2023ed9c 189 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 190 // audio feedback
jamesheavey 0:7d4d2023ed9c 191 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 192 }
jamesheavey 0:7d4d2023ed9c 193
jamesheavey 0:7d4d2023ed9c 194 // update ball parameters
jamesheavey 0:7d4d2023ed9c 195 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 196 _ball.set_pos(ball_pos);
jamesheavey 4:6bd488b5b31a 197
jamesheavey 0:7d4d2023ed9c 198 }
jamesheavey 0:7d4d2023ed9c 199
jamesheavey 0:7d4d2023ed9c 200 void PongEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 201 {
jamesheavey 0:7d4d2023ed9c 202 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 203 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 204 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 205
jamesheavey 0:7d4d2023ed9c 206 // check p1 first
jamesheavey 0:7d4d2023ed9c 207 Vector2D p1_pos = _p1.get_pos();
jamesheavey 0:7d4d2023ed9c 208
jamesheavey 0:7d4d2023ed9c 209 // see if ball has hit the paddle by checking for overlaps
jamesheavey 0:7d4d2023ed9c 210 if (
jamesheavey 9:f6f0f39538c7 211 (ball_pos.x >= p1_pos.x) && //left
jamesheavey 4:6bd488b5b31a 212 (ball_pos.x <= p1_pos.x + _paddle_width) && //right
jamesheavey 4:6bd488b5b31a 213 (ball_pos.y >= _p1y) && //bottom
jamesheavey 4:6bd488b5b31a 214 (ball_pos.y <= _p1y + _paddle_height) //top
jamesheavey 4:6bd488b5b31a 215 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 4:6bd488b5b31a 216 // if it has, fix position and reflect x velocity
jamesheavey 0:7d4d2023ed9c 217 pad.tone(1000.0,0.1);
jamesheavey 9:f6f0f39538c7 218 ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 9:f6f0f39538c7 219 ball_velocity.y = -ball_velocity.y;
jamesheavey 8:1ab6d90c4d60 220
jamesheavey 9:f6f0f39538c7 221 // 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 9:f6f0f39538c7 222 // ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 9:f6f0f39538c7 223 // ball_velocity.y = -ball_velocity.y;
jamesheavey 9:f6f0f39538c7 224 // }
jamesheavey 9:f6f0f39538c7 225 // else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 9:f6f0f39538c7 226 // 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 9:f6f0f39538c7 227 // if (ball_velocity.x > 0) {
jamesheavey 9:f6f0f39538c7 228 // ball_velocity.x = -ball_velocity.x;
jamesheavey 9:f6f0f39538c7 229 // }
jamesheavey 9:f6f0f39538c7 230 // ball_pos.y = _p1y + _paddle_heigh - 1;
jamesheavey 9:f6f0f39538c7 231 // ball_velocity.y = -tan(ang);
jamesheavey 9:f6f0f39538c7 232 // }
jamesheavey 9:f6f0f39538c7 233 // else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
jamesheavey 9:f6f0f39538c7 234 // 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 9:f6f0f39538c7 235 // if (ball_velocity.x < 0) {
jamesheavey 9:f6f0f39538c7 236 // ball_velocity.x = -ball_velocity.x;
jamesheavey 9:f6f0f39538c7 237 // }
jamesheavey 9:f6f0f39538c7 238 // ball_pos.y = _p1y + _paddle_height - 1;
jamesheavey 9:f6f0f39538c7 239 // ball_velocity.y = -tan(ang);
jamesheavey 9:f6f0f39538c7 240 // }
jamesheavey 0:7d4d2023ed9c 241 }
jamesheavey 0:7d4d2023ed9c 242
jamesheavey 0:7d4d2023ed9c 243 // write new attributes
jamesheavey 0:7d4d2023ed9c 244 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 245 _ball.set_pos(ball_pos);
jamesheavey 0:7d4d2023ed9c 246 }
jamesheavey 0:7d4d2023ed9c 247
jamesheavey 4:6bd488b5b31a 248 void PongEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 4:6bd488b5b31a 249 {
jamesheavey 4:6bd488b5b31a 250 // read current ball attributes
jamesheavey 4:6bd488b5b31a 251 Vector2D ball_pos = _ball.get_pos();
jamesheavey 4:6bd488b5b31a 252 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 7:ef162a6232ca 253
jamesheavey 4:6bd488b5b31a 254 // check p1 first
jamesheavey 7:ef162a6232ca 255 Vector2D _brick11_pos = _brick11.get_pos();
jamesheavey 7:ef162a6232ca 256
jamesheavey 4:6bd488b5b31a 257 // see if ball has hit the paddle by checking for overlaps
jamesheavey 4:6bd488b5b31a 258 if (
jamesheavey 7:ef162a6232ca 259 (ball_pos.x >= _brick11_pos.x) && //left
jamesheavey 7:ef162a6232ca 260 (ball_pos.x <= _brick11_pos.x + WIDTH_BRICK) && //right
jamesheavey 7:ef162a6232ca 261 (ball_pos.y >= _brick11_pos.y) && //bottom
jamesheavey 7:ef162a6232ca 262 (ball_pos.y <= _brick11_pos.y + HEIGHT_BRICK) //top
jamesheavey 4:6bd488b5b31a 263 ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
jamesheavey 4:6bd488b5b31a 264 // if it has, fix position and reflect x velocity
jamesheavey 7:ef162a6232ca 265 ball_pos.y = _brick11_pos.y;
jamesheavey 4:6bd488b5b31a 266 ball_velocity.y = -ball_velocity.y;
jamesheavey 4:6bd488b5b31a 267 // audio feedback
jamesheavey 4:6bd488b5b31a 268 pad.tone(1000.0,0.1);
jamesheavey 4:6bd488b5b31a 269 }
jamesheavey 7:ef162a6232ca 270
jamesheavey 4:6bd488b5b31a 271 // write new attributes
jamesheavey 4:6bd488b5b31a 272 _ball.set_velocity(ball_velocity);
jamesheavey 4:6bd488b5b31a 273 _ball.set_pos(ball_pos);
jamesheavey 7:ef162a6232ca 274 _brick11.hit();
jamesheavey 4:6bd488b5b31a 275 }
jamesheavey 4:6bd488b5b31a 276
jamesheavey 0:7d4d2023ed9c 277 void PongEngine::check_goal(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 278 {
jamesheavey 0:7d4d2023ed9c 279 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 280 // P1 has scored
jamesheavey 0:7d4d2023ed9c 281 if (ball_pos.y > HEIGHT) {
jamesheavey 9:f6f0f39538c7 282 _p1.lose_life();
jamesheavey 0:7d4d2023ed9c 283 //lose_screen(); // go to loss screen then initialise again
jamesheavey 9:f6f0f39538c7 284
jamesheavey 0:7d4d2023ed9c 285 _ball.init(_ball_size,_speed);
jamesheavey 0:7d4d2023ed9c 286 pad.tone(1500.0,0.5);
jamesheavey 0:7d4d2023ed9c 287 pad.leds_on();
jamesheavey 0:7d4d2023ed9c 288 wait(0.5);
jamesheavey 0:7d4d2023ed9c 289 pad.leds_off();
jamesheavey 0:7d4d2023ed9c 290 }
jamesheavey 0:7d4d2023ed9c 291 }
jamesheavey 0:7d4d2023ed9c 292
jamesheavey 9:f6f0f39538c7 293
jamesheavey 9:f6f0f39538c7 294 //void lives_leds(Gamepad &pad)
jamesheavey 9:f6f0f39538c7 295 //{
jamesheavey 9:f6f0f39538c7 296 // int lives = _p1.get_pos();
jamesheavey 9:f6f0f39538c7 297 // if (lives == 6) {
jamesheavey 9:f6f0f39538c7 298 // pad.leds_on();
jamesheavey 9:f6f0f39538c7 299 // }
jamesheavey 9:f6f0f39538c7 300
jamesheavey 9:f6f0f39538c7 301 //if {lives == 5) {
jamesheavey 9:f6f0f39538c7 302 // 5leds on
jamesheavey 9:f6f0f39538c7 303 //}
jamesheavey 9:f6f0f39538c7 304
jamesheavey 9:f6f0f39538c7 305 // if (lives == 0) {
jamesheavey 9:f6f0f39538c7 306 // pad.leds_off();
jamesheavey 9:f6f0f39538c7 307 // }
jamesheavey 9:f6f0f39538c7 308 //}
jamesheavey 9:f6f0f39538c7 309
jamesheavey 9:f6f0f39538c7 310
jamesheavey 0:7d4d2023ed9c 311 void PongEngine::print_scores(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 312 {
jamesheavey 0:7d4d2023ed9c 313 // get scores from paddles
jamesheavey 0:7d4d2023ed9c 314 int p1_score = _p1.get_score();
jamesheavey 0:7d4d2023ed9c 315
jamesheavey 0:7d4d2023ed9c 316 // print to LCD i
jamesheavey 0:7d4d2023ed9c 317 char buffer1[14];
jamesheavey 0:7d4d2023ed9c 318 sprintf(buffer1,"%2d",p1_score);
jamesheavey 3:5719d0fe94ed 319 //lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
jamesheavey 0:7d4d2023ed9c 320 }