ELEC2645 (2018/19) / Mbed 2 deprecated el17cr

Dependencies:   mbed

Revision:
4:03d13a53308c
Parent:
3:5edefa83f8f0
Child:
5:1a9d84df7207
--- a/Falldown/Falldown.cpp	Tue Apr 16 07:43:34 2019 +0000
+++ b/Falldown/Falldown.cpp	Mon Apr 22 16:14:49 2019 +0000
@@ -13,22 +13,25 @@
 void Falldown::init(int ground_height,int ball_size)
 {
     // initialise the game parameters
-    _ground_width1 = 50;
+   srand(time(NULL));
+   int randomnumber1 = rand()%(70 - 50 + 1) + 50;
+   //int randomnumber2 = rand()%(70 - 50 + 1) + 50;
+    _ground_width1 = randomnumber1;
+    _ground_width2 = 50;
+   
+   
+   
+   
     _ground_height = ground_height;
-    _ground_width2 = 70;
     //_ground_height2 = 2;
     _ball_size = ball_size;
     
-    
-    
-    
-
     // x position on screen - WIDTH is defined in N5110.h
     //_p1x = GAP;
     //_p2x = WIDTH - GAP - _Ground_width;
     _bally = GAP;
     _g1x = 1;
-    _g2x = 1;
+    _g2x = 20;
     _g1y = 10;
     _g2y = 20;
     
@@ -38,6 +41,14 @@
     
     _ground1.init(_g1x,_g1y,_ground_height,_ground_width1);
     _ground2.init(_g2x,_g2y,_ground_height,_ground_width2);
+    /*_ground3.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground4.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground5.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground6.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground7.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground8.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground9.init(_g2x,_g2y,_ground_height,_ground_width2);
+    _ground10.init(_g2x,_g2y,_ground_height,_ground_width2);*/
 }
 
 void Falldown::read_input(Gamepad &pad)
@@ -58,11 +69,12 @@
     _ground1.draw(lcd);
     _ground2.draw(lcd);
     _ball.draw(lcd);
+    check_goal(lcd);
 }
 
 void Falldown::update(Gamepad &pad)
 {
-    //check_goal(pad);
+    //check_goal(lcd);
     // important to update Grounds and ball before checking collisions so can
     // correct for it before updating the display
     _ball.updateX(_d,_mag);
@@ -113,18 +125,28 @@
     // see if ball has hit the Ground by checking for overlaps
     if (
         (ball_pos.y >= 7) && //top
-        (ball_pos.y <= 10) //bottom
-        //(ball_pos.x >= _p1x) && //left
+        (ball_pos.y <= 10) && //bottom
+        (ball_pos.x <= _ground_width1) //left
         //(ball_pos.x <= _p1x + _Ground_width)  //right
     ) {
         // if it has, fix position and reflect x velocity
         ball_pos.y = 7;
         ball_velocity.y = 0;
+        }
+    else {
+        ball_velocity.y = 1;
+        }
+        
         //ball_velocity.x = -ball_velocity.x;
         // audio feedback
         //pad.tone(1000.0,0.1);
-    }
-     
+    
+    //if (ball_pos.x >= 70){
+        //ball_pos.y = 8;
+        //ball_velocity.y = 1;
+        
+        //}
+        
      _ball.set_velocity(ball_velocity);
      _ball.set_pos(ball_pos);
 }
@@ -150,31 +172,29 @@
     _ball.set_velocity(ball_velocity);
     _ball.set_pos(ball_pos);
 }
-
-void Falldown::check_goal(Gamepad &pad)
+*/
+void Falldown::check_goal(N5110 &lcd)
 {
     Vector2D ball_pos = _ball.get_pos();
     // P2 has scored
-    if (ball_pos.x + _ball_size < 0) {
-        _p2.add_score();
-        _ball.init(_ball_size,_speed);
-        pad.tone(1500.0,0.5);
-        pad.leds_on();
+    if (ball_pos.y > HEIGHT) {
+        lcd.clear();
+        lcd.printString(" Level 2 ",0,1);
+        lcd.refresh();
+        
         wait(0.5);
-        pad.leds_off();
-    }
-
-    // P1 has scored
-    if (ball_pos.x > WIDTH) {
-        _p1.add_score();
-        _ball.init(_ball_size,_speed);
-        pad.tone(1500.0,0.5);
-        pad.leds_on();
-        wait(0.5);
-        pad.leds_off();
-    }
+        
+        //_ball.init(_bally,_ball_size);
+    
+       // _ground1.init(_g1x,_g1y,_ground_height,_ground_width1);
+       // _ground2.init(_g2x,_g2y,_ground_height,_ground_width2);
+       // lcd.refresh();
+        }
+        
+    
 }
 
+/*
 void Falldown::print_scores(N5110 &lcd)
 {
     // get scores from Grounds