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.
lib/PongEngine.cpp@2:482d74ef09c8, 2021-03-10 (annotated)
- Committer:
- eencae
- Date:
- Wed Mar 10 16:37:52 2021 +0000
- Revision:
- 2:482d74ef09c8
- Parent:
- 1:d63a63f0d397
- Child:
- 3:5746c6833d73
Added re-bound off back wall and paddle collisions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 1:d63a63f0d397 | 1 | #include "PongEngine.h" |
eencae | 1:d63a63f0d397 | 2 | |
eencae | 1:d63a63f0d397 | 3 | PongEngine::PongEngine(){} |
eencae | 1:d63a63f0d397 | 4 | |
eencae | 1:d63a63f0d397 | 5 | void PongEngine::init(int paddle_position, int paddle_height, int paddle_width, int ball_size, int speed){ |
eencae | 1:d63a63f0d397 | 6 | printf("Pong Engine: Init\n"); |
eencae | 1:d63a63f0d397 | 7 | _ball.init(ball_size,speed); |
eencae | 1:d63a63f0d397 | 8 | _paddle.init(paddle_position, paddle_height, paddle_width); |
eencae | 1:d63a63f0d397 | 9 | } |
eencae | 1:d63a63f0d397 | 10 | |
eencae | 1:d63a63f0d397 | 11 | void PongEngine::update(UserInput input) { |
eencae | 1:d63a63f0d397 | 12 | printf("Pong Engine: Update\n"); |
eencae | 1:d63a63f0d397 | 13 | _ball.update(); |
eencae | 1:d63a63f0d397 | 14 | _paddle.update(input); |
eencae | 1:d63a63f0d397 | 15 | // important to update paddles and ball before checking collisions so can |
eencae | 1:d63a63f0d397 | 16 | // correct for it before updating the display |
eencae | 1:d63a63f0d397 | 17 | check_wall_collision(); |
eencae | 2:482d74ef09c8 | 18 | check_paddle_collision(); |
eencae | 1:d63a63f0d397 | 19 | } |
eencae | 1:d63a63f0d397 | 20 | |
eencae | 1:d63a63f0d397 | 21 | void PongEngine::draw(N5110 &lcd) { |
eencae | 1:d63a63f0d397 | 22 | printf("Pong Engine: Draw\n"); |
eencae | 1:d63a63f0d397 | 23 | // draw the elements in the LCD buffer |
eencae | 1:d63a63f0d397 | 24 | // pitch |
eencae | 1:d63a63f0d397 | 25 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
eencae | 1:d63a63f0d397 | 26 | lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2); |
eencae | 1:d63a63f0d397 | 27 | _ball.draw(lcd); |
eencae | 1:d63a63f0d397 | 28 | _paddle.draw(lcd); |
eencae | 1:d63a63f0d397 | 29 | } |
eencae | 1:d63a63f0d397 | 30 | |
eencae | 2:482d74ef09c8 | 31 | void PongEngine::check_wall_collision() { |
eencae | 2:482d74ef09c8 | 32 | printf("Pong Engine: Check Wall Collision\n"); |
eencae | 1:d63a63f0d397 | 33 | // read current ball attributes |
eencae | 1:d63a63f0d397 | 34 | Position2D ball_pos = _ball.get_pos(); |
eencae | 1:d63a63f0d397 | 35 | Position2D ball_velocity = _ball.get_velocity(); |
eencae | 1:d63a63f0d397 | 36 | int size = _ball.get_size(); |
eencae | 1:d63a63f0d397 | 37 | |
eencae | 1:d63a63f0d397 | 38 | // check if hit top wall |
eencae | 1:d63a63f0d397 | 39 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
eencae | 1:d63a63f0d397 | 40 | ball_pos.y = 1; // bounce off ceiling without going off screen |
eencae | 1:d63a63f0d397 | 41 | ball_velocity.y = -ball_velocity.y; // flip velocity |
eencae | 2:482d74ef09c8 | 42 | } else if (ball_pos.y + size >= (HEIGHT-1) ) { |
eencae | 1:d63a63f0d397 | 43 | // hit bottom |
eencae | 1:d63a63f0d397 | 44 | ball_pos.y = (HEIGHT-1) - size; // stops ball going off screen |
eencae | 1:d63a63f0d397 | 45 | ball_velocity.y = -ball_velocity.y; // flip velcoity |
eencae | 2:482d74ef09c8 | 46 | } else if (ball_pos.x + size >= (WIDTH-1) ) { |
eencae | 2:482d74ef09c8 | 47 | // hit right wall |
eencae | 2:482d74ef09c8 | 48 | ball_pos.x = (WIDTH-1) - size; // stops ball going off screen |
eencae | 2:482d74ef09c8 | 49 | ball_velocity.x = -ball_velocity.x; // flip velcoity |
eencae | 2:482d74ef09c8 | 50 | } |
eencae | 1:d63a63f0d397 | 51 | |
eencae | 1:d63a63f0d397 | 52 | // update ball parameters |
eencae | 1:d63a63f0d397 | 53 | _ball.set_velocity(ball_velocity); |
eencae | 1:d63a63f0d397 | 54 | _ball.set_pos(ball_pos); |
eencae | 2:482d74ef09c8 | 55 | } |
eencae | 2:482d74ef09c8 | 56 | |
eencae | 2:482d74ef09c8 | 57 | void PongEngine::check_paddle_collision() { |
eencae | 2:482d74ef09c8 | 58 | printf("Pong Engine: Check Paddle Collision\n"); |
eencae | 2:482d74ef09c8 | 59 | // read current ball and paddle attributes |
eencae | 2:482d74ef09c8 | 60 | Position2D ball_pos = _ball.get_pos(); |
eencae | 2:482d74ef09c8 | 61 | Position2D ball_velocity = _ball.get_velocity(); |
eencae | 2:482d74ef09c8 | 62 | Position2D paddle_pos = _paddle.get_pos(); // paddle |
eencae | 2:482d74ef09c8 | 63 | |
eencae | 2:482d74ef09c8 | 64 | // see if ball has hit the paddle by checking for overlaps |
eencae | 2:482d74ef09c8 | 65 | if ( |
eencae | 2:482d74ef09c8 | 66 | (ball_pos.y >= paddle_pos.y) && //top |
eencae | 2:482d74ef09c8 | 67 | (ball_pos.y <= paddle_pos.y + _paddle.get_height() ) && //bottom |
eencae | 2:482d74ef09c8 | 68 | (ball_pos.x >= paddle_pos.x) && //left |
eencae | 2:482d74ef09c8 | 69 | (ball_pos.x <= paddle_pos.x + _paddle.get_width() ) //right |
eencae | 2:482d74ef09c8 | 70 | ) { |
eencae | 2:482d74ef09c8 | 71 | // if it has, fix position and reflect x velocity |
eencae | 2:482d74ef09c8 | 72 | ball_pos.x = paddle_pos.x + _paddle.get_width(); |
eencae | 2:482d74ef09c8 | 73 | ball_velocity.x = -ball_velocity.x; |
eencae | 2:482d74ef09c8 | 74 | } |
eencae | 2:482d74ef09c8 | 75 | |
eencae | 2:482d74ef09c8 | 76 | // write new attributes |
eencae | 2:482d74ef09c8 | 77 | _ball.set_velocity(ball_velocity); |
eencae | 2:482d74ef09c8 | 78 | _ball.set_pos(ball_pos); |
eencae | 1:d63a63f0d397 | 79 | } |