James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 16 18:58:18 2019 +0000
Revision:
0:7d4d2023ed9c
Child:
1:e18615d46071
Game changed to breakout form space invaders (new folder) template initialised, edited paddle, edited direction of movement, edited collision boundaries

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