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.
Diff: BreakoutEngine/BreakoutEngine.cpp
- Revision:
- 82:d1341d632890
- Parent:
- 81:735e5ee2c92a
- Child:
- 84:6483503a72fc
--- a/BreakoutEngine/BreakoutEngine.cpp Mon May 06 14:23:19 2019 +0000
+++ b/BreakoutEngine/BreakoutEngine.cpp Mon May 06 14:29:15 2019 +0000
@@ -12,12 +12,6 @@
void BreakoutEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
{
- /*
- printf("init started /n");
- listofBricks.clear();
- listofLasers.clear();
- */
-
// initialise the game parameters
_paddle_width = paddle_width;
_paddle_height = paddle_height;
@@ -31,34 +25,34 @@
_multiplier = 1;
// y position on screen - WIDTH is defined in N5110.h
- _p1y = HEIGHT - GAP - 1;
+ _paddley = HEIGHT - GAP - 1;
// puts paddles and ball in middle
- _p1.init(_p1y,_paddle_height,_paddle_width);
- _ball.init(_ball_size,_speed,_p1.get_pos().x + 7);
+ _paddle.init(_paddley,_paddle_height,_paddle_width);
+ _ball.init(_ball_size,_speed,_paddle.get_pos().x + 7);
- _brick11.init(3,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3); // need to figure out how to make a list of these
+ _brick11.init(3,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
_brick12.init(16,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
_brick13.init(29,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
_brick14.init(42,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
_brick15.init(55,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
_brick16.init(68,GAP_TOP+1,BRICK_HEIGHT,BRICK_WIDTH,3);
- _brick21.init(3,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2); // need to figure out how to make a list of these
+ _brick21.init(3,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
_brick22.init(16,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
_brick23.init(29,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
_brick24.init(42,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
_brick25.init(55,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
_brick26.init(68,GAP_TOP+BRICK_HEIGHT+2,BRICK_HEIGHT,BRICK_WIDTH,2);
- _brick31.init(3,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1); // need to figure out how to make a list of these
+ _brick31.init(3,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
_brick32.init(16,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
_brick33.init(29,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
_brick34.init(42,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
_brick35.init(55,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
_brick36.init(68,GAP_TOP+1+((BRICK_HEIGHT+1)*2),BRICK_HEIGHT,BRICK_WIDTH,1);
- listofBricks.push_back(_brick11); //maybe be able yo iterate through and this.draw(), maybe useful if i figure out how to delete objects aswell
+ listofBricks.push_back(_brick11);
listofBricks.push_back(_brick12);
listofBricks.push_back(_brick13);
listofBricks.push_back(_brick14);
@@ -84,14 +78,13 @@
listofLasers.push_back(_laser1);
listofLasers.push_back(_laser2);
listofLasers.push_back(_laser3);
- //printf("init ended /n");
}
void BreakoutEngine::reset_game() // rename to reset add in ball reset and use to increase the number of balls each time
{
reset_num_left();
- _ball.init(_ball_size,_speed + _multiplier/2, _p1.get_pos().x + 7); // replace the 1 with a multiplier private variable that tracks the number of times continued from victory
- _p1.recentre();
+ _ball.init(_ball_size,_speed + _multiplier/2, _paddle.get_pos().x + 7); // replace the 1 with a multiplier private variable that tracks the number of times continued from victory
+ _paddle.recentre();
int pointer = 0;
for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R){
if (pointer <= 5) {
@@ -116,17 +109,17 @@
_mag = pad.get_mag();
if (tilt == true) {
- _p1.set_tilt();
+ _paddle.set_tilt();
}
else {
- _p1.set_joy();
+ _paddle.set_joy();
}
- _p1.set_sens(sens);
+ _paddle.set_sens(sens);
if (pad.check_event(Gamepad::B_PRESSED) && _cool_time <= 0) { // max of 3 lasers on screen at once
- Vector2D p_pos = _p1.get_pos();
+ Vector2D p_pos = _paddle.get_pos();
it_L = listofLasers.begin();
switch(_index){
case 0:
@@ -174,7 +167,7 @@
//score
print_scores(lcd);
// paddles
- _p1.draw(lcd);
+ _paddle.draw(lcd);
// ball
_ball.draw(lcd);
@@ -195,7 +188,7 @@
check_loss(pad);
// important to update paddles and ball before checking collisions so can
// correct for it before updating the display
- _p1.update(_d,_mag);
+ _paddle.update(_d,_mag);
_ball.update();
for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
@@ -214,24 +207,24 @@
void BreakoutEngine::lives_leds(Gamepad &pad)
{
- if (_p1.get_lives() == 0) {
+ if (_paddle.get_lives() == 0) {
pad.leds_off();
}
- else if (_p1.get_lives() == 1) {
+ else if (_paddle.get_lives() == 1) {
//turn leftmost led on
pad.leds_off();
pad.led(1,1);
}
- else if (_p1.get_lives() == 2) {
+ else if (_paddle.get_lives() == 2) {
//turn leftmost led on
pad.leds_off();
pad.led(1,1);
pad.led(2,1);
}
- else if (_p1.get_lives() == 3) {
+ else if (_paddle.get_lives() == 3) {
//turn leftmost led on
pad.leds_off();
pad.led(1,1);
@@ -239,24 +232,24 @@
pad.led(3,1);
}
- else if (_p1.get_lives() == 4) {
+ else if (_paddle.get_lives() == 4) {
pad.leds_on();
pad.led(5,0);
pad.led(6,0);
}
- else if (_p1.get_lives() == 5) {
+ else if (_paddle.get_lives() == 5) {
pad.leds_on();
pad.led(6,0);
}
- else if (_p1.get_lives() == 6) {
+ else if (_paddle.get_lives() == 6) {
pad.leds_on();
}
}
int BreakoutEngine::get_lives() {
- return _p1.get_lives();
+ return _paddle.get_lives();
}
void BreakoutEngine::check_wall_collisions(Gamepad &pad)
@@ -310,22 +303,22 @@
Vector2D ball_velocity = _ball.get_velocity();
// check p1 first
- Vector2D p1_pos = _p1.get_pos();
+ Vector2D p1_pos = _paddle.get_pos();
// see if ball has hit the paddle by checking for overlaps
if (
(ball_pos.x >= p1_pos.x) && //left
(ball_pos.x <= p1_pos.x + _paddle_width) && //right
- (ball_pos.y >= _p1y) && //bottom
- (ball_pos.y <= _p1y + _paddle_height) //top
+ (ball_pos.y >= _paddley) && //bottom
+ (ball_pos.y <= _paddley + _paddle_height) //top
) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
// if it has, fix position and reflect x velocity
pad.tone(1000.0,0.1);
- ball_pos.y = _p1y + _paddle_height - 1;
+ ball_pos.y = _paddley + _paddle_height - 1;
ball_velocity.y = -ball_velocity.y;
// 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
-// ball_pos.y = _p1y + _paddle_height - 1;
+// ball_pos.y = _paddley + _paddle_height - 1;
// ball_velocity.y = -ball_velocity.y;
// }
// else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
@@ -333,7 +326,7 @@
// if (ball_velocity.x > 0) {
// ball_velocity.x = -ball_velocity.x;
// }
- // ball_pos.y = _p1y + _paddle_heigh - 1;
+ // ball_pos.y = _paddley + _paddle_heigh - 1;
// ball_velocity.y = -tan(ang);
// }
// else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
@@ -341,7 +334,7 @@
// if (ball_velocity.x < 0) {
// ball_velocity.x = -ball_velocity.x;
// }
- // ball_pos.y = _p1y + _paddle_height - 1;
+ // ball_pos.y = _paddley + _paddle_height - 1;
// ball_velocity.y = -tan(ang);
// }
}
@@ -419,10 +412,10 @@
Vector2D ball_pos = _ball.get_pos();
// P1 has scored
if (ball_pos.y > HEIGHT) {
- _p1.lose_life();
+ _paddle.lose_life();
//lose_screen(); // go to loss screen then initialise again
- _ball.init(_ball_size,_speed+_multiplier/2,_p1.get_pos().x + 7);
+ _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + 7);
pad.tone(1500.0,0.5);
return true;
}