James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Sat Apr 20 19:25:28 2019 +0000
Revision:
6:7f79f320b827
Parent:
4:6bd488b5b31a
Child:
7:ef162a6232ca
need to work out how to delete a brick once lives == 0, and need to work out how to iteratively create a list of bricks so they can all be created and  checked for collision at once

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 2:b3ca50f2e85d 27
jamesheavey 6:7f79f320b827 28 _brick1.init(3,GAP+1,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
jamesheavey 6:7f79f320b827 29 _brick2.init(16,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 6:7f79f320b827 30 _brick3.init(29,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 6:7f79f320b827 31 _brick4.init(42,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 6:7f79f320b827 32 _brick5.init(55,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 6:7f79f320b827 33 _brick6.init(68,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
jamesheavey 0:7d4d2023ed9c 34 }
jamesheavey 0:7d4d2023ed9c 35
jamesheavey 0:7d4d2023ed9c 36 void PongEngine::read_input(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 37 {
jamesheavey 0:7d4d2023ed9c 38 _d = pad.get_direction();
jamesheavey 0:7d4d2023ed9c 39 _mag = pad.get_mag();
jamesheavey 2:b3ca50f2e85d 40 //_pause = pad.check_event(Gamepad::START_PRESSED); // == false then system("PAUSE") or cin.get()
jamesheavey 0:7d4d2023ed9c 41 }
jamesheavey 0:7d4d2023ed9c 42
jamesheavey 0:7d4d2023ed9c 43 void PongEngine::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 44 {
jamesheavey 0:7d4d2023ed9c 45 // draw the elements in the LCD buffer
jamesheavey 0:7d4d2023ed9c 46 // pitch
jamesheavey 0:7d4d2023ed9c 47 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
jamesheavey 0:7d4d2023ed9c 48 //score
jamesheavey 0:7d4d2023ed9c 49 print_scores(lcd);
jamesheavey 0:7d4d2023ed9c 50 // paddles
jamesheavey 0:7d4d2023ed9c 51 _p1.draw(lcd);
jamesheavey 0:7d4d2023ed9c 52 // ball
jamesheavey 0:7d4d2023ed9c 53 _ball.draw(lcd);
jamesheavey 2:b3ca50f2e85d 54
jamesheavey 6:7f79f320b827 55 _brick1.draw(lcd);
jamesheavey 6:7f79f320b827 56 _brick2.draw(lcd);
jamesheavey 6:7f79f320b827 57 _brick3.draw(lcd);
jamesheavey 6:7f79f320b827 58 _brick4.draw(lcd);
jamesheavey 6:7f79f320b827 59 _brick5.draw(lcd);
jamesheavey 6:7f79f320b827 60 _brick6.draw(lcd);
jamesheavey 6:7f79f320b827 61
jamesheavey 2:b3ca50f2e85d 62
jamesheavey 2:b3ca50f2e85d 63
jamesheavey 2:b3ca50f2e85d 64
jamesheavey 2:b3ca50f2e85d 65
jamesheavey 2:b3ca50f2e85d 66 // for(int i = GAP+1; i <= WIDTH; i+= WIDTH_BRICK + 1){
jamesheavey 2:b3ca50f2e85d 67 // for(int j = GAP+1; j <= (HEIGHT_BRICK + 1)*3; j+= HEIGHT_BRICK + 1){
jamesheavey 6:7f79f320b827 68 // Vector3d grid[i][j]= _brick.init(i, j,WIDTH_BRICK,HEIGHT_BRICK);
jamesheavey 2:b3ca50f2e85d 69 // }
jamesheavey 2:b3ca50f2e85d 70 // }
jamesheavey 0:7d4d2023ed9c 71 }
jamesheavey 0:7d4d2023ed9c 72
jamesheavey 2:b3ca50f2e85d 73
jamesheavey 0:7d4d2023ed9c 74 void PongEngine::update(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 75 {
jamesheavey 0:7d4d2023ed9c 76 check_goal(pad);
jamesheavey 0:7d4d2023ed9c 77 // important to update paddles and ball before checking collisions so can
jamesheavey 0:7d4d2023ed9c 78 // correct for it before updating the display
jamesheavey 0:7d4d2023ed9c 79 _p1.update(_d,_mag);
jamesheavey 0:7d4d2023ed9c 80 _ball.update();
jamesheavey 0:7d4d2023ed9c 81
jamesheavey 0:7d4d2023ed9c 82 check_wall_collision(pad);
jamesheavey 0:7d4d2023ed9c 83 check_paddle_collisions(pad);
jamesheavey 4:6bd488b5b31a 84 check_brick_collisions(pad);
jamesheavey 0:7d4d2023ed9c 85 }
jamesheavey 0:7d4d2023ed9c 86
jamesheavey 0:7d4d2023ed9c 87 void PongEngine::check_wall_collision(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 88 {
jamesheavey 0:7d4d2023ed9c 89 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 90 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 91 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 92
jamesheavey 0:7d4d2023ed9c 93 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 94 ball_pos.y = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 95 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 96 // audio feedback
jamesheavey 0:7d4d2023ed9c 97 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 98 }
jamesheavey 0:7d4d2023ed9c 99
jamesheavey 0:7d4d2023ed9c 100 else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
jamesheavey 0:7d4d2023ed9c 101 ball_pos.x = 1; // bounce off ceiling without going off screen
jamesheavey 0:7d4d2023ed9c 102 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 103 // audio feedback
jamesheavey 0:7d4d2023ed9c 104 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 105 }
jamesheavey 0:7d4d2023ed9c 106
jamesheavey 0:7d4d2023ed9c 107 // check if hit bottom wall
jamesheavey 0:7d4d2023ed9c 108 else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
jamesheavey 0:7d4d2023ed9c 109 // hit bottom
jamesheavey 0:7d4d2023ed9c 110 ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
jamesheavey 0:7d4d2023ed9c 111 ball_velocity.x = -ball_velocity.x;
jamesheavey 0:7d4d2023ed9c 112 // audio feedback
jamesheavey 0:7d4d2023ed9c 113 pad.tone(750.0,0.1);
jamesheavey 0:7d4d2023ed9c 114 }
jamesheavey 0:7d4d2023ed9c 115
jamesheavey 0:7d4d2023ed9c 116 // update ball parameters
jamesheavey 0:7d4d2023ed9c 117 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 118 _ball.set_pos(ball_pos);
jamesheavey 4:6bd488b5b31a 119
jamesheavey 0:7d4d2023ed9c 120 }
jamesheavey 0:7d4d2023ed9c 121
jamesheavey 0:7d4d2023ed9c 122 void PongEngine::check_paddle_collisions(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 123 {
jamesheavey 0:7d4d2023ed9c 124 // read current ball attributes
jamesheavey 0:7d4d2023ed9c 125 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 126 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 0:7d4d2023ed9c 127
jamesheavey 0:7d4d2023ed9c 128 // check p1 first
jamesheavey 0:7d4d2023ed9c 129 Vector2D p1_pos = _p1.get_pos();
jamesheavey 0:7d4d2023ed9c 130
jamesheavey 0:7d4d2023ed9c 131 // see if ball has hit the paddle by checking for overlaps
jamesheavey 0:7d4d2023ed9c 132 if (
jamesheavey 4:6bd488b5b31a 133 (ball_pos.x >= p1_pos.x) && //left
jamesheavey 4:6bd488b5b31a 134 (ball_pos.x <= p1_pos.x + _paddle_width) && //right
jamesheavey 4:6bd488b5b31a 135 (ball_pos.y >= _p1y) && //bottom
jamesheavey 4:6bd488b5b31a 136 (ball_pos.y <= _p1y + _paddle_height) //top
jamesheavey 4:6bd488b5b31a 137 ) { // 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 138 // if it has, fix position and reflect x velocity
jamesheavey 1:e18615d46071 139 ball_pos.y = _p1y + _paddle_height;
jamesheavey 1:e18615d46071 140 ball_velocity.y = -ball_velocity.y;
jamesheavey 0:7d4d2023ed9c 141 // audio feedback
jamesheavey 0:7d4d2023ed9c 142 pad.tone(1000.0,0.1);
jamesheavey 0:7d4d2023ed9c 143 }
jamesheavey 0:7d4d2023ed9c 144
jamesheavey 0:7d4d2023ed9c 145 // write new attributes
jamesheavey 0:7d4d2023ed9c 146 _ball.set_velocity(ball_velocity);
jamesheavey 0:7d4d2023ed9c 147 _ball.set_pos(ball_pos);
jamesheavey 0:7d4d2023ed9c 148 }
jamesheavey 0:7d4d2023ed9c 149
jamesheavey 4:6bd488b5b31a 150 void PongEngine::check_brick_collisions(Gamepad &pad)
jamesheavey 4:6bd488b5b31a 151 {
jamesheavey 4:6bd488b5b31a 152 // read current ball attributes
jamesheavey 4:6bd488b5b31a 153 Vector2D ball_pos = _ball.get_pos();
jamesheavey 4:6bd488b5b31a 154 Vector2D ball_velocity = _ball.get_velocity();
jamesheavey 4:6bd488b5b31a 155
jamesheavey 4:6bd488b5b31a 156 // check p1 first
jamesheavey 6:7f79f320b827 157 Vector2D _brick1_pos = _brick1.get_pos();
jamesheavey 4:6bd488b5b31a 158
jamesheavey 4:6bd488b5b31a 159 // see if ball has hit the paddle by checking for overlaps
jamesheavey 4:6bd488b5b31a 160 if (
jamesheavey 6:7f79f320b827 161 (ball_pos.x >= _brick1_pos.x) && //left
jamesheavey 6:7f79f320b827 162 (ball_pos.x <= _brick1_pos.x + WIDTH_BRICK) && //right
jamesheavey 6:7f79f320b827 163 (ball_pos.y >= _brick1_pos.y) && //bottom
jamesheavey 6:7f79f320b827 164 (ball_pos.y <= _brick1_pos.y + HEIGHT_BRICK) //top
jamesheavey 4:6bd488b5b31a 165 ) { // 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 166 // if it has, fix position and reflect x velocity
jamesheavey 6:7f79f320b827 167 ball_pos.y = _brick1_pos.y;
jamesheavey 4:6bd488b5b31a 168 ball_velocity.y = -ball_velocity.y;
jamesheavey 4:6bd488b5b31a 169 // audio feedback
jamesheavey 4:6bd488b5b31a 170 pad.tone(1000.0,0.1);
jamesheavey 4:6bd488b5b31a 171 }
jamesheavey 4:6bd488b5b31a 172
jamesheavey 4:6bd488b5b31a 173 // write new attributes
jamesheavey 4:6bd488b5b31a 174 _ball.set_velocity(ball_velocity);
jamesheavey 4:6bd488b5b31a 175 _ball.set_pos(ball_pos);
jamesheavey 6:7f79f320b827 176 _brick1.hit();
jamesheavey 4:6bd488b5b31a 177 }
jamesheavey 4:6bd488b5b31a 178
jamesheavey 0:7d4d2023ed9c 179 void PongEngine::check_goal(Gamepad &pad)
jamesheavey 0:7d4d2023ed9c 180 {
jamesheavey 0:7d4d2023ed9c 181 Vector2D ball_pos = _ball.get_pos();
jamesheavey 0:7d4d2023ed9c 182 // P1 has scored
jamesheavey 0:7d4d2023ed9c 183 if (ball_pos.y > HEIGHT) {
jamesheavey 0:7d4d2023ed9c 184 //lose_screen(); // go to loss screen then initialise again
jamesheavey 0:7d4d2023ed9c 185 _ball.init(_ball_size,_speed);
jamesheavey 0:7d4d2023ed9c 186 pad.tone(1500.0,0.5);
jamesheavey 0:7d4d2023ed9c 187 pad.leds_on();
jamesheavey 0:7d4d2023ed9c 188 wait(0.5);
jamesheavey 0:7d4d2023ed9c 189 pad.leds_off();
jamesheavey 0:7d4d2023ed9c 190 }
jamesheavey 0:7d4d2023ed9c 191 }
jamesheavey 0:7d4d2023ed9c 192
jamesheavey 0:7d4d2023ed9c 193 void PongEngine::print_scores(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 194 {
jamesheavey 0:7d4d2023ed9c 195 // get scores from paddles
jamesheavey 0:7d4d2023ed9c 196 int p1_score = _p1.get_score();
jamesheavey 0:7d4d2023ed9c 197
jamesheavey 0:7d4d2023ed9c 198 // print to LCD i
jamesheavey 0:7d4d2023ed9c 199 char buffer1[14];
jamesheavey 0:7d4d2023ed9c 200 sprintf(buffer1,"%2d",p1_score);
jamesheavey 3:5719d0fe94ed 201 //lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
jamesheavey 0:7d4d2023ed9c 202 }