Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
38:30e4e6191762
Parent:
37:ee47699915b8
Child:
39:210ac915e0a0
--- a/SnakevsBlock/SnakevsBlock.cpp	Thu Apr 18 14:03:43 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Mon Apr 22 04:24:38 2019 +0000
@@ -15,6 +15,8 @@
     //The snake length configuration and all the other initial information passing will be done here
     length = 3;
     level = 1;
+    garbage = 0;
+    angle = 0;
     foodbuff = 0;
     send=0;
     speed = 1;
@@ -32,10 +34,35 @@
 
 
 
-void SnakevsBlock::read_input(Gamepad &pad)
+void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int gm)
 {
-    _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
-    _mag = pad.get_mag(); //Obtains Magnitude of Joystick.
+    device.get_values();
+    angle = -device.get_roll_angle();
+    //if button A is pressed then reset that particular position to center
+    if (pad.check_event(Gamepad::A_PRESSED) == true)  {
+        garbage = angle;
+    }
+    device.get_values();
+    angle = -device.get_roll_angle() - garbage;
+    if(gm == 1)  {
+        _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
+        _mag = pad.get_mag(); //Obtains Magnitude of Joystick.
+    }
+    else if(gm == 2)  {
+        if (angle >= 8) {
+            _d = E;
+        }
+        else if (angle <= -8) {
+            _d = W;
+        }
+        else {
+            _d = CENTRE;
+        }
+    }
+    device.get_values();
+    //printf("%d",gm);
+    //printf("%f",angle);
+    //printf("%f",device.get_roll_angle());
 }
 
 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)   {
@@ -54,7 +81,7 @@
         send = _b.draw(lcd, length, blockgap);
     }
     if(foodbuff == 8) {
-        blockbuff = -11;
+        blockbuff = -10;
     }
     //Code to print length on game screen.
     char bufferscore[14];
@@ -67,8 +94,8 @@
 {
     CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
     CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
-    CheckSnakeBlockSidesCollision(pad, _d); //Function checks for when the snake collides with any of the blocks' sides.
-    _s.update(pad.get_direction(), length, speed, b); //_d is the direction of joystick and b controls thew motion of a section of the snake relative to obstruction
+    CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
+    _s.update(_d, length, speed, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction
     _f.update();
     _ff.update();
     _fff.update(); 
@@ -132,10 +159,8 @@
                     //printf("snake feast working \n");
                     //audio feedback
                     pad.tone(1000.0,0.1);
-                    food_pos[food_sr].x = rand() % 82;
-                    if((blockbuff>=11)&&(blockbuff<=blockgap-11))  { //this makes sure that the snake food appears seperated from the block
-                        food_pos[food_sr].y = -2;
-                    }
+                    food_pos[food_sr].x = (rand() % 82);  //this makes the food pop up at a random, unspecified location in the x axis.
+                    food_pos[food_sr].y = -3;
                     length+=1;
                 }
             }
@@ -166,9 +191,17 @@
                 ImplementCollision(pad);
                 if((length>=10)&&(b_number[srn]>0)) { //this makes the block stop moving down if it's length is more than 10 and still collides.
                     _b.velocity.y = 0;
+                    _f.velocity.y = 0;
+                    _ff.velocity.y = 0;
+                    _fff.velocity.y = 0;
                     break;//change this so that it collides slower.
                 }
-                else {_b.velocity.y = 1;}
+                else {
+                    _b.velocity.y = 1;
+                    _f.velocity.y = 1;
+                    _ff.velocity.y = 1;
+                    _fff.velocity.y = 1;
+                }
             }
         }
 }
@@ -193,7 +226,7 @@
     }
 }
 
-void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad, Direction d)
+void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad)
     {
         //Obtains all required coordinates for checking block sides and snake collision.
         Vector2D b_pos = _b.get_pos();
@@ -236,7 +269,7 @@
                     //For West side of walls 
                     if(
                         ((snake_pos[i].x == b_pos.x + b_x_combination+2) ||  //W
-                        (snake_pos[i].x + 1 == b_x_combination+2))&&(d != E)&&(_length > i) //W
+                        (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(_length > i) //W
                         ) {
                         //code makes sure that the colliding part doesn't move in x axis.
                         for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++)  {
@@ -249,7 +282,7 @@
                     //for East side of walls
                     else if (
                         ((snake_pos[i].x + 1 == b_x_combination) ||  //E
-                        (snake_pos[i].x + 2 == b_x_combination))&&(d != W)&&(_length > i) //E
+                        (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(_length > i) //E
                     ) {
                         //code makes sure that the colliding part doesn't move in x axis.
                         for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++)  {