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:
- 114:280903dd7e06
- Parent:
- 111:4848c399fae0
- Child:
- 115:aee684f62361
diff -r d180342ac017 -r 280903dd7e06 BreakoutEngine/BreakoutEngine.cpp
--- a/BreakoutEngine/BreakoutEngine.cpp Wed May 08 04:00:00 2019 +0000
+++ b/BreakoutEngine/BreakoutEngine.cpp Wed May 08 13:08:55 2019 +0000
@@ -82,6 +82,8 @@
listofLasers.push_back(_laser1);
listofLasers.push_back(_laser2);
listofLasers.push_back(_laser3);
+
+ _powerup.init();
}
void BreakoutEngine::reset_game() // resets the game after victory screen
@@ -170,6 +172,9 @@
for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
it_L->draw(lcd);
}
+
+ check_life_powerup();
+ _powerup.draw(lcd);
}
@@ -180,6 +185,7 @@
// correct for it before updating the display
_paddle.update(_d,_mag);
_ball.update();
+ _powerup.update();
for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
it_L->update();
@@ -191,7 +197,7 @@
check_paddle_collisions(pad);
check_brick_collisions(pad);
check_laser_collisions(pad);
-
+ check_powerup_collisions(pad);
}
@@ -375,7 +381,7 @@
(it_L -> get_x() >= it_R -> get_x()) && //left
(it_L -> get_x() <= it_R -> get_x() + BRICK_WIDTH) && //right
(it_L -> get_y() >= it_R -> get_y()) && //bottom
- (it_L -> get_y()<= it_R -> get_y() + BRICK_HEIGHT) //top
+ (it_L -> get_y() <= it_R -> get_y() + BRICK_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
it_L -> set_posx(-10);
@@ -390,6 +396,36 @@
}
}
+
+void BreakoutEngine::check_powerup_collisions(Gamepad &pad)
+{
+ // read current ball attributes
+
+ // check paddle first
+ Vector2D paddle_pos = _paddle.get_pos();
+
+ // see if ball has hit the paddle by checking for overlaps
+ if (
+ (_powerup.get_x() >= paddle_pos.x) && //left
+ (_powerup.get_x() <= paddle_pos.x + _paddle_width) && //right
+ (_powerup.get_y() +9>= _paddley) && //bottom
+ (_powerup.get_y() +9<= _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(2500.0,0.1);
+ _powerup.set_posx(-50);
+ _paddle.inc_life();
+ }
+
+ else if (
+ (_powerup.get_y() +9 >= HEIGHT) // bottom of screen
+ ) { // 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
+ _powerup.set_posx(-50);
+ }
+
+}
+
bool BreakoutEngine::check_loss(Gamepad &pad)
{
Vector2D ball_pos = _ball.get_pos();
@@ -412,7 +448,6 @@
} else {
_paddle.set_joy();
}
-
_paddle.set_sens(sens);
}
@@ -489,3 +524,18 @@
{
_index = 0;
}
+
+void BreakoutEngine::check_life_powerup()
+{
+ srand(time(0)); // Initialize random number generator.
+ int r1 = (rand() % 20) + 1;
+ int r2 = (rand() % 20) + 1;
+ int rx = (rand() % 50) + 10;
+ //printf("r1 = %d",r1);
+ //printf("r2 = %d",r2);
+ //printf("rx = %d",rx);
+ if ((get_lives() < 6) & (r1 == r2)){
+ _powerup.set_posx(rx);
+ _powerup.set_posy(HEIGHT/2);
+ }
+}
\ No newline at end of file