James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Sun Apr 21 12:45:11 2019 +0000
Revision:
7:ef162a6232ca
Parent:
6:7f79f320b827
Child:
8:1ab6d90c4d60
hardcoded the bricks, need to iterate. deleting the bricks doesnt work, need victory screen (if list of bricks == 0) or something

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 0:7d4d2023ed9c 142
jamesheavey 0:7d4d2023ed9c 143 check_wall_collision(pad);
jamesheavey 0:7d4d2023ed9c 144 check_paddle_collisions(pad);
jamesheavey 4:6bd488b5b31a 145 check_brick_collisions(pad);
jamesheavey 0:7d4d2023ed9c 146 }
jamesheavey 0:7d4d2023ed9c 147
jamesheavey 0:7d4d2023ed9c 148 void PongEngine::check_wall_collision(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 149 {
jamesheavey 0:7d4d2023ed9c 150 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 151 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 152 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 153
jamesheavey 0:7d4d2023ed9c 154 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 155 ball_pos.y = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 156 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 157 // audio feedback
jamesheavey 0:7d4d2023ed9c 158 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 159 }
jamesheavey 0:7d4d2023ed9c 160
jamesheavey 0:7d4d2023ed9c 161 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 162 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 163 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 164 // audio feedback
jamesheavey 0:7d4d2023ed9c 165 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 166 }
jamesheavey 0:7d4d2023ed9c 167
jamesheavey 0:7d4d2023ed9c 168 // check if hit bottom wall
jamesheavey 0:7d4d2023ed9c 169 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 0:7d4d2023ed9c 170 // hit bottom
jamesheavey 0:7d4d2023ed9c 171 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 0:7d4d2023ed9c 172 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 173 // audio feedback
jamesheavey 0:7d4d2023ed9c 174 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 175 }
jamesheavey 0:7d4d2023ed9c 176
jamesheavey 0:7d4d2023ed9c 177 // update ball parameters
jamesheavey 0:7d4d2023ed9c 178 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 179 _ball.set_pos(ball_pos);
jamesheavey 4:6bd488b5b31a 180
jamesheavey 0:7d4d2023ed9c 181 }
jamesheavey 0:7d4d2023ed9c 182
jamesheavey 0:7d4d2023ed9c 183 void PongEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 184 {
jamesheavey 0:7d4d2023ed9c 185 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 186 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 187 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 188
jamesheavey 0:7d4d2023ed9c 189 // check p1 first
jamesheavey 0:7d4d2023ed9c 190 Vector2D p1_pos = _p1.get_pos();
jamesheavey 0:7d4d2023ed9c 191
jamesheavey 0:7d4d2023ed9c 192 // see if ball has hit the paddle by checking for overlaps
jamesheavey 0:7d4d2023ed9c 193 if (
jamesheavey 4:6bd488b5b31a 194 (ball_pos.x >= p1_pos.x) && //left
jamesheavey 4:6bd488b5b31a 195 (ball_pos.x <= p1_pos.x + _paddle_width) && //right
jamesheavey 4:6bd488b5b31a 196 (ball_pos.y >= _p1y) && //bottom
jamesheavey 4:6bd488b5b31a 197 (ball_pos.y <= _p1y + _paddle_height) //top
jamesheavey 4:6bd488b5b31a 198 ) { // 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 199 // if it has, fix position and reflect x velocity
jamesheavey 1:e18615d46071 200 ball_pos.y = _p1y + _paddle_height;
jamesheavey 1:e18615d46071 201 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 202 // audio feedback
jamesheavey 0:7d4d2023ed9c 203 pad.tone(1000.0,0.1);
jamesheavey 0:7d4d2023ed9c 204 }
jamesheavey 0:7d4d2023ed9c 205
jamesheavey 0:7d4d2023ed9c 206 // write new attributes
jamesheavey 0:7d4d2023ed9c 207 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 208 _ball.set_pos(ball_pos);
jamesheavey 0:7d4d2023ed9c 209 }
jamesheavey 0:7d4d2023ed9c 210
jamesheavey 4:6bd488b5b31a 211 void PongEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 4:6bd488b5b31a 212 {
jamesheavey 4:6bd488b5b31a 213 // read current ball attributes
jamesheavey 4:6bd488b5b31a 214 Vector2D ball_pos = _ball.get_pos();
jamesheavey 4:6bd488b5b31a 215 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 7:ef162a6232ca 216
jamesheavey 4:6bd488b5b31a 217 // check p1 first
jamesheavey 7:ef162a6232ca 218 Vector2D _brick11_pos = _brick11.get_pos();
jamesheavey 7:ef162a6232ca 219
jamesheavey 4:6bd488b5b31a 220 // see if ball has hit the paddle by checking for overlaps
jamesheavey 4:6bd488b5b31a 221 if (
jamesheavey 7:ef162a6232ca 222 (ball_pos.x >= _brick11_pos.x) && //left
jamesheavey 7:ef162a6232ca 223 (ball_pos.x <= _brick11_pos.x + WIDTH_BRICK) && //right
jamesheavey 7:ef162a6232ca 224 (ball_pos.y >= _brick11_pos.y) && //bottom
jamesheavey 7:ef162a6232ca 225 (ball_pos.y <= _brick11_pos.y + HEIGHT_BRICK) //top
jamesheavey 4:6bd488b5b31a 226 ) { // 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 227 // if it has, fix position and reflect x velocity
jamesheavey 7:ef162a6232ca 228 ball_pos.y = _brick11_pos.y;
jamesheavey 4:6bd488b5b31a 229 ball_velocity.y = -ball_velocity.y;
jamesheavey 4:6bd488b5b31a 230 // audio feedback
jamesheavey 4:6bd488b5b31a 231 pad.tone(1000.0,0.1);
jamesheavey 4:6bd488b5b31a 232 }
jamesheavey 7:ef162a6232ca 233
jamesheavey 4:6bd488b5b31a 234 // write new attributes
jamesheavey 4:6bd488b5b31a 235 _ball.set_velocity(ball_velocity);
jamesheavey 4:6bd488b5b31a 236 _ball.set_pos(ball_pos);
jamesheavey 7:ef162a6232ca 237 _brick11.hit();
jamesheavey 4:6bd488b5b31a 238 }
jamesheavey 4:6bd488b5b31a 239
jamesheavey 0:7d4d2023ed9c 240 void PongEngine::check_goal(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 241 {
jamesheavey 0:7d4d2023ed9c 242 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 243 // P1 has scored
jamesheavey 0:7d4d2023ed9c 244 if (ball_pos.y > HEIGHT) {
jamesheavey 0:7d4d2023ed9c 245 //lose_screen(); // go to loss screen then initialise again
jamesheavey 0:7d4d2023ed9c 246 _ball.init(_ball_size,_speed);
jamesheavey 0:7d4d2023ed9c 247 pad.tone(1500.0,0.5);
jamesheavey 0:7d4d2023ed9c 248 pad.leds_on();
jamesheavey 0:7d4d2023ed9c 249 wait(0.5);
jamesheavey 0:7d4d2023ed9c 250 pad.leds_off();
jamesheavey 0:7d4d2023ed9c 251 }
jamesheavey 0:7d4d2023ed9c 252 }
jamesheavey 0:7d4d2023ed9c 253
jamesheavey 0:7d4d2023ed9c 254 void PongEngine::print_scores(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 255 {
jamesheavey 0:7d4d2023ed9c 256 // get scores from paddles
jamesheavey 0:7d4d2023ed9c 257 int p1_score = _p1.get_score();
jamesheavey 0:7d4d2023ed9c 258
jamesheavey 0:7d4d2023ed9c 259 // print to LCD i
jamesheavey 0:7d4d2023ed9c 260 char buffer1[14];
jamesheavey 0:7d4d2023ed9c 261 sprintf(buffer1,"%2d",p1_score);
jamesheavey 3:5719d0fe94ed 262 //lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
jamesheavey 0:7d4d2023ed9c 263 }