James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu Apr 18 15:39:11 2019 +0000
Revision:
2:b3ca50f2e85d
Parent:
1:e18615d46071
Child:
3:5719d0fe94ed
Brick class made, need to fix collision detection and enable brick initialisation via array

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