Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
lib/PongEngine.cpp@3:5746c6833d73, 2021-03-11 (annotated)
- Committer:
- eencae
- Date:
- Thu Mar 11 14:54:25 2021 +0000
- Revision:
- 3:5746c6833d73
- Parent:
- 2:482d74ef09c8
Added on LEDs and 7-seg for lives, bouncing off paddle and walls.
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 | 3:5746c6833d73 | 3 | PongEngine::PongEngine(){ _lives = 4; } |
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 | 3:5746c6833d73 | 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 | 3:5746c6833d73 | 11 | int PongEngine::update(UserInput input) { |
eencae | 3:5746c6833d73 | 12 | //printf("Pong Engine: Update\n"); |
eencae | 3:5746c6833d73 | 13 | check_goal(); // checking for a goal is a priority |
eencae | 1:d63a63f0d397 | 14 | _ball.update(); |
eencae | 1:d63a63f0d397 | 15 | _paddle.update(input); |
eencae | 1:d63a63f0d397 | 16 | // important to update paddles and ball before checking collisions so can |
eencae | 1:d63a63f0d397 | 17 | // correct for it before updating the display |
eencae | 1:d63a63f0d397 | 18 | check_wall_collision(); |
eencae | 2:482d74ef09c8 | 19 | check_paddle_collision(); |
eencae | 3:5746c6833d73 | 20 | |
eencae | 3:5746c6833d73 | 21 | return _lives; |
eencae | 1:d63a63f0d397 | 22 | } |
eencae | 1:d63a63f0d397 | 23 | |
eencae | 1:d63a63f0d397 | 24 | void PongEngine::draw(N5110 &lcd) { |
eencae | 3:5746c6833d73 | 25 | //printf("Pong Engine: Draw\n"); |
eencae | 1:d63a63f0d397 | 26 | // draw the elements in the LCD buffer |
eencae | 1:d63a63f0d397 | 27 | // pitch |
eencae | 3:5746c6833d73 | 28 | lcd.drawLine(0,0,WIDTH-1,0,1); // top |
eencae | 3:5746c6833d73 | 29 | lcd.drawLine(WIDTH-1,0,WIDTH-1,HEIGHT-1,1); // back wall |
eencae | 3:5746c6833d73 | 30 | lcd.drawLine(0,HEIGHT-1,WIDTH-1,HEIGHT-1,1); // bottom |
eencae | 1:d63a63f0d397 | 31 | _ball.draw(lcd); |
eencae | 1:d63a63f0d397 | 32 | _paddle.draw(lcd); |
eencae | 1:d63a63f0d397 | 33 | } |
eencae | 1:d63a63f0d397 | 34 | |
eencae | 2:482d74ef09c8 | 35 | void PongEngine::check_wall_collision() { |
eencae | 3:5746c6833d73 | 36 | //printf("Pong Engine: Check Wall Collision\n"); |
eencae | 1:d63a63f0d397 | 37 | // read current ball attributes |
eencae | 1:d63a63f0d397 | 38 | Position2D ball_pos = _ball.get_pos(); |
eencae | 1:d63a63f0d397 | 39 | Position2D ball_velocity = _ball.get_velocity(); |
eencae | 1:d63a63f0d397 | 40 | int size = _ball.get_size(); |
eencae | 1:d63a63f0d397 | 41 | |
eencae | 1:d63a63f0d397 | 42 | // check if hit top wall |
eencae | 1:d63a63f0d397 | 43 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
eencae | 1:d63a63f0d397 | 44 | ball_pos.y = 1; // bounce off ceiling without going off screen |
eencae | 1:d63a63f0d397 | 45 | ball_velocity.y = -ball_velocity.y; // flip velocity |
eencae | 2:482d74ef09c8 | 46 | } else if (ball_pos.y + size >= (HEIGHT-1) ) { |
eencae | 1:d63a63f0d397 | 47 | // hit bottom |
eencae | 1:d63a63f0d397 | 48 | ball_pos.y = (HEIGHT-1) - size; // stops ball going off screen |
eencae | 1:d63a63f0d397 | 49 | ball_velocity.y = -ball_velocity.y; // flip velcoity |
eencae | 2:482d74ef09c8 | 50 | } else if (ball_pos.x + size >= (WIDTH-1) ) { |
eencae | 2:482d74ef09c8 | 51 | // hit right wall |
eencae | 2:482d74ef09c8 | 52 | ball_pos.x = (WIDTH-1) - size; // stops ball going off screen |
eencae | 2:482d74ef09c8 | 53 | ball_velocity.x = -ball_velocity.x; // flip velcoity |
eencae | 2:482d74ef09c8 | 54 | } |
eencae | 1:d63a63f0d397 | 55 | |
eencae | 1:d63a63f0d397 | 56 | // update ball parameters |
eencae | 1:d63a63f0d397 | 57 | _ball.set_velocity(ball_velocity); |
eencae | 1:d63a63f0d397 | 58 | _ball.set_pos(ball_pos); |
eencae | 2:482d74ef09c8 | 59 | } |
eencae | 2:482d74ef09c8 | 60 | |
eencae | 2:482d74ef09c8 | 61 | void PongEngine::check_paddle_collision() { |
eencae | 3:5746c6833d73 | 62 | //printf("Pong Engine: Check Paddle Collision\n"); |
eencae | 2:482d74ef09c8 | 63 | // read current ball and paddle attributes |
eencae | 2:482d74ef09c8 | 64 | Position2D ball_pos = _ball.get_pos(); |
eencae | 2:482d74ef09c8 | 65 | Position2D ball_velocity = _ball.get_velocity(); |
eencae | 2:482d74ef09c8 | 66 | Position2D paddle_pos = _paddle.get_pos(); // paddle |
eencae | 2:482d74ef09c8 | 67 | |
eencae | 2:482d74ef09c8 | 68 | // see if ball has hit the paddle by checking for overlaps |
eencae | 2:482d74ef09c8 | 69 | if ( |
eencae | 2:482d74ef09c8 | 70 | (ball_pos.y >= paddle_pos.y) && //top |
eencae | 2:482d74ef09c8 | 71 | (ball_pos.y <= paddle_pos.y + _paddle.get_height() ) && //bottom |
eencae | 2:482d74ef09c8 | 72 | (ball_pos.x >= paddle_pos.x) && //left |
eencae | 2:482d74ef09c8 | 73 | (ball_pos.x <= paddle_pos.x + _paddle.get_width() ) //right |
eencae | 2:482d74ef09c8 | 74 | ) { |
eencae | 2:482d74ef09c8 | 75 | // if it has, fix position and reflect x velocity |
eencae | 2:482d74ef09c8 | 76 | ball_pos.x = paddle_pos.x + _paddle.get_width(); |
eencae | 2:482d74ef09c8 | 77 | ball_velocity.x = -ball_velocity.x; |
eencae | 2:482d74ef09c8 | 78 | } |
eencae | 2:482d74ef09c8 | 79 | |
eencae | 2:482d74ef09c8 | 80 | // write new attributes |
eencae | 2:482d74ef09c8 | 81 | _ball.set_velocity(ball_velocity); |
eencae | 2:482d74ef09c8 | 82 | _ball.set_pos(ball_pos); |
eencae | 3:5746c6833d73 | 83 | } |
eencae | 3:5746c6833d73 | 84 | |
eencae | 3:5746c6833d73 | 85 | void PongEngine::check_goal() { |
eencae | 3:5746c6833d73 | 86 | //printf("Pong Engine: Check Goal\n"); |
eencae | 3:5746c6833d73 | 87 | Position2D ball_pos = _ball.get_pos(); |
eencae | 3:5746c6833d73 | 88 | int size = _ball.get_size(); |
eencae | 3:5746c6833d73 | 89 | int speed = abs(_ball.get_velocity().x); // speed is magnitude of velocity |
eencae | 3:5746c6833d73 | 90 | // check if ball position has gone off the left |
eencae | 3:5746c6833d73 | 91 | if (ball_pos.x + size < 0) { |
eencae | 3:5746c6833d73 | 92 | // reset the ball |
eencae | 3:5746c6833d73 | 93 | _ball.init(size,speed); |
eencae | 3:5746c6833d73 | 94 | _lives--; // lose a life |
eencae | 3:5746c6833d73 | 95 | } |
eencae | 1:d63a63f0d397 | 96 | } |