Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
29:c6358c39a70e
Parent:
28:d2c621d67e3b
Child:
32:3a3bdeffdf62
--- a/SnakevsBlock/SnakevsBlock.cpp	Fri Apr 12 16:43:16 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Sun Apr 14 10:24:59 2019 +0000
@@ -113,76 +113,41 @@
 
 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)    {
         //Obtains all required coordinates.
-        Vector2D f_pos = _f.get_pos();
-        Vector2D ff_pos = _ff.get_pos();
-        Vector2D fff_pos = _fff.get_pos();
+        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 i=0; i<=2; i++)  {    //this loop automatically detects each combination of collision 
-        if (
-            ((snake_pos.y + i == f_pos.y) ||
-            (snake_pos.y + i == f_pos.y + 1) ||
-            (snake_pos.y + i == f_pos.y + 2)) &&
-            ((snake_pos.x + i == f_pos.x) ||
-            (snake_pos.x + i == f_pos.x + 1) ||
-            (snake_pos.x + i == f_pos.x + 2))
-        ) {
-            //printf("snake feast working \n");
-            //audio feedback
-            pad.tone(1000.0,0.1);
-            f_pos.x = rand() % 82;
-            if((blockbuff>=11)&&(blockbuff<=blockgap-11))  { //this makes sure that the snake food appears seperated from the block
-                f_pos.y = -2;
+    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.
+                if (
+                    ((snake_pos.y + y == food_pos[food_sr].y) ||
+                    (snake_pos.y + y == food_pos[food_sr].y + 1) ||
+                    (snake_pos.y + y == food_pos[food_sr].y + 2)) &&
+                    ((snake_pos.x + x == food_pos[food_sr].x) ||
+                    (snake_pos.x + x == food_pos[food_sr].x + 1) ||
+                    (snake_pos.x + x == food_pos[food_sr].x + 2))
+                ) {
+                    //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;
+                    }
+                    length+=1;
+                }
             }
-            length+=1;
         }
-    if (
-        ((snake_pos.y + i== ff_pos.y) ||
-        (snake_pos.y + i == ff_pos.y + 1) ||
-        (snake_pos.y + i == ff_pos.y + 2)) &&
-        ((snake_pos.x + i == ff_pos.x) ||
-        (snake_pos.x + i == ff_pos.x + 1) ||
-        (snake_pos.x + i == ff_pos.x + 2))
-    ) {
-        
-        //printf("snake feast working \n");
-        // audio feedback
-        pad.tone(1000.0,0.1);
-        ff_pos.x = rand() % 82;
-        if((blockbuff>=11)&&(blockbuff<=blockgap-11))  { //this makes sure that the snake food appears seperated from the block
-            ff_pos.y = -2;
-        }
-        length+=1;
-        
     }
-    
-    if (
-        ((snake_pos.y + i == fff_pos.y) ||
-        (snake_pos.y + i == fff_pos.y + 1) ||
-        (snake_pos.y + i == fff_pos.y + 2)) &&
-        ((snake_pos.x + i == fff_pos.x) ||
-        (snake_pos.x + i == fff_pos.x + 1) ||
-        (snake_pos.x + i == fff_pos.x + 2))
-    ) {
-        
-        //printf("snake feast working \n");
-        // audio feedback
-        pad.tone(1000.0,0.1);
-        fff_pos.x = rand() % 82;
-        if((blockbuff>=11)&&(blockbuff<=blockgap-11))  { //this makes sure that the snake food appears seperated from the block
-            fff_pos.y = -2;
-        }
-        length+=1;
-        
-    }
-    }
-    _f.set_pos(f_pos);
-    _ff.set_pos(ff_pos);
-    _fff.set_pos(fff_pos);
-    
-    }
+    _f.set_pos(food_pos[0]);
+    _ff.set_pos(food_pos[1]);
+    _fff.set_pos(food_pos[2]);
+}
     
     void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
     {
@@ -191,39 +156,39 @@
         int *b_number;
         b_number = _b.get_number();
         Vector2D snake_pos = _s.get_pos(length);
-        
         //If statements check if the snake sprite has collided with any
         //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
         //the block has to move slower and come down after every 2/3 iterations(dependent on the snake size.(think about this)
-    for(int block=1; block<=82; block+=1)  {    //this loop automatically detects for each section of block and each combination of collision
-            if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block))  {
-                //printf("snake collision working \n");
-                //audio feedback
-                if(blocknum > 0)  { //change this to speed y = 0 when length = 10.
-                    b_pos.y = 0;
-                }
-                if((block>=1)&&(block<=18))  {srn = 0;}
-                if((block>=19)&&(block<=34))  {srn = 1;}
-                if((block>=35)&&(block<=50))  {srn = 2;}
-                if((block>=51)&&(block<=66))  {srn = 3;}
-                if((block>=67)&&(block<=82))  {srn = 4;}
-                blocknum = b_number[srn];
-                ImplementCollision(pad);
+    for(int block=0; block<=83; block+=1)  {    //this loop automatically detects for each section of block and each combination of collision
+        if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block))  {
+            //printf("snake collision working \n");
+            //audio feedback
+            if(blocknum > 0)  {b_pos.y = 0;}  //change this to speed y = 0 when length = 10.
+            srn = CheckBlock(block);  //this tells us which of the 5 blocks we are colliding with
+            blocknum = b_number[srn];
+            ImplementCollision(pad);
         }
     }
 }
+ 
+int SnakevsBlock::CheckBlock(int block)  {
+    int srn;
+    if((block>=0)&&(block<=18))  {srn = 0;}
+    if((block>=19)&&(block<=34))  {srn = 1;}
+    if((block>=35)&&(block<=50))  {srn = 2;}
+    if((block>=51)&&(block<=66))  {srn = 3;}
+    if((block>=67)&&(block<=83))  {srn = 4;}
+    return srn;
+}
     
 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;
-        speed = 2;
         length-=1;
         pad.tone(1000.0,0.1);
-        wait(0.05);
-    }
-    else  {
-        speed = 1;
+        wait(0.04);
     }
 }