Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BreakoutEngine/BreakoutEngine.cpp@36:cb73014d3a99, 2019-04-25 (annotated)
- Committer:
- jamesheavey
- Date:
- Thu Apr 25 20:50:49 2019 +0000
- Revision:
- 36:cb73014d3a99
- Parent:
- 35:3a614d539a54
- Child:
- 37:dd1538ae6534
laser workkkks but cant set the position yet as the objects in a list are different to the ones outside it apparantly
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jamesheavey | 27:1b5038b0a7a2 | 1 | #include "BreakoutEngine.h" |
| jamesheavey | 27:1b5038b0a7a2 | 2 | |
| jamesheavey | 27:1b5038b0a7a2 | 3 | |
| jamesheavey | 27:1b5038b0a7a2 | 4 | |
| jamesheavey | 27:1b5038b0a7a2 | 5 | BreakoutEngine::BreakoutEngine() |
| jamesheavey | 27:1b5038b0a7a2 | 6 | { |
| jamesheavey | 27:1b5038b0a7a2 | 7 | |
| jamesheavey | 27:1b5038b0a7a2 | 8 | } |
| jamesheavey | 27:1b5038b0a7a2 | 9 | |
| jamesheavey | 27:1b5038b0a7a2 | 10 | BreakoutEngine::~BreakoutEngine() |
| jamesheavey | 27:1b5038b0a7a2 | 11 | { |
| jamesheavey | 27:1b5038b0a7a2 | 12 | |
| jamesheavey | 27:1b5038b0a7a2 | 13 | } |
| jamesheavey | 27:1b5038b0a7a2 | 14 | |
| jamesheavey | 27:1b5038b0a7a2 | 15 | void BreakoutEngine::init(int paddle_width,int paddle_height,int ball_size,int speed) |
| jamesheavey | 27:1b5038b0a7a2 | 16 | { |
| jamesheavey | 27:1b5038b0a7a2 | 17 | // initialise the game parameters |
| jamesheavey | 27:1b5038b0a7a2 | 18 | _paddle_width = paddle_width; |
| jamesheavey | 27:1b5038b0a7a2 | 19 | _paddle_height = paddle_height; |
| jamesheavey | 27:1b5038b0a7a2 | 20 | _ball_size = ball_size; |
| jamesheavey | 27:1b5038b0a7a2 | 21 | _speed = speed; |
| jamesheavey | 27:1b5038b0a7a2 | 22 | _number_left = 18; |
| jamesheavey | 30:e3d2f0ca416f | 23 | _index = 0; |
| jamesheavey | 27:1b5038b0a7a2 | 24 | |
| jamesheavey | 27:1b5038b0a7a2 | 25 | // y position on screen - WIDTH is defined in N5110.h |
| jamesheavey | 27:1b5038b0a7a2 | 26 | _p1y = HEIGHT - GAP; |
| jamesheavey | 27:1b5038b0a7a2 | 27 | |
| jamesheavey | 27:1b5038b0a7a2 | 28 | // puts paddles and ball in middle |
| jamesheavey | 27:1b5038b0a7a2 | 29 | _p1.init(_p1y,_paddle_height,_paddle_width); |
| jamesheavey | 27:1b5038b0a7a2 | 30 | _ball.init(_ball_size,_speed); |
| jamesheavey | 27:1b5038b0a7a2 | 31 | |
| jamesheavey | 27:1b5038b0a7a2 | 32 | _brick11.init(3,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these |
| jamesheavey | 27:1b5038b0a7a2 | 33 | _brick12.init(16,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 34 | _brick13.init(29,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 35 | _brick14.init(42,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 36 | _brick15.init(55,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 37 | _brick16.init(68,GAP+1,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 38 | |
| jamesheavey | 27:1b5038b0a7a2 | 39 | _brick21.init(3,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these |
| jamesheavey | 27:1b5038b0a7a2 | 40 | _brick22.init(16,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 41 | _brick23.init(29,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 42 | _brick24.init(42,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 43 | _brick25.init(55,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 44 | _brick26.init(68,GAP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 45 | |
| jamesheavey | 27:1b5038b0a7a2 | 46 | _brick31.init(3,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); // need to figure out how to make a list of these |
| jamesheavey | 27:1b5038b0a7a2 | 47 | _brick32.init(16,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 48 | _brick33.init(29,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 49 | _brick34.init(42,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 50 | _brick35.init(55,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 51 | _brick36.init(68,GAP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH); |
| jamesheavey | 27:1b5038b0a7a2 | 52 | |
| jamesheavey | 34:07ded1f83c59 | 53 | listofBricks.push_back(_brick11); //maybe be able yo iterate through and this.draw(), maybe useful if i figure out how to delete objects aswell |
| jamesheavey | 34:07ded1f83c59 | 54 | listofBricks.push_back(_brick12); |
| jamesheavey | 34:07ded1f83c59 | 55 | listofBricks.push_back(_brick13); |
| jamesheavey | 34:07ded1f83c59 | 56 | listofBricks.push_back(_brick14); |
| jamesheavey | 34:07ded1f83c59 | 57 | listofBricks.push_back(_brick15); |
| jamesheavey | 34:07ded1f83c59 | 58 | listofBricks.push_back(_brick16); |
| jamesheavey | 34:07ded1f83c59 | 59 | listofBricks.push_back(_brick21); |
| jamesheavey | 34:07ded1f83c59 | 60 | listofBricks.push_back(_brick22); |
| jamesheavey | 34:07ded1f83c59 | 61 | listofBricks.push_back(_brick23); |
| jamesheavey | 34:07ded1f83c59 | 62 | listofBricks.push_back(_brick24); |
| jamesheavey | 34:07ded1f83c59 | 63 | listofBricks.push_back(_brick25); |
| jamesheavey | 34:07ded1f83c59 | 64 | listofBricks.push_back(_brick26); |
| jamesheavey | 34:07ded1f83c59 | 65 | listofBricks.push_back(_brick31); |
| jamesheavey | 34:07ded1f83c59 | 66 | listofBricks.push_back(_brick32); |
| jamesheavey | 34:07ded1f83c59 | 67 | listofBricks.push_back(_brick33); |
| jamesheavey | 34:07ded1f83c59 | 68 | listofBricks.push_back(_brick34); |
| jamesheavey | 34:07ded1f83c59 | 69 | listofBricks.push_back(_brick35); |
| jamesheavey | 34:07ded1f83c59 | 70 | listofBricks.push_back(_brick36); |
| jamesheavey | 34:07ded1f83c59 | 71 | |
| jamesheavey | 30:e3d2f0ca416f | 72 | _laser1.init(-10); |
| jamesheavey | 30:e3d2f0ca416f | 73 | _laser2.init(-10); |
| jamesheavey | 30:e3d2f0ca416f | 74 | _laser3.init(-10); |
| jamesheavey | 35:3a614d539a54 | 75 | |
| jamesheavey | 35:3a614d539a54 | 76 | listofLasers.push_back(_laser1); |
| jamesheavey | 35:3a614d539a54 | 77 | listofLasers.push_back(_laser2); |
| jamesheavey | 35:3a614d539a54 | 78 | listofLasers.push_back(_laser3); |
| jamesheavey | 27:1b5038b0a7a2 | 79 | } |
| jamesheavey | 27:1b5038b0a7a2 | 80 | |
| jamesheavey | 27:1b5038b0a7a2 | 81 | void BreakoutEngine::read_input(Gamepad &pad, bool x) |
| jamesheavey | 27:1b5038b0a7a2 | 82 | { |
| jamesheavey | 27:1b5038b0a7a2 | 83 | _d = pad.get_direction(); |
| jamesheavey | 27:1b5038b0a7a2 | 84 | _mag = pad.get_mag(); |
| jamesheavey | 27:1b5038b0a7a2 | 85 | |
| jamesheavey | 27:1b5038b0a7a2 | 86 | if (x == true) { |
| jamesheavey | 27:1b5038b0a7a2 | 87 | _p1.set_tilt(); |
| jamesheavey | 27:1b5038b0a7a2 | 88 | } |
| jamesheavey | 27:1b5038b0a7a2 | 89 | else { |
| jamesheavey | 27:1b5038b0a7a2 | 90 | _p1.set_joy(); |
| jamesheavey | 27:1b5038b0a7a2 | 91 | } |
| jamesheavey | 31:516d4e27765a | 92 | if (pad.check_event(Gamepad::B_PRESSED)) { // max of 3 lasers on screen at once |
| jamesheavey | 29:5168318d3e88 | 93 | |
| jamesheavey | 29:5168318d3e88 | 94 | Vector2D p_pos = _p1.get_pos(); |
| jamesheavey | 30:e3d2f0ca416f | 95 | switch(_index){ |
| jamesheavey | 30:e3d2f0ca416f | 96 | case 0: |
| jamesheavey | 36:cb73014d3a99 | 97 | _laser1.set_posx(p_pos.x+7); |
| jamesheavey | 36:cb73014d3a99 | 98 | _laser1.set_posy(p_pos.y); |
| jamesheavey | 30:e3d2f0ca416f | 99 | inc_index(); |
| jamesheavey | 30:e3d2f0ca416f | 100 | break; |
| jamesheavey | 30:e3d2f0ca416f | 101 | case 1: |
| jamesheavey | 36:cb73014d3a99 | 102 | _laser2.set_posx(p_pos.x+7); |
| jamesheavey | 36:cb73014d3a99 | 103 | _laser2.set_posy(p_pos.y); |
| jamesheavey | 30:e3d2f0ca416f | 104 | inc_index(); |
| jamesheavey | 30:e3d2f0ca416f | 105 | break; |
| jamesheavey | 30:e3d2f0ca416f | 106 | case 2: |
| jamesheavey | 36:cb73014d3a99 | 107 | _laser3.set_posx(p_pos.x+7); |
| jamesheavey | 36:cb73014d3a99 | 108 | _laser3.set_posy(p_pos.y); |
| jamesheavey | 30:e3d2f0ca416f | 109 | reset_index(); |
| jamesheavey | 30:e3d2f0ca416f | 110 | break; |
| jamesheavey | 30:e3d2f0ca416f | 111 | } |
| jamesheavey | 29:5168318d3e88 | 112 | } |
| jamesheavey | 27:1b5038b0a7a2 | 113 | } |
| jamesheavey | 27:1b5038b0a7a2 | 114 | |
| jamesheavey | 30:e3d2f0ca416f | 115 | void BreakoutEngine::inc_index() { |
| jamesheavey | 30:e3d2f0ca416f | 116 | _index ++; |
| jamesheavey | 30:e3d2f0ca416f | 117 | } |
| jamesheavey | 30:e3d2f0ca416f | 118 | |
| jamesheavey | 30:e3d2f0ca416f | 119 | void BreakoutEngine::reset_index() { |
| jamesheavey | 30:e3d2f0ca416f | 120 | _index = 0; |
| jamesheavey | 30:e3d2f0ca416f | 121 | } |
| jamesheavey | 30:e3d2f0ca416f | 122 | |
| jamesheavey | 27:1b5038b0a7a2 | 123 | void BreakoutEngine::draw(N5110 &lcd) |
| jamesheavey | 27:1b5038b0a7a2 | 124 | { |
| jamesheavey | 27:1b5038b0a7a2 | 125 | // draw the elements in the LCD buffer |
| jamesheavey | 27:1b5038b0a7a2 | 126 | // pitch |
| jamesheavey | 27:1b5038b0a7a2 | 127 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
| jamesheavey | 27:1b5038b0a7a2 | 128 | //score |
| jamesheavey | 27:1b5038b0a7a2 | 129 | //print_scores(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 130 | // paddles |
| jamesheavey | 27:1b5038b0a7a2 | 131 | _p1.draw(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 132 | // ball |
| jamesheavey | 27:1b5038b0a7a2 | 133 | _ball.draw(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 134 | |
| jamesheavey | 27:1b5038b0a7a2 | 135 | //print_scores(lcd); |
| jamesheavey | 27:1b5038b0a7a2 | 136 | |
| jamesheavey | 34:07ded1f83c59 | 137 | for (it = listofBricks.begin(); it != listofBricks.end(); ++it){ |
| jamesheavey | 34:07ded1f83c59 | 138 | it->draw(lcd); |
| jamesheavey | 34:07ded1f83c59 | 139 | } |
| jamesheavey | 35:3a614d539a54 | 140 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){ |
| jamesheavey | 35:3a614d539a54 | 141 | it_L->draw(lcd); |
| jamesheavey | 35:3a614d539a54 | 142 | } |
| jamesheavey | 27:1b5038b0a7a2 | 143 | |
| jamesheavey | 27:1b5038b0a7a2 | 144 | } |
| jamesheavey | 27:1b5038b0a7a2 | 145 | |
| jamesheavey | 27:1b5038b0a7a2 | 146 | |
| jamesheavey | 27:1b5038b0a7a2 | 147 | void BreakoutEngine::update(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 148 | { |
| jamesheavey | 27:1b5038b0a7a2 | 149 | check_goal(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 150 | // important to update paddles and ball before checking collisions so can |
| jamesheavey | 27:1b5038b0a7a2 | 151 | // correct for it before updating the display |
| jamesheavey | 27:1b5038b0a7a2 | 152 | _p1.update(_d,_mag); |
| jamesheavey | 27:1b5038b0a7a2 | 153 | _ball.update(); |
| jamesheavey | 35:3a614d539a54 | 154 | |
| jamesheavey | 35:3a614d539a54 | 155 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){ |
| jamesheavey | 35:3a614d539a54 | 156 | it_L->update(); |
| jamesheavey | 35:3a614d539a54 | 157 | } |
| jamesheavey | 35:3a614d539a54 | 158 | |
| jamesheavey | 27:1b5038b0a7a2 | 159 | lives_leds(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 160 | |
| jamesheavey | 27:1b5038b0a7a2 | 161 | check_wall_collision(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 162 | check_paddle_collisions(pad); |
| jamesheavey | 27:1b5038b0a7a2 | 163 | check_brick_collisions(pad); |
| jamesheavey | 36:cb73014d3a99 | 164 | check_laser_collisions(pad); |
| jamesheavey | 35:3a614d539a54 | 165 | |
| jamesheavey | 35:3a614d539a54 | 166 | |
| jamesheavey | 27:1b5038b0a7a2 | 167 | } |
| jamesheavey | 27:1b5038b0a7a2 | 168 | |
| jamesheavey | 27:1b5038b0a7a2 | 169 | void BreakoutEngine::lives_leds(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 170 | { |
| jamesheavey | 27:1b5038b0a7a2 | 171 | if (_p1.get_lives() == 0) { |
| jamesheavey | 27:1b5038b0a7a2 | 172 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 173 | } |
| jamesheavey | 27:1b5038b0a7a2 | 174 | |
| jamesheavey | 27:1b5038b0a7a2 | 175 | else if (_p1.get_lives() == 1) { |
| jamesheavey | 27:1b5038b0a7a2 | 176 | //turn leftmost led on |
| jamesheavey | 27:1b5038b0a7a2 | 177 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 178 | pad.led(1,1); |
| jamesheavey | 27:1b5038b0a7a2 | 179 | } |
| jamesheavey | 27:1b5038b0a7a2 | 180 | |
| jamesheavey | 27:1b5038b0a7a2 | 181 | else if (_p1.get_lives() == 2) { |
| jamesheavey | 27:1b5038b0a7a2 | 182 | //turn leftmost led on |
| jamesheavey | 27:1b5038b0a7a2 | 183 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 184 | pad.led(1,1); |
| jamesheavey | 27:1b5038b0a7a2 | 185 | pad.led(2,1); |
| jamesheavey | 27:1b5038b0a7a2 | 186 | } |
| jamesheavey | 27:1b5038b0a7a2 | 187 | |
| jamesheavey | 27:1b5038b0a7a2 | 188 | else if (_p1.get_lives() == 3) { |
| jamesheavey | 27:1b5038b0a7a2 | 189 | //turn leftmost led on |
| jamesheavey | 27:1b5038b0a7a2 | 190 | pad.leds_off(); |
| jamesheavey | 27:1b5038b0a7a2 | 191 | pad.led(1,1); |
| jamesheavey | 27:1b5038b0a7a2 | 192 | pad.led(2,1); |
| jamesheavey | 27:1b5038b0a7a2 | 193 | pad.led(3,1); |
| jamesheavey | 27:1b5038b0a7a2 | 194 | } |
| jamesheavey | 27:1b5038b0a7a2 | 195 | |
| jamesheavey | 27:1b5038b0a7a2 | 196 | else if (_p1.get_lives() == 4) { |
| jamesheavey | 27:1b5038b0a7a2 | 197 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 198 | pad.led(5,0); |
| jamesheavey | 27:1b5038b0a7a2 | 199 | pad.led(6,0); |
| jamesheavey | 27:1b5038b0a7a2 | 200 | } |
| jamesheavey | 27:1b5038b0a7a2 | 201 | |
| jamesheavey | 27:1b5038b0a7a2 | 202 | else if (_p1.get_lives() == 5) { |
| jamesheavey | 27:1b5038b0a7a2 | 203 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 204 | pad.led(6,0); |
| jamesheavey | 27:1b5038b0a7a2 | 205 | } |
| jamesheavey | 27:1b5038b0a7a2 | 206 | |
| jamesheavey | 27:1b5038b0a7a2 | 207 | else if (_p1.get_lives() == 6) { |
| jamesheavey | 27:1b5038b0a7a2 | 208 | pad.leds_on(); |
| jamesheavey | 27:1b5038b0a7a2 | 209 | } |
| jamesheavey | 27:1b5038b0a7a2 | 210 | } |
| jamesheavey | 27:1b5038b0a7a2 | 211 | |
| jamesheavey | 27:1b5038b0a7a2 | 212 | int BreakoutEngine::get_lives() { |
| jamesheavey | 27:1b5038b0a7a2 | 213 | return _p1.get_lives(); |
| jamesheavey | 27:1b5038b0a7a2 | 214 | } |
| jamesheavey | 27:1b5038b0a7a2 | 215 | |
| jamesheavey | 27:1b5038b0a7a2 | 216 | void BreakoutEngine::check_wall_collision(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 217 | { |
| jamesheavey | 27:1b5038b0a7a2 | 218 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 219 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 220 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 27:1b5038b0a7a2 | 221 | |
| jamesheavey | 27:1b5038b0a7a2 | 222 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 27:1b5038b0a7a2 | 223 | ball_pos.y = 1; // bounce off ceiling without going off screen |
| jamesheavey | 27:1b5038b0a7a2 | 224 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 27:1b5038b0a7a2 | 225 | // audio feedback |
| jamesheavey | 27:1b5038b0a7a2 | 226 | pad.tone(750.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 227 | } |
| jamesheavey | 27:1b5038b0a7a2 | 228 | |
| jamesheavey | 27:1b5038b0a7a2 | 229 | else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary |
| jamesheavey | 27:1b5038b0a7a2 | 230 | ball_pos.x = 1; // bounce off ceiling without going off screen |
| jamesheavey | 27:1b5038b0a7a2 | 231 | ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 232 | // audio feedback |
| jamesheavey | 27:1b5038b0a7a2 | 233 | pad.tone(750.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 234 | } |
| jamesheavey | 27:1b5038b0a7a2 | 235 | |
| jamesheavey | 27:1b5038b0a7a2 | 236 | // check if hit bottom wall |
| jamesheavey | 27:1b5038b0a7a2 | 237 | else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47 |
| jamesheavey | 27:1b5038b0a7a2 | 238 | // hit bottom |
| jamesheavey | 27:1b5038b0a7a2 | 239 | ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen |
| jamesheavey | 27:1b5038b0a7a2 | 240 | ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 241 | // audio feedback |
| jamesheavey | 27:1b5038b0a7a2 | 242 | pad.tone(750.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 243 | } |
| jamesheavey | 27:1b5038b0a7a2 | 244 | |
| jamesheavey | 27:1b5038b0a7a2 | 245 | // update ball parameters |
| jamesheavey | 27:1b5038b0a7a2 | 246 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 27:1b5038b0a7a2 | 247 | _ball.set_pos(ball_pos); |
| jamesheavey | 27:1b5038b0a7a2 | 248 | |
| jamesheavey | 27:1b5038b0a7a2 | 249 | } |
| jamesheavey | 27:1b5038b0a7a2 | 250 | |
| jamesheavey | 27:1b5038b0a7a2 | 251 | void BreakoutEngine::check_paddle_collisions(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 252 | { |
| jamesheavey | 27:1b5038b0a7a2 | 253 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 254 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 255 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 27:1b5038b0a7a2 | 256 | |
| jamesheavey | 27:1b5038b0a7a2 | 257 | // check p1 first |
| jamesheavey | 27:1b5038b0a7a2 | 258 | Vector2D p1_pos = _p1.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 259 | |
| jamesheavey | 27:1b5038b0a7a2 | 260 | // see if ball has hit the paddle by checking for overlaps |
| jamesheavey | 27:1b5038b0a7a2 | 261 | if ( |
| jamesheavey | 27:1b5038b0a7a2 | 262 | (ball_pos.x >= p1_pos.x) && //left |
| jamesheavey | 27:1b5038b0a7a2 | 263 | (ball_pos.x <= p1_pos.x + _paddle_width) && //right |
| jamesheavey | 27:1b5038b0a7a2 | 264 | (ball_pos.y >= _p1y) && //bottom |
| jamesheavey | 27:1b5038b0a7a2 | 265 | (ball_pos.y <= _p1y + _paddle_height) //top |
| jamesheavey | 27:1b5038b0a7a2 | 266 | ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball) |
| jamesheavey | 27:1b5038b0a7a2 | 267 | // if it has, fix position and reflect x velocity |
| jamesheavey | 27:1b5038b0a7a2 | 268 | pad.tone(1000.0,0.1); |
| jamesheavey | 27:1b5038b0a7a2 | 269 | ball_pos.y = _p1y + _paddle_height - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 270 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 27:1b5038b0a7a2 | 271 | |
| jamesheavey | 27:1b5038b0a7a2 | 272 | // if (ball_pos.x == p1_pos.x + PADDLE_WIDTH/2) { // check ballxpos in relation to paddle xpos. translate the distance from the centre to an angle between 30 and 60 degrees in that direction |
| jamesheavey | 27:1b5038b0a7a2 | 273 | // ball_pos.y = _p1y + _paddle_height - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 274 | // ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 27:1b5038b0a7a2 | 275 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 276 | // else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) { |
| jamesheavey | 27:1b5038b0a7a2 | 277 | // float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30; //converts the distance from the centre to an angle between 30 and 60 |
| jamesheavey | 27:1b5038b0a7a2 | 278 | // if (ball_velocity.x > 0) { |
| jamesheavey | 27:1b5038b0a7a2 | 279 | // ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 280 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 281 | // ball_pos.y = _p1y + _paddle_heigh - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 282 | // ball_velocity.y = -tan(ang); |
| jamesheavey | 27:1b5038b0a7a2 | 283 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 284 | // else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) { |
| jamesheavey | 27:1b5038b0a7a2 | 285 | // float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30; //converts the distance from the centre to an angle between 30 and 60 |
| jamesheavey | 27:1b5038b0a7a2 | 286 | // if (ball_velocity.x < 0) { |
| jamesheavey | 27:1b5038b0a7a2 | 287 | // ball_velocity.x = -ball_velocity.x; |
| jamesheavey | 27:1b5038b0a7a2 | 288 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 289 | // ball_pos.y = _p1y + _paddle_height - 1; |
| jamesheavey | 27:1b5038b0a7a2 | 290 | // ball_velocity.y = -tan(ang); |
| jamesheavey | 27:1b5038b0a7a2 | 291 | // } |
| jamesheavey | 27:1b5038b0a7a2 | 292 | } |
| jamesheavey | 27:1b5038b0a7a2 | 293 | |
| jamesheavey | 27:1b5038b0a7a2 | 294 | // write new attributes |
| jamesheavey | 27:1b5038b0a7a2 | 295 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 27:1b5038b0a7a2 | 296 | _ball.set_pos(ball_pos); |
| jamesheavey | 27:1b5038b0a7a2 | 297 | } |
| jamesheavey | 27:1b5038b0a7a2 | 298 | |
| jamesheavey | 27:1b5038b0a7a2 | 299 | void BreakoutEngine::check_brick_collisions(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 300 | { |
| jamesheavey | 27:1b5038b0a7a2 | 301 | // read current ball attributes |
| jamesheavey | 27:1b5038b0a7a2 | 302 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 303 | Vector2D ball_velocity = _ball.get_velocity(); |
| jamesheavey | 27:1b5038b0a7a2 | 304 | |
| jamesheavey | 34:07ded1f83c59 | 305 | for (it = listofBricks.begin(); it != listofBricks.end(); ++it){ |
| jamesheavey | 34:07ded1f83c59 | 306 | if ( |
| jamesheavey | 34:07ded1f83c59 | 307 | (ball_pos.x >= it -> get_x()) && //left |
| jamesheavey | 34:07ded1f83c59 | 308 | (ball_pos.x <= it -> get_x() + BRICK_WIDTH) && //right |
| jamesheavey | 34:07ded1f83c59 | 309 | (ball_pos.y >= it -> get_y()) && //bottom |
| jamesheavey | 34:07ded1f83c59 | 310 | (ball_pos.y <= it -> get_y() + BRICK_HEIGHT) //top |
| jamesheavey | 34:07ded1f83c59 | 311 | ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball) |
| jamesheavey | 34:07ded1f83c59 | 312 | // if it has, fix position and reflect x velocity |
| jamesheavey | 34:07ded1f83c59 | 313 | ball_pos.y = it -> get_y() + BRICK_HEIGHT; |
| jamesheavey | 34:07ded1f83c59 | 314 | ball_velocity.y = -ball_velocity.y; |
| jamesheavey | 34:07ded1f83c59 | 315 | // audio feedback |
| jamesheavey | 34:07ded1f83c59 | 316 | pad.tone(1000.0,0.1); |
| jamesheavey | 34:07ded1f83c59 | 317 | it -> hit(); |
| jamesheavey | 34:07ded1f83c59 | 318 | it -> hit(); |
| jamesheavey | 34:07ded1f83c59 | 319 | //delete _brick11; |
| jamesheavey | 34:07ded1f83c59 | 320 | it -> set_posx(-100); |
| jamesheavey | 34:07ded1f83c59 | 321 | it -> set_posy(-100); |
| jamesheavey | 34:07ded1f83c59 | 322 | one_less(); |
| jamesheavey | 34:07ded1f83c59 | 323 | } |
| jamesheavey | 27:1b5038b0a7a2 | 324 | } |
| jamesheavey | 27:1b5038b0a7a2 | 325 | // write new attributes |
| jamesheavey | 27:1b5038b0a7a2 | 326 | _ball.set_velocity(ball_velocity); |
| jamesheavey | 27:1b5038b0a7a2 | 327 | _ball.set_pos(ball_pos); |
| jamesheavey | 27:1b5038b0a7a2 | 328 | } |
| jamesheavey | 36:cb73014d3a99 | 329 | |
| jamesheavey | 32:4dba7a85dbb2 | 330 | void BreakoutEngine::check_laser_collisions(Gamepad &pad) |
| jamesheavey | 32:4dba7a85dbb2 | 331 | { |
| jamesheavey | 32:4dba7a85dbb2 | 332 | // read current ball attributes |
| jamesheavey | 34:07ded1f83c59 | 333 | // check p1 first |
| jamesheavey | 36:cb73014d3a99 | 334 | for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){ |
| jamesheavey | 36:cb73014d3a99 | 335 | for (it = listofBricks.begin(); it != listofBricks.end(); ++it){ |
| jamesheavey | 36:cb73014d3a99 | 336 | if ( |
| jamesheavey | 36:cb73014d3a99 | 337 | (it_L -> get_x() >= it -> get_x()) && //left |
| jamesheavey | 36:cb73014d3a99 | 338 | (it_L -> get_x() <= it -> get_x() + BRICK_WIDTH) && //right |
| jamesheavey | 36:cb73014d3a99 | 339 | (it_L -> get_y() >= it -> get_y()) && //bottom |
| jamesheavey | 36:cb73014d3a99 | 340 | (it_L -> get_y()<= it -> get_y() + BRICK_HEIGHT) //top |
| jamesheavey | 36:cb73014d3a99 | 341 | ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball) |
| jamesheavey | 36:cb73014d3a99 | 342 | // if it has, fix position and reflect x velocity |
| jamesheavey | 36:cb73014d3a99 | 343 | it_L -> set_posx(-100); |
| jamesheavey | 36:cb73014d3a99 | 344 | // audio feedback |
| jamesheavey | 36:cb73014d3a99 | 345 | pad.tone(1000.0,0.1); |
| jamesheavey | 36:cb73014d3a99 | 346 | if(it->hit() == true) { |
| jamesheavey | 36:cb73014d3a99 | 347 | it -> set_posx(-100); |
| jamesheavey | 36:cb73014d3a99 | 348 | it -> set_posy(-100); |
| jamesheavey | 36:cb73014d3a99 | 349 | one_less(); |
| jamesheavey | 36:cb73014d3a99 | 350 | } |
| jamesheavey | 36:cb73014d3a99 | 351 | } |
| jamesheavey | 34:07ded1f83c59 | 352 | } |
| jamesheavey | 34:07ded1f83c59 | 353 | } |
| jamesheavey | 36:cb73014d3a99 | 354 | } |
| jamesheavey | 32:4dba7a85dbb2 | 355 | |
| jamesheavey | 27:1b5038b0a7a2 | 356 | bool BreakoutEngine::check_goal(Gamepad &pad) |
| jamesheavey | 27:1b5038b0a7a2 | 357 | { |
| jamesheavey | 27:1b5038b0a7a2 | 358 | Vector2D ball_pos = _ball.get_pos(); |
| jamesheavey | 27:1b5038b0a7a2 | 359 | // P1 has scored |
| jamesheavey | 27:1b5038b0a7a2 | 360 | if (ball_pos.y > HEIGHT) { |
| jamesheavey | 27:1b5038b0a7a2 | 361 | _p1.lose_life(); |
| jamesheavey | 27:1b5038b0a7a2 | 362 | //lose_screen(); // go to loss screen then initialise again |
| jamesheavey | 27:1b5038b0a7a2 | 363 | |
| jamesheavey | 27:1b5038b0a7a2 | 364 | _ball.init(_ball_size,_speed); |
| jamesheavey | 27:1b5038b0a7a2 | 365 | pad.tone(1500.0,0.5); |
| jamesheavey | 27:1b5038b0a7a2 | 366 | return true; |
| jamesheavey | 27:1b5038b0a7a2 | 367 | } |
| jamesheavey | 27:1b5038b0a7a2 | 368 | else { |
| jamesheavey | 27:1b5038b0a7a2 | 369 | return false; |
| jamesheavey | 27:1b5038b0a7a2 | 370 | } |
| jamesheavey | 27:1b5038b0a7a2 | 371 | } |
| jamesheavey | 27:1b5038b0a7a2 | 372 | |
| jamesheavey | 27:1b5038b0a7a2 | 373 | |
| jamesheavey | 27:1b5038b0a7a2 | 374 | |
| jamesheavey | 34:07ded1f83c59 | 375 | void BreakoutEngine::print_scores(N5110 &lcd) //maybe add to oneless |
| jamesheavey | 27:1b5038b0a7a2 | 376 | { |
| jamesheavey | 27:1b5038b0a7a2 | 377 | // get scores from paddles |
| jamesheavey | 27:1b5038b0a7a2 | 378 | int p1_score = _p1.get_lives(); |
| jamesheavey | 27:1b5038b0a7a2 | 379 | |
| jamesheavey | 27:1b5038b0a7a2 | 380 | // print to LCD i |
| jamesheavey | 27:1b5038b0a7a2 | 381 | char buffer1[14]; |
| jamesheavey | 27:1b5038b0a7a2 | 382 | sprintf(buffer1,"%2d",p1_score); |
| jamesheavey | 27:1b5038b0a7a2 | 383 | lcd.printString(buffer1,WIDTH/2 -8,4); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
| jamesheavey | 27:1b5038b0a7a2 | 384 | } |
| jamesheavey | 27:1b5038b0a7a2 | 385 | |
| jamesheavey | 27:1b5038b0a7a2 | 386 | int BreakoutEngine::get_num_left(){ |
| jamesheavey | 27:1b5038b0a7a2 | 387 | return _number_left; |
| jamesheavey | 27:1b5038b0a7a2 | 388 | } |
| jamesheavey | 27:1b5038b0a7a2 | 389 | void BreakoutEngine::one_less() { |
| jamesheavey | 27:1b5038b0a7a2 | 390 | _number_left -= 1; |
| jamesheavey | 27:1b5038b0a7a2 | 391 | } |