James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed Apr 17 21:19:16 2019 +0000
Revision:
1:e18615d46071
Parent:
0:7d4d2023ed9c
Child:
2:b3ca50f2e85d
Added the brick class

Who changed what in which revision?

UserRevisionLine numberNew 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 0:7d4d2023ed9c 27 }
jamesheavey 0:7d4d2023ed9c 28
jamesheavey 0:7d4d2023ed9c 29 void PongEngine::read_input(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 30 {
jamesheavey 0:7d4d2023ed9c 31 _d = pad.get_direction();
jamesheavey 0:7d4d2023ed9c 32 _mag = pad.get_mag();
jamesheavey 0:7d4d2023ed9c 33 }
jamesheavey 0:7d4d2023ed9c 34
jamesheavey 0:7d4d2023ed9c 35 void PongEngine::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 36 {
jamesheavey 0:7d4d2023ed9c 37 // draw the elements in the LCD buffer
jamesheavey 0:7d4d2023ed9c 38 // pitch
jamesheavey 0:7d4d2023ed9c 39 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
jamesheavey 0:7d4d2023ed9c 40 //score
jamesheavey 0:7d4d2023ed9c 41 print_scores(lcd);
jamesheavey 0:7d4d2023ed9c 42 // paddles
jamesheavey 0:7d4d2023ed9c 43 _p1.draw(lcd);
jamesheavey 0:7d4d2023ed9c 44 // ball
jamesheavey 0:7d4d2023ed9c 45 _ball.draw(lcd);
jamesheavey 0:7d4d2023ed9c 46 }
jamesheavey 0:7d4d2023ed9c 47
jamesheavey 0:7d4d2023ed9c 48 void PongEngine::update(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 49 {
jamesheavey 0:7d4d2023ed9c 50 check_goal(pad);
jamesheavey 0:7d4d2023ed9c 51 // important to update paddles and ball before checking collisions so can
jamesheavey 0:7d4d2023ed9c 52 // correct for it before updating the display
jamesheavey 0:7d4d2023ed9c 53 _p1.update(_d,_mag);
jamesheavey 0:7d4d2023ed9c 54 _ball.update();
jamesheavey 0:7d4d2023ed9c 55
jamesheavey 0:7d4d2023ed9c 56 check_wall_collision(pad);
jamesheavey 0:7d4d2023ed9c 57 check_paddle_collisions(pad);
jamesheavey 0:7d4d2023ed9c 58 }
jamesheavey 0:7d4d2023ed9c 59
jamesheavey 0:7d4d2023ed9c 60 void PongEngine::check_wall_collision(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 61 {
jamesheavey 0:7d4d2023ed9c 62 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 63 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 64 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 65
jamesheavey 0:7d4d2023ed9c 66 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 67 ball_pos.y = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 68 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 69 // audio feedback
jamesheavey 0:7d4d2023ed9c 70 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 71 }
jamesheavey 0:7d4d2023ed9c 72
jamesheavey 0:7d4d2023ed9c 73 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 74 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 75 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 76 // audio feedback
jamesheavey 0:7d4d2023ed9c 77 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 78 }
jamesheavey 0:7d4d2023ed9c 79
jamesheavey 0:7d4d2023ed9c 80 // check if hit bottom wall
jamesheavey 0:7d4d2023ed9c 81 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 0:7d4d2023ed9c 82 // hit bottom
jamesheavey 0:7d4d2023ed9c 83 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 0:7d4d2023ed9c 84 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 85 // audio feedback
jamesheavey 0:7d4d2023ed9c 86 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 87 }
jamesheavey 0:7d4d2023ed9c 88
jamesheavey 0:7d4d2023ed9c 89 // update ball parameters
jamesheavey 0:7d4d2023ed9c 90 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 91 _ball.set_pos(ball_pos);
jamesheavey 0:7d4d2023ed9c 92 }
jamesheavey 0:7d4d2023ed9c 93
jamesheavey 0:7d4d2023ed9c 94 void PongEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 95 {
jamesheavey 0:7d4d2023ed9c 96 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 97 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 98 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 99
jamesheavey 0:7d4d2023ed9c 100 // check p1 first
jamesheavey 0:7d4d2023ed9c 101 Vector2D p1_pos = _p1.get_pos();
jamesheavey 0:7d4d2023ed9c 102
jamesheavey 0:7d4d2023ed9c 103 // see if ball has hit the paddle by checking for overlaps
jamesheavey 0:7d4d2023ed9c 104 if (
jamesheavey 1:e18615d46071 105 (ball_pos.y >= p1_pos.x) && //left
jamesheavey 1:e18615d46071 106 (ball_pos.y <= p1_pos.x + _paddle_width) && //right
jamesheavey 1:e18615d46071 107 (ball_pos.x >= _p1y) && //bottom
jamesheavey 1:e18615d46071 108 (ball_pos.x <= _p1y + _paddle_height) //top
jamesheavey 0:7d4d2023ed9c 109 ) {
jamesheavey 0:7d4d2023ed9c 110 // if it has, fix position and reflect x velocity
jamesheavey 1:e18615d46071 111 ball_pos.y = _p1y + _paddle_height;
jamesheavey 1:e18615d46071 112 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 113 // audio feedback
jamesheavey 0:7d4d2023ed9c 114 pad.tone(1000.0,0.1);
jamesheavey 0:7d4d2023ed9c 115 }
jamesheavey 0:7d4d2023ed9c 116
jamesheavey 0:7d4d2023ed9c 117 // write new attributes
jamesheavey 0:7d4d2023ed9c 118 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 119 _ball.set_pos(ball_pos);
jamesheavey 0:7d4d2023ed9c 120 }
jamesheavey 0:7d4d2023ed9c 121
jamesheavey 0:7d4d2023ed9c 122 void PongEngine::check_goal(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 123 {
jamesheavey 0:7d4d2023ed9c 124 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 125 // P1 has scored
jamesheavey 0:7d4d2023ed9c 126 if (ball_pos.y > HEIGHT) {
jamesheavey 0:7d4d2023ed9c 127 //lose_screen(); // go to loss screen then initialise again
jamesheavey 0:7d4d2023ed9c 128 _ball.init(_ball_size,_speed);
jamesheavey 0:7d4d2023ed9c 129 pad.tone(1500.0,0.5);
jamesheavey 0:7d4d2023ed9c 130 pad.leds_on();
jamesheavey 0:7d4d2023ed9c 131 wait(0.5);
jamesheavey 0:7d4d2023ed9c 132 pad.leds_off();
jamesheavey 0:7d4d2023ed9c 133 }
jamesheavey 0:7d4d2023ed9c 134 }
jamesheavey 0:7d4d2023ed9c 135
jamesheavey 0:7d4d2023ed9c 136 void PongEngine::print_scores(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 137 {
jamesheavey 0:7d4d2023ed9c 138 // get scores from paddles
jamesheavey 0:7d4d2023ed9c 139 int p1_score = _p1.get_score();
jamesheavey 0:7d4d2023ed9c 140
jamesheavey 0:7d4d2023ed9c 141 // print to LCD i
jamesheavey 0:7d4d2023ed9c 142 char buffer1[14];
jamesheavey 0:7d4d2023ed9c 143 sprintf(buffer1,"%2d",p1_score);
jamesheavey 0:7d4d2023ed9c 144 lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
jamesheavey 0:7d4d2023ed9c 145 }