Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
12:1e601b176437
Parent:
10:751bd953fa27
Child:
13:9785f2404045
--- a/SnakevsBlock/SnakevsBlock.cpp	Sat Mar 30 21:42:28 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Tue Apr 02 17:06:22 2019 +0000
@@ -13,8 +13,10 @@
 void SnakevsBlock::init()
 {
     //The snake length configuration and all the other initial information passing will be done here
-    length=3;
-    foodbuff=0; 
+    length = 3;
+    foodbuff = 0;
+    send=0;
+    //memory = 0;
 }
 
 
@@ -41,7 +43,7 @@
             foodbuff = 110;
         }
     }
-    _b.draw(lcd, length);
+    send = _b.draw(lcd, length, blocknum, srn);
     
     //Code to print length on game screen.
     char bufferscore[14];
@@ -60,12 +62,13 @@
 
 void SnakevsBlock::update(Gamepad &pad) //Updates objects on screen.
 {
+    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
+    CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
     _s.update(_d,_mag, length);
     _f.update();
     _ff.update();
     _fff.update(); 
-    _b.update();
-    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
+    _b.update(blocknum, srn, send);
 }
 
 void SnakevsBlock::get_pos()
@@ -193,4 +196,214 @@
     _ff.set_pos(ff_pos);
     _fff.set_pos(fff_pos);
     
-    }    
\ No newline at end of file
+    }
+    
+    void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
+    {
+        //Obtains all required coordinates.
+        Vector2D b_pos = _b.get_pos();
+        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)
+        
+    if (
+        ((snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10)) &&
+        ((snake_pos.x + 1 == b_pos.x + 1) ||
+        (snake_pos.x + 1 == b_pos.x + 2) ||
+        (snake_pos.x + 1 == b_pos.x + 3) ||
+        (snake_pos.x + 1 == b_pos.x + 4) ||
+        (snake_pos.x + 1 == b_pos.x + 5) ||
+        (snake_pos.x + 1 == b_pos.x + 6) ||
+        (snake_pos.x + 1 == b_pos.x + 7) ||
+        (snake_pos.x + 1 == b_pos.x + 8) ||
+        (snake_pos.x + 1 == b_pos.x + 9) ||
+        (snake_pos.x + 1 == b_pos.x + 10) ||
+        (snake_pos.x + 1 == b_pos.x + 11) ||
+        (snake_pos.x + 1 == b_pos.x + 12) ||
+        (snake_pos.x + 1 == b_pos.x + 13) ||
+        (snake_pos.x + 1 == b_pos.x + 14) ||
+        (snake_pos.x + 1 == b_pos.x + 15) ||
+        (snake_pos.x + 1 == b_pos.x + 16) ||
+        (snake_pos.x + 1 == b_pos.x + 17) ||
+        (snake_pos.x + 1 == b_pos.x + 18))
+    ) {
+        
+        //printf("snake collision working \n");
+        //audio feedback
+        if(blocknum > -1)  {
+            b_pos.y = 0;
+            pad.tone(1000.0,0.1);
+        }
+        srn = 0;
+        blocknum = b_number[srn];
+        send=1;
+        blocknum-=1;
+        if(blocknum >= -1)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
+            length-=1;
+        }
+        wait(0.1);
+    }
+    
+    if (
+        ((snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10)) &&
+        ((snake_pos.x + 1 == b_pos.x + 19) ||
+        (snake_pos.x + 1 == b_pos.x + 20) ||
+        (snake_pos.x + 1 == b_pos.x + 21) ||
+        (snake_pos.x + 1 == b_pos.x + 22) ||
+        (snake_pos.x + 1 == b_pos.x + 23) ||
+        (snake_pos.x + 1 == b_pos.x + 24) ||
+        (snake_pos.x + 1 == b_pos.x + 25) ||
+        (snake_pos.x + 1 == b_pos.x + 26) ||
+        (snake_pos.x + 1 == b_pos.x + 27) ||
+        (snake_pos.x + 1 == b_pos.x + 28) ||
+        (snake_pos.x + 1 == b_pos.x + 29) ||
+        (snake_pos.x + 1 == b_pos.x + 30) ||
+        (snake_pos.x + 1 == b_pos.x + 31) ||
+        (snake_pos.x + 1 == b_pos.x + 32) ||
+        (snake_pos.x + 1 == b_pos.x + 33) ||
+        (snake_pos.x + 1 == b_pos.x + 34))
+    ) {
+        
+        //printf("snake collision working \n");
+        //audio feedback
+        if(blocknum > -1)  {
+            b_pos.y = 0;
+            pad.tone(1000.0,0.1);
+        }
+        srn = 1;
+        blocknum = b_number[srn];
+        send=1;
+        blocknum-=1;
+        if(blocknum >= -1)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
+            length-=1;
+        }
+        wait(0.1);
+        
+    }
+    
+    if (
+        ((snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10)) &&
+        ((snake_pos.x + 1 == b_pos.x + 35) ||
+        (snake_pos.x + 1 == b_pos.x + 36) ||
+        (snake_pos.x + 1 == b_pos.x + 37) ||
+        (snake_pos.x + 1 == b_pos.x + 38) ||
+        (snake_pos.x + 1 == b_pos.x + 39) ||
+        (snake_pos.x + 1 == b_pos.x + 40) ||
+        (snake_pos.x + 1 == b_pos.x + 41) ||
+        (snake_pos.x + 1 == b_pos.x + 42) ||
+        (snake_pos.x + 1 == b_pos.x + 43) ||
+        (snake_pos.x + 1 == b_pos.x + 44) ||
+        (snake_pos.x + 1 == b_pos.x + 45) ||
+        (snake_pos.x + 1 == b_pos.x + 46) ||
+        (snake_pos.x + 1 == b_pos.x + 47) ||
+        (snake_pos.x + 1 == b_pos.x + 48) ||
+        (snake_pos.x + 1 == b_pos.x + 49) ||
+        (snake_pos.x + 1 == b_pos.x + 50))
+    ) {
+        
+        //printf("snake collision working \n");
+        //audio feedback
+        if(blocknum > -1)  {
+            b_pos.y = 0;
+            pad.tone(1000.0,0.1);
+        }
+        srn = 2;
+        blocknum = b_number[srn];
+        send=1;
+        blocknum-=1;
+        if(blocknum >= -1)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
+            length-=1;
+        }
+        wait(0.1);
+        
+    }
+    
+    if (
+        ((snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10)) &&
+        ((snake_pos.x + 1 == b_pos.x + 51) ||
+        (snake_pos.x + 1 == b_pos.x + 52) ||
+        (snake_pos.x + 1 == b_pos.x + 53) ||
+        (snake_pos.x + 1 == b_pos.x + 54) ||
+        (snake_pos.x + 1 == b_pos.x + 55) ||
+        (snake_pos.x + 1 == b_pos.x + 56) ||
+        (snake_pos.x + 1 == b_pos.x + 57) ||
+        (snake_pos.x + 1 == b_pos.x + 58) ||
+        (snake_pos.x + 1 == b_pos.x + 59) ||
+        (snake_pos.x + 1 == b_pos.x + 60) ||
+        (snake_pos.x + 1 == b_pos.x + 61) ||
+        (snake_pos.x + 1 == b_pos.x + 62) ||
+        (snake_pos.x + 1 == b_pos.x + 63) ||
+        (snake_pos.x + 1 == b_pos.x + 64) ||
+        (snake_pos.x + 1 == b_pos.x + 65) ||
+        (snake_pos.x + 1 == b_pos.x + 66))
+    ) {
+        
+        //printf("snake collision working \n");
+        //audio feedback
+        if(blocknum > -1)  {
+            b_pos.y = 0;
+            pad.tone(1000.0,0.1);
+        }
+        srn = 3;
+        blocknum = b_number[srn];
+        send=1;
+        blocknum-=1;
+        if(blocknum >= -1)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
+            length-=1;
+        }
+        wait(0.1);
+        
+    }
+    
+    if (
+        ((snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10) ||
+        (snake_pos.y == b_pos.y + 10)) &&
+        ((snake_pos.x + 1 == b_pos.x + 67) ||
+        (snake_pos.x + 1 == b_pos.x + 68) ||
+        (snake_pos.x + 1 == b_pos.x + 69) ||
+        (snake_pos.x + 1 == b_pos.x + 70) ||
+        (snake_pos.x + 1 == b_pos.x + 71) ||
+        (snake_pos.x + 1 == b_pos.x + 72) ||
+        (snake_pos.x + 1 == b_pos.x + 73) ||
+        (snake_pos.x + 1 == b_pos.x + 74) ||
+        (snake_pos.x + 1 == b_pos.x + 75) ||
+        (snake_pos.x + 1 == b_pos.x + 76) ||
+        (snake_pos.x + 1 == b_pos.x + 77) ||
+        (snake_pos.x + 1 == b_pos.x + 78) ||
+        (snake_pos.x + 1 == b_pos.x + 79) ||
+        (snake_pos.x + 1 == b_pos.x + 80) ||
+        (snake_pos.x + 1 == b_pos.x + 81) ||
+        (snake_pos.x + 1 == b_pos.x + 82) ||
+        (snake_pos.x + 1 == b_pos.x + 83))
+    ) {
+        
+        //printf("snake collision working \n");
+        //audio feedback
+        if(blocknum > -1)  {
+            b_pos.y = 0;
+            pad.tone(1000.0,0.1);
+        }
+        srn = 4;
+        blocknum = b_number[srn];
+        send=1;
+        blocknum-=1;
+        if(blocknum >= -1)  {  // to make sure that snake doesn't decrease in length if number on the block is less than 1;
+            length-=1;
+        }
+        wait(0.1);
+    }
+    
+    }
\ No newline at end of file