Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PongEngine/PongEngine.cpp@4:6bd488b5b31a, 2019-04-19 (annotated)
- Committer:
- jamesheavey
- Date:
- Fri Apr 19 12:30:58 2019 +0000
- Revision:
- 4:6bd488b5b31a
- Parent:
- 3:5719d0fe94ed
- Child:
- 6:7f79f320b827
fixed collisions, added brick collisions, a pause screen that doesnt unpause, bricks dont destroy after lives = 0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jamesheavey | 0:7d4d2023ed9c | 1 | #include "PongEngine.h" |
| jamesheavey | 0:7d4d2023ed9c | 2 | |
| jamesheavey | 0:7d4d2023ed9c | 3 | PongEngine::PongEngine() |
| jamesheavey | 0:7d4d2023ed9c | 4 | { |
| jamesheavey | 0:7d4d2023ed9c | 5 | |
| jamesheavey | 0:7d4d2023ed9c | 6 | } |
| jamesheavey | 0:7d4d2023ed9c | 7 | |
| jamesheavey | 0:7d4d2023ed9c | 8 | PongEngine::~PongEngine() |
| jamesheavey | 0:7d4d2023ed9c | 9 | { |
| jamesheavey | 0:7d4d2023ed9c | 10 | |
| jamesheavey | 0:7d4d2023ed9c | 11 | } |
| jamesheavey | 0:7d4d2023ed9c | 12 | |
| jamesheavey | 0:7d4d2023ed9c | 13 | void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed) |
| jamesheavey | 0:7d4d2023ed9c | 14 | { |
| jamesheavey | 0:7d4d2023ed9c | 15 | // initialise the game parameters |
| jamesheavey | 0:7d4d2023ed9c | 16 | _paddle_width = paddle_width; |
| jamesheavey | 0:7d4d2023ed9c | 17 | _paddle_height = paddle_height; |
| jamesheavey | 0:7d4d2023ed9c | 18 | _ball_size = ball_size; |
| jamesheavey | 0:7d4d2023ed9c | 19 | _speed = speed; |
| jamesheavey | 0:7d4d2023ed9c | 20 | |
| jamesheavey | 1:e18615d46071 | 21 | // y position on screen - WIDTH is defined in N5110.h |
| jamesheavey | 1:e18615d46071 | 22 | _p1y = HEIGHT - GAP; |
| jamesheavey | 0:7d4d2023ed9c | 23 | |
| jamesheavey | 0:7d4d2023ed9c | 24 | // puts paddles and ball in middle |
| jamesheavey | 1:e18615d46071 | 25 | _p1.init(_p1y,_paddle_height,_paddle_width); |
| jamesheavey | 0:7d4d2023ed9c | 26 | _ball.init(_ball_size,_speed); |
| jamesheavey | 2:b3ca50f2e85d | 27 | |
| jamesheavey | 2:b3ca50f2e85d | 28 | _grid1.init(3,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); // need to figure out how to make a list of these |
| jamesheavey | 2:b3ca50f2e85d | 29 | _grid2.init(13,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 30 | _grid3.init(23,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 31 | _grid4.init(33,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 32 | _grid5.init(43,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 33 | _grid6.init(53,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 34 | _grid7.init(63,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 35 | _grid8.init(73,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 36 | _grid9.init(83,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 0:7d4d2023ed9c | 37 | } |
| jamesheavey | 0:7d4d2023ed9c | 38 | |
| jamesheavey | 0:7d4d2023ed9c | 39 | void PongEngine::read_input(Gamepad &pad) |
| jamesheavey | 0:7d4d2023ed9c | 40 | { |
| jamesheavey | 0:7d4d2023ed9c | 41 | _d = pad.get_direction(); |
| jamesheavey | 0:7d4d2023ed9c | 42 | _mag = pad.get_mag(); |
| jamesheavey | 2:b3ca50f2e85d | 43 | //_pause = pad.check_event(Gamepad::START_PRESSED); // == false then system("PAUSE") or cin.get() |
| jamesheavey | 0:7d4d2023ed9c | 44 | } |
| jamesheavey | 0:7d4d2023ed9c | 45 | |
| jamesheavey | 0:7d4d2023ed9c | 46 | void PongEngine::draw(N5110 &lcd) |
| jamesheavey | 0:7d4d2023ed9c | 47 | { |
| jamesheavey | 0:7d4d2023ed9c | 48 | // draw the elements in the LCD buffer |
| jamesheavey | 0:7d4d2023ed9c | 49 | // pitch |
| jamesheavey | 0:7d4d2023ed9c | 50 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
| jamesheavey | 0:7d4d2023ed9c | 51 | //score |
| jamesheavey | 0:7d4d2023ed9c | 52 | print_scores(lcd); |
| jamesheavey | 0:7d4d2023ed9c | 53 | // paddles |
| jamesheavey | 0:7d4d2023ed9c | 54 | _p1.draw(lcd); |
| jamesheavey | 0:7d4d2023ed9c | 55 | // ball |
| jamesheavey | 0:7d4d2023ed9c | 56 | _ball.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 57 | |
| jamesheavey | 2:b3ca50f2e85d | 58 | _grid1.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 59 | _grid2.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 60 | _grid3.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 61 | _grid4.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 62 | _grid5.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 63 | _grid6.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 64 | _grid7.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 65 | _grid8.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 66 | _grid9.draw(lcd); |
| jamesheavey | 2:b3ca50f2e85d | 67 | |
| jamesheavey | 2:b3ca50f2e85d | 68 | |
| jamesheavey | 2:b3ca50f2e85d | 69 | |
| jamesheavey | 2:b3ca50f2e85d | 70 | |
| jamesheavey | 2:b3ca50f2e85d | 71 | // for(int i = GAP+1; i <= WIDTH; i+= WIDTH_BRICK + 1){ |
| jamesheavey | 2:b3ca50f2e85d | 72 | // for(int j = GAP+1; j <= (HEIGHT_BRICK + 1)*3; j+= HEIGHT_BRICK + 1){ |
| jamesheavey | 2:b3ca50f2e85d | 73 | // Vector3d grid[i][j]= _grid.init(i, j,WIDTH_BRICK,HEIGHT_BRICK); |
| jamesheavey | 2:b3ca50f2e85d | 74 | // } |
| jamesheavey | 2:b3ca50f2e85d | 75 | // } |
| jamesheavey | 0:7d4d2023ed9c | 76 | } |
| jamesheavey | 0:7d4d2023ed9c | 77 | |
| jamesheavey | 2:b3ca50f2e85d | 78 | |
| jamesheavey | 0:7d4d2023ed9c | 79 | void PongEngine::update(Gamepad &pad) |
| jamesheavey | 0:7d4d2023ed9c | 80 | { |
| jamesheavey | 0:7d4d2023ed9c | 81 | check_goal(pad); |
| jamesheavey | 0:7d4d2023ed9c | 82 | // important to update paddles and ball before checking collisions so can |
| jamesheavey | 0:7d4d2023ed9c | 83 | // correct for it before updating the display |
| jamesheavey | 0:7d4d2023ed9c | 84 | _p1.update(_d,_mag); |
| jamesheavey | 0:7d4d2023ed9c | 85 | _ball.update(); |
| jamesheavey | 0:7d4d2023ed9c | 86 | |
| jamesheavey | 0:7d4d2023ed9c | 87 | check_wall_collision(pad); |
| jamesheavey | 0:7d4d2023ed9c | 88 | check_paddle_collisions(pad); |
| jamesheavey | 4:6bd488b5b31a | 89 | check_brick_collisions(pad); |
| jamesheavey | 0:7d4d2023ed9c | 90 | } |
| jamesheavey | 0:7d4d2023ed9c | 91 | |
| jamesheavey | 0:7d4d2023ed9c | 92 | void PongEngine::check_wall_collision(Gamepad &pad) |
| jamesheavey | 0:7d4d2023ed9c | 93 | { |
| jamesheavey | 0:7d4d2023ed9c | 94 | // read current ball attributes |
| jamesheavey | 0:7d4d2023ed9c | 95 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 0:7d4d2023ed9c | 96 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 0:7d4d2023ed9c | 97 | |
| jamesheavey | 0:7d4d2023ed9c | 98 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 0:7d4d2023ed9c | 99 | ball_pos.y = 1; // bounce off ceiling without going off screen |
| jamesheavey | 0:7d4d2023ed9c | 100 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 0:7d4d2023ed9c | 101 | // audio feedback |
| jamesheavey | 0:7d4d2023ed9c | 102 | pad.tone(750.0,0.1); |
| jamesheavey | 0:7d4d2023ed9c | 103 | } |
| jamesheavey | 0:7d4d2023ed9c | 104 | |
| jamesheavey | 0:7d4d2023ed9c | 105 | else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 0:7d4d2023ed9c | 106 | ball_pos.x = 1; // bounce off ceiling without going off screen |
| jamesheavey | 0:7d4d2023ed9c | 107 | ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 0:7d4d2023ed9c | 108 | // audio feedback |
| jamesheavey | 0:7d4d2023ed9c | 109 | pad.tone(750.0,0.1); |
| jamesheavey | 0:7d4d2023ed9c | 110 | } |
| jamesheavey | 0:7d4d2023ed9c | 111 | |
| jamesheavey | 0:7d4d2023ed9c | 112 | // check if hit bottom wall |
| jamesheavey | 0:7d4d2023ed9c | 113 | else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47 |
| jamesheavey | 0:7d4d2023ed9c | 114 | // hit bottom |
| jamesheavey | 0:7d4d2023ed9c | 115 | ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen |
| jamesheavey | 0:7d4d2023ed9c | 116 | ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 0:7d4d2023ed9c | 117 | // audio feedback |
| jamesheavey | 0:7d4d2023ed9c | 118 | pad.tone(750.0,0.1); |
| jamesheavey | 0:7d4d2023ed9c | 119 | } |
| jamesheavey | 0:7d4d2023ed9c | 120 | |
| jamesheavey | 0:7d4d2023ed9c | 121 | // update ball parameters |
| jamesheavey | 0:7d4d2023ed9c | 122 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 0:7d4d2023ed9c | 123 | _ball.set_pos(ball_pos); |
| jamesheavey | 4:6bd488b5b31a | 124 | |
| jamesheavey | 0:7d4d2023ed9c | 125 | } |
| jamesheavey | 0:7d4d2023ed9c | 126 | |
| jamesheavey | 0:7d4d2023ed9c | 127 | void PongEngine::check_paddle_collisions(Gamepad &pad) |
| jamesheavey | 0:7d4d2023ed9c | 128 | { |
| jamesheavey | 0:7d4d2023ed9c | 129 | // read current ball attributes |
| jamesheavey | 0:7d4d2023ed9c | 130 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 0:7d4d2023ed9c | 131 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 0:7d4d2023ed9c | 132 | |
| jamesheavey | 0:7d4d2023ed9c | 133 | // check p1 first |
| jamesheavey | 0:7d4d2023ed9c | 134 | Vector2D p1_pos = _p1.get_pos(); |
| jamesheavey | 0:7d4d2023ed9c | 135 | |
| jamesheavey | 0:7d4d2023ed9c | 136 | // see if ball has hit the paddle by checking for overlaps |
| jamesheavey | 0:7d4d2023ed9c | 137 | if ( |
| jamesheavey | 4:6bd488b5b31a | 138 | (ball_pos.x >= p1_pos.x) && //left |
| jamesheavey | 4:6bd488b5b31a | 139 | (ball_pos.x <= p1_pos.x + _paddle_width) && //right |
| jamesheavey | 4:6bd488b5b31a | 140 | (ball_pos.y >= _p1y) && //bottom |
| jamesheavey | 4:6bd488b5b31a | 141 | (ball_pos.y <= _p1y + _paddle_height) //top |
| jamesheavey | 4:6bd488b5b31a | 142 | ) { // 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 | 143 | // if it has, fix position and reflect x velocity |
| jamesheavey | 1:e18615d46071 | 144 | ball_pos.y = _p1y + _paddle_height; |
| jamesheavey | 1:e18615d46071 | 145 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 0:7d4d2023ed9c | 146 | // audio feedback |
| jamesheavey | 0:7d4d2023ed9c | 147 | pad.tone(1000.0,0.1); |
| jamesheavey | 0:7d4d2023ed9c | 148 | } |
| jamesheavey | 0:7d4d2023ed9c | 149 | |
| jamesheavey | 0:7d4d2023ed9c | 150 | // write new attributes |
| jamesheavey | 0:7d4d2023ed9c | 151 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 0:7d4d2023ed9c | 152 | _ball.set_pos(ball_pos); |
| jamesheavey | 0:7d4d2023ed9c | 153 | } |
| jamesheavey | 0:7d4d2023ed9c | 154 | |
| jamesheavey | 4:6bd488b5b31a | 155 | void PongEngine::check_brick_collisions(Gamepad &pad) |
| jamesheavey | 4:6bd488b5b31a | 156 | { |
| jamesheavey | 4:6bd488b5b31a | 157 | // read current ball attributes |
| jamesheavey | 4:6bd488b5b31a | 158 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 4:6bd488b5b31a | 159 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 4:6bd488b5b31a | 160 | |
| jamesheavey | 4:6bd488b5b31a | 161 | // check p1 first |
| jamesheavey | 4:6bd488b5b31a | 162 | Vector2D _grid1_pos = _grid1.get_pos(); |
| jamesheavey | 4:6bd488b5b31a | 163 | |
| jamesheavey | 4:6bd488b5b31a | 164 | // see if ball has hit the paddle by checking for overlaps |
| jamesheavey | 4:6bd488b5b31a | 165 | if ( |
| jamesheavey | 4:6bd488b5b31a | 166 | (ball_pos.x >= _grid1_pos.x) && //left |
| jamesheavey | 4:6bd488b5b31a | 167 | (ball_pos.x <= _grid1_pos.x + WIDTH_BRICK) && //right |
| jamesheavey | 4:6bd488b5b31a | 168 | (ball_pos.y >= _grid1_pos.y) && //bottom |
| jamesheavey | 4:6bd488b5b31a | 169 | (ball_pos.y <= _grid1_pos.y + HEIGHT_BRICK) //top |
| jamesheavey | 4:6bd488b5b31a | 170 | ) { // 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 | 171 | // if it has, fix position and reflect x velocity |
| jamesheavey | 4:6bd488b5b31a | 172 | ball_pos.y = _grid1_pos.y; |
| jamesheavey | 4:6bd488b5b31a | 173 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 4:6bd488b5b31a | 174 | // audio feedback |
| jamesheavey | 4:6bd488b5b31a | 175 | pad.tone(1000.0,0.1); |
| jamesheavey | 4:6bd488b5b31a | 176 | } |
| jamesheavey | 4:6bd488b5b31a | 177 | |
| jamesheavey | 4:6bd488b5b31a | 178 | // write new attributes |
| jamesheavey | 4:6bd488b5b31a | 179 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 4:6bd488b5b31a | 180 | _ball.set_pos(ball_pos); |
| jamesheavey | 4:6bd488b5b31a | 181 | _grid1.hit(); |
| jamesheavey | 4:6bd488b5b31a | 182 | } |
| jamesheavey | 4:6bd488b5b31a | 183 | |
| jamesheavey | 0:7d4d2023ed9c | 184 | void PongEngine::check_goal(Gamepad &pad) |
| jamesheavey | 0:7d4d2023ed9c | 185 | { |
| jamesheavey | 0:7d4d2023ed9c | 186 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 0:7d4d2023ed9c | 187 | // P1 has scored |
| jamesheavey | 0:7d4d2023ed9c | 188 | if (ball_pos.y > HEIGHT) { |
| jamesheavey | 0:7d4d2023ed9c | 189 | //lose_screen(); // go to loss screen then initialise again |
| jamesheavey | 0:7d4d2023ed9c | 190 | _ball.init(_ball_size,_speed); |
| jamesheavey | 0:7d4d2023ed9c | 191 | pad.tone(1500.0,0.5); |
| jamesheavey | 0:7d4d2023ed9c | 192 | pad.leds_on(); |
| jamesheavey | 0:7d4d2023ed9c | 193 | wait(0.5); |
| jamesheavey | 0:7d4d2023ed9c | 194 | pad.leds_off(); |
| jamesheavey | 0:7d4d2023ed9c | 195 | } |
| jamesheavey | 0:7d4d2023ed9c | 196 | } |
| jamesheavey | 0:7d4d2023ed9c | 197 | |
| jamesheavey | 0:7d4d2023ed9c | 198 | void PongEngine::print_scores(N5110 &lcd) |
| jamesheavey | 0:7d4d2023ed9c | 199 | { |
| jamesheavey | 0:7d4d2023ed9c | 200 | // get scores from paddles |
| jamesheavey | 0:7d4d2023ed9c | 201 | int p1_score = _p1.get_score(); |
| jamesheavey | 0:7d4d2023ed9c | 202 | |
| jamesheavey | 0:7d4d2023ed9c | 203 | // print to LCD i |
| jamesheavey | 0:7d4d2023ed9c | 204 | char buffer1[14]; |
| jamesheavey | 0:7d4d2023ed9c | 205 | sprintf(buffer1,"%2d",p1_score); |
| jamesheavey | 3:5719d0fe94ed | 206 | //lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
| jamesheavey | 0:7d4d2023ed9c | 207 | } |