ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Revision:
5:6e3afee7eac3
Parent:
4:aae7f8d4ab78
Child:
6:e285eaf8bdcd
diff -r aae7f8d4ab78 -r 6e3afee7eac3 CrossyChicken/CrossyChicken.cpp
--- a/CrossyChicken/CrossyChicken.cpp	Wed May 06 16:53:18 2020 +0000
+++ b/CrossyChicken/CrossyChicken.cpp	Thu May 07 05:35:50 2020 +0000
@@ -5,6 +5,7 @@
 #include <stdio.h> 
 #include <cstddef>
 
+// start from the top (height)
 Gamepad gamepad;
 
 //create three class: CAR LANE, SAFETY LANE, OBSTACLE LANE(water)
@@ -20,7 +21,9 @@
 Chicken *chickenptr= &chicken;
 
 //there will be multiple cars       
-std::vector<Car> cars;  
+Car firstLane[2];
+Car secondLane[2];  
+Car thirdLane[3];
 
 //class that whill show objects
 GraphicEngine graphics(chickenptr);
@@ -33,17 +36,49 @@
     graphics.backLightOn();
     gamepad.init();
     
-    for (int i = 0; i < 4; i++){
-        Car car;
-        cars.push_back(car);   
-    }
+    //first lane of left racers
+    firstLane[0].setSeperation(0);
+    firstLane[1].setSeperation(40);
+    firstLane[0].setRow(2);
+    firstLane[1].setRow(2);
     
+    secondLane[0].setSeperation(0);
+    secondLane[1].setSeperation(30);
+    secondLane[0].setRow(3);
+    secondLane[1].setRow(3);
+    
+    thirdLane[0].setSeperation(30);
+    thirdLane[1].setSeperation(0);
+    thirdLane[2].setSeperation(50);
+    thirdLane[0].setRow(4);
+    thirdLane[1].setRow(4);
+    thirdLane[2].setRow(4);
+
     //keep reading and processing user input
     while(1) {
         graphics.clear();
         graphics.showChicken(); 
         process_input();
-        moveCar();
+        
+        for(int i = 0; i < 2; i++){
+            moveCar(&firstLane[i], 2, (-2)); // change x position (moving)
+            collision(&firstLane[i]);
+        }
+        
+        for(int x = 0; x < 2; x++){
+            moveCar(&secondLane[x], 1, 1);
+        }
+        
+        for(int t = 0; t < 3; t++){
+            moveCar(&thirdLane[t], 2, 1);
+            collision(&thirdLane[t]);
+
+        }
+        
+        graphics.showCar(firstLane);
+        graphics.showCar(secondLane);
+        graphics.showCar(thirdLane);
+
         graphics.refresh();
         wait_ms(100);
     } 
@@ -80,22 +115,27 @@
     wait_ms(30);
 }
 
-void CrossyChicken::moveCar() {
-    std::vector<Car>::size_type t;
-    
-    
-            
-    //for(int t = 0; t != cars.size(); t++){
-        //set the seperation of the first row
-        cars.at(0).setRow(4);
+void CrossyChicken::moveCar(Car *car, int dir, int x) {
+    car->speedMedium(dir, x);
     
-        Car *carptr = &cars[0];
+      // check if car goes out of bounds
+    if(car->vehicle.x > 84+grid){
+        car->vehicle.x = -4;
         
-        carptr->speedMedium(2);
-        //carptr->left_car.x += 0.2;
-          
-        carptr->setSeperation(0);
+    } else if(car->vehicle.x < -4){ 
+        car->vehicle.x = 84 + grid;
+    }
+}
+
+// debug
+void CrossyChicken::collision(Car *car){
     
-        graphics.showCar(carptr);
-    //}
-}
\ No newline at end of file
+    float other_bottom = car->vehicle.height + car->vehicle.y;
+    if(!(chicken.up >= other_bottom || 
+    (chicken.right_side <= car->vehicle.x)  ||
+    (chicken.down <= car->vehicle.y) ||
+    chicken.left_side >= (car->vehicle.width + car->vehicle.x))){
+        graphics.printTest();
+    }
+}
+