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: PongEngine/PongEngine.cpp
- Revision:
- 9:f6f0f39538c7
- Parent:
- 8:1ab6d90c4d60
- Child:
- 10:9f445faa892c
--- a/PongEngine/PongEngine.cpp Sun Apr 21 19:40:05 2019 +0000
+++ b/PongEngine/PongEngine.cpp Sun Apr 21 20:44:24 2019 +0000
@@ -139,12 +139,29 @@
// correct for it before updating the display
_p1.update(_d,_mag);
_ball.update();
-
+
+ lives_leds(pad);
+
check_wall_collision(pad);
check_paddle_collisions(pad);
check_brick_collisions(pad);
}
+void PongEngine::lives_leds(Gamepad &pad)
+{
+ if (_p1.get_lives() == 0) {
+ pad.leds_off();
+ }
+
+ //else if (_p1.get_lives() == 1) {
+ //turn leftmost led on
+ //}
+
+ else if (_p1.get_lives() == 6) {
+ pad.leds_off();
+ }
+}
+
void PongEngine::check_wall_collision(Gamepad &pad)
{
// read current ball attributes
@@ -191,36 +208,37 @@
// 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) && //left
(ball_pos.x <= p1_pos.x + _paddle_width) && //right
(ball_pos.y >= _p1y) && //bottom
(ball_pos.y <= _p1y + _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_velocity.y = -ball_velocity.y;
- if (ball_pos.x == p1_pos.x + PADDLE_WIDTH/2) {
- ball_pos.y = _p1y + _paddle_height;
- ball_velocity.y = -ball_velocity.y;
- }
- else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
- float ang = 30*(((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
- if (ball_velocity.x > 0) {
- ball_velocity.x = -ball_velocity.x;
- }
- ball_velocity.y = -tan(ang);
- }
- else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
- float ang = 30*(((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
- if (ball_velocity.x < 0) {
- ball_velocity.x = -ball_velocity.x;
- }
- ball_velocity.y = -tan(ang);
- }
+// 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_velocity.y = -ball_velocity.y;
+// }
+// else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
+// 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
+// if (ball_velocity.x > 0) {
+// ball_velocity.x = -ball_velocity.x;
+// }
+ // ball_pos.y = _p1y + _paddle_heigh - 1;
+ // ball_velocity.y = -tan(ang);
+ // }
+ // else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
+ // 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
+ // if (ball_velocity.x < 0) {
+ // ball_velocity.x = -ball_velocity.x;
+ // }
+ // ball_pos.y = _p1y + _paddle_height - 1;
+ // ball_velocity.y = -tan(ang);
+ // }
}
-
- // check ballxpos in relation to paddle xpos. translate the distance from the centre to an angle between 30 and 60 degrees in that direction
-
// write new attributes
_ball.set_velocity(ball_velocity);
@@ -261,7 +279,9 @@
Vector2D ball_pos = _ball.get_pos();
// P1 has scored
if (ball_pos.y > HEIGHT) {
+ _p1.lose_life();
//lose_screen(); // go to loss screen then initialise again
+
_ball.init(_ball_size,_speed);
pad.tone(1500.0,0.5);
pad.leds_on();
@@ -270,6 +290,24 @@
}
}
+
+//void lives_leds(Gamepad &pad)
+//{
+// int lives = _p1.get_pos();
+// if (lives == 6) {
+// pad.leds_on();
+// }
+
+ //if {lives == 5) {
+ // 5leds on
+ //}
+
+// if (lives == 0) {
+// pad.leds_off();
+// }
+//}
+
+
void PongEngine::print_scores(N5110 &lcd)
{
// get scores from paddles