Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
32:3a3bdeffdf62
Parent:
29:c6358c39a70e
Child:
33:249cf423fb18
--- a/SnakevsBlock/SnakevsBlock.cpp	Mon Apr 15 06:27:23 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Mon Apr 15 13:58:09 2019 +0000
@@ -19,7 +19,7 @@
     send=0;
     speed = 1;
     blockgap = 500;
-    blockbuff = 0;
+    blockbuff = -50;
     for(int i=0; i<=14; i++)  {
         b[i] = 1;
     }
@@ -68,7 +68,7 @@
     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(_d,_mag, length, speed, b); //_d is the direction of joystick and b controls thew motion of a section of the snake relative to obstruction
+    _s.update(pad.get_direction(),_mag, length, speed, b); //_d is the direction of joystick and b controls thew motion of a section of the snake relative to obstruction
     _f.update();
     _ff.update();
     _fff.update(); 
@@ -77,12 +77,6 @@
     if(blockbuff == blockgap)  {  //change this while changing the block drop gap
         blockbuff = 0;
     }
-    if(length >= 11)  {  //to make it easier to react to sudden happenings
-        speed = 2;
-    }
-    else  {
-        speed = 1;
-    }
     if(length > 15)  {  //to make progressive levels harder
         blockgap -= 20;
         level += 1;
@@ -112,15 +106,15 @@
     
 
 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)    {
-        //Obtains all required coordinates.
-        Vector2D food_pos[3];
-        food_pos[0] = _f.get_pos();
-        food_pos[1] = _ff.get_pos();
-        food_pos[2] = _fff.get_pos();
-        Vector2D snake_pos = _s.get_pos(length);
-        //If statements check if the snake sprite has collided with any
-        //of the three food sprites, if so then the food location is reset and
-        //length of the snake is increased using the length variable.
+    //Obtains all required coordinates.
+    Vector2D food_pos[3];
+    food_pos[0] = _f.get_pos();
+    food_pos[1] = _ff.get_pos();
+    food_pos[2] = _fff.get_pos();
+    Vector2D snake_pos = _s.get_pos(length);
+    //If statements check if the snake sprite has collided with any
+    //of the three food sprites, if so then the food location is reset and
+    //length of the snake is increased using the length variable.
     for(int y=0; y<=2; y++)  {    //this loop automatically detects each combination of collision in the y postion
         for(int x=0; x<=2; x++)  {    //this loop automatically detects each combination of collision in the x postion
             for(int food_sr=0; food_sr<=2; food_sr++)  {    //this loop automatically detects which food we are interacting with.
@@ -184,7 +178,6 @@
 void SnakevsBlock::ImplementCollision(Gamepad &pad)  {
     send=1;
     blocknum-=1;
-    speed = 2;
     if(blocknum >= 0)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
         length-=1;
         pad.tone(1000.0,0.1);
@@ -215,7 +208,6 @@
         //If statements check if the snake sprite has collided with any
         //of the blocks' sides and then stop the snake moving in x axis
         
-        speed = 1;
         for(int i=0; i<=14; i++)  {
             b[i] = 1;
         }
@@ -227,9 +219,9 @@
                 (snake_pos[i].y + 1 == b_pos.y + a) ||
                 (snake_pos[i].y + 2 == b_pos.y + a))  {
                 
-                    //For West side of walls 
-                    if(
-                    (((snake_pos[i].x == b_pos.x + 4) ||  //W
+                //For West side of walls 
+                if(
+                    ((snake_pos[i].x == b_pos.x + 4) ||  //W
                     (snake_pos[i].x == b_pos.x + 36) ||  //W
                     (snake_pos[i].x == b_pos.x + 68) ||  //W
                     (snake_pos[i].x + 1 == b_pos.x + 4) ||  //W
@@ -240,7 +232,7 @@
                     (snake_pos[i].x == b_pos.x + 84) ||  //W
                     (snake_pos[i].x + 1 == b_pos.x + 20) ||  //W
                     (snake_pos[i].x + 1 == b_pos.x + 52) ||  //W
-                    (snake_pos[i].x + 1 == b_pos.x + 84))&&(d != E)&&(length > i)) //W
+                    (snake_pos[i].x + 1 == b_pos.x + 84))&&(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<=15; snake_beed_num++)  {
@@ -252,7 +244,7 @@
             
              //for East side of walls
                 else if (
-                    (((snake_pos[i].x + 1 == b_pos.x + 18) ||  //E
+                    ((snake_pos[i].x + 1 == b_pos.x + 18) ||  //E
                     (snake_pos[i].x + 1 == b_pos.x + 50) ||  //E
                     (snake_pos[i].x + 1 == b_pos.x + 82) ||  //E
                     (snake_pos[i].x + 2 == b_pos.x + 18) ||  //E
@@ -263,10 +255,14 @@
                     (snake_pos[i].x + 1 == b_pos.x + 66) ||  //E
                     (snake_pos[i].x + 2 == b_pos.x + 2) ||  //E
                     (snake_pos[i].x + 2 == b_pos.x + 34) ||  //E
-                    (snake_pos[i].x + 2 == b_pos.x + 66))&&(d != W)) //E
+                    (snake_pos[i].x + 2 == b_pos.x + 66))&&(d != W) //E
                 ) {
-                    //add some code that it doesn't move through
-                    speed = 0;
+                    //code makes sure that the colliding part doesn't move in x axis.
+                    for(int snake_beed_num=0; snake_beed_num<=15; snake_beed_num++)  {
+                        if(length == snake_beed_num + i)  {
+                            b[snake_beed_num - 1] = 0;
+                        }
+                    }
                 }
             }
         }