ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Revision:
8:52794de935b2
Parent:
7:1dce07fd0867
Child:
9:d210eede4a1b
--- a/CrossyChicken/CrossyChicken.cpp	Mon May 11 22:57:53 2020 +0000
+++ b/CrossyChicken/CrossyChicken.cpp	Tue May 12 05:14:17 2020 +0000
@@ -29,9 +29,16 @@
 Car lo[2];
 Car logger[2];
 
+int screenW = 84;
+int screenH = 48;
+
 //class that whill show objects
 GraphicEngine graphics(chickenptr);
 
+bool frogLog = false;
+bool frogDie = false; // whenever the frog is above safety lane in the middle
+bool attach = false;
+
 //main function that starts the game
 void CrossyChicken::start(){     
     //game setup
@@ -87,10 +94,8 @@
     //keep reading and processing user input
     while(1) {
         graphics.clear();
-        graphics.showChicken(); 
-        process_input();
-        
-         for(int i = 0; i < 2; i++){
+          
+        for(int i = 0; i < 2; i++){
             moveCar(&firstLane[i], 2, (-2)); // change x position (moving)
         }
         
@@ -107,7 +112,7 @@
         }
         
         for(int v = 0; v < 2; v++){
-            moveCar(&log1Lane[v], 2, 1);
+            moveCar(&log1Lane[v], 1, 1);
         }
        
         for(int b = 0; b < 2; b++){
@@ -123,13 +128,33 @@
         graphics.showCar(log1Lane);
         graphics.showCar(lo);
         //graphics.showCar(logger);
+        
+        graphics.showChicken(); 
+        process_input();
+        
+        // now when the chicken is above the safety 
+        // lane we need to ensure that it only can go on logs
+        if(chicken.y < screenH - grid*6){
+            frogDie = true;  // frog can die if it is in water  
 
-        for(int t = 0; t < 3; t++){
-            collision(&thirdLane[t]);
+            //if it is not in a log
+            for(int x = 0; x < 2; x++){
+                setCollision(&log1Lane[x]);
+                
+                if(attach){
+                    frogOnLog(&log1Lane[x]);   
+                }
+            }
+            
+            if(!attach){
+                graphics.print();    
+            }
+            
         }
-        
+                
+
         graphics.refresh();
-        wait_ms(50);
+        wait_ms(200);
     } 
 }
 
@@ -144,15 +169,28 @@
 //Y moves left
 void CrossyChicken::process_input() {
     //determine the input 
+    /* make this a switch statement */
     if(gamepad.A_pressed()){
         moveChicken(1,0);
+        attach = false;
     } else if(gamepad.X_pressed()){
         moveChicken(0,-1);
+        attach = false;
     } else if(gamepad.B_pressed()){
         moveChicken(0,1);
+        attach = false;
     } else if(gamepad.Y_pressed()){
         moveChicken(-1,0);
-    }   
+        attach = false;
+    } 
+}
+
+// make the frog move same speed as log
+// if the frog moves then detach
+void CrossyChicken::frogOnLog(Car *log) {
+   // if(log->seperation != 0){
+        chicken.x += 0.4;
+   // }     
 }
 
 //moves the chicken around the grid
@@ -167,17 +205,17 @@
     chicken.right_side = grid + chicken.x;
     chicken.up = chicken.y;
     chicken.down = grid + chicken.y;
-    
+        
     //display the new state of the chicken
     graphics.showChicken();
     
-    wait_ms(30);
+    //wait_ms(30);
 }
 
 void CrossyChicken::moveCar(Car *car, int dir, int x) {
     car->speedMedium(dir, x);
     
-      // check if car goes out of bounds
+    // check if car goes out of bounds
     if(car->vehicle.x > 84+grid){
         car->vehicle.x = -4;
         
@@ -187,7 +225,7 @@
 }
 
 // debug
-void CrossyChicken::collision(Car *car){
+void CrossyChicken::setCollision(Car *car){
     
     float other_bottom = car->vehicle.height + car->vehicle.y;
     
@@ -197,6 +235,24 @@
     (chicken.down <= car->vehicle.y) ||
     chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
         graphics.printTest();
+        
+        if(chicken.y < screenH - grid*6){
+            attach = true;
+        } else { 
+            attach = false;
+        } 
+    }
+}
+ 
+bool CrossyChicken::returnCollision(Car* log){      
+    
+    float ob= log->vehicle.height + log->vehicle.y;
+    
+    if(!(chicken.up >= ob||
+    (chicken.right_side <= log->vehicle.x)  ||
+    (chicken.down <= log->vehicle.y) ||
+    chicken.left_side >= (log->vehicle.width + log->vehicle.x))){
+        return true;
     }
 }