Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
27:3b4775a0d0bb
Parent:
26:3495f7b0ede7
Child:
28:d2c621d67e3b
--- a/SnakevsBlock/SnakevsBlock.cpp	Thu Apr 11 05:34:03 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Thu Apr 11 06:50:47 2019 +0000
@@ -38,8 +38,7 @@
     _mag = pad.get_mag(); //Obtains Magnitude of Joystick.
 }
 
-void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)
-{
+void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)   {
     length = _s.draw(pad, lcd, length, level); //Draws the Snake.     //Make these snake buffs relative to the snake drops which in turn relate to the game speed
     if(foodbuff >=0)  {
         _f.draw(lcd, blockgap, blockbuff); //Draws the first food.
@@ -57,12 +56,10 @@
     if(foodbuff == 8) {
         blockbuff = 0;
     }
-    
     //Code to print length on game screen.
     char bufferscore[14];
     sprintf(bufferscore,"%d",length);
     lcd.printString(bufferscore,1,0);    
-    
 }
 
 
@@ -114,16 +111,12 @@
 }   
     
 
-void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)
-    {
+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 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.
@@ -136,7 +129,6 @@
             (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);
@@ -145,10 +137,7 @@
                 f_pos.y = -2;
             }
             length+=1;
-            
         }
-        
-    
     if (
         ((snake_pos.y + i== ff_pos.y) ||
         (snake_pos.y + i == ff_pos.y + 1) ||
@@ -206,138 +195,40 @@
         //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 i=1; i<=18; i++)  {    //this loop automatically detects each combination of collision     
-        if (
-            ((snake_pos.y == b_pos.y + 10)) &&
-            ((snake_pos.x + 1 == b_pos.x + i))
-        ) {
-            
-            //printf("snake collision working \n");
-            //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;
-            }
-            srn = 0;
-            blocknum = b_number[srn];
-            send=1;
-            blocknum-=1;
-            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;
+    for(int block=1; block<=65; block+=16)  {    //this loop automatically detects for each section of block
+        if(block == 65) {
+                    block += 2;
+                }
+        for(int i = block; i <= (block+15); i++)  {    //this loop automatically detects each combination of collision     
+            if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + i))  {
+                
+                //printf("snake collision working \n");
+                //audio feedback
+                if(blocknum > 0)  {
+                    b_pos.y = 0;
+                }
+                if(block == 64) {
+                    block += 2;
+                }
+                srn = ((block + 5)/17);
+                blocknum = b_number[srn];
+                ImplementCollision(pad);
             }
         }
     }
-    for(int i=19; i<=34; i++)  {    //this loop automatically detects each combination of collision
-        if (
-            ((snake_pos.y == b_pos.y + 10)) &&
-            ((snake_pos.x + 1 == b_pos.x + i))
-        ) {
-            
-            //printf("snake collision working \n");
-            //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;
-            }
-            srn = 1;
-            blocknum = b_number[srn];
-            send=1;
-            blocknum-=1;
-            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;
-            }
-            
-        }
+}
+    
+void SnakevsBlock::ImplementCollision(Gamepad &pad)  {
+    send=1;
+    blocknum-=1;
+    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);
     }
-    for(int i=35; i<=50; i++)  {    //this loop automatically detects each combination of collision
-        if (
-            ((snake_pos.y == b_pos.y + 10)) &&
-            ((snake_pos.x + 1 == b_pos.x + i))
-        ) {
-            
-            //printf("snake collision working \n");
-            //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;
-            }
-            srn = 2;
-            blocknum = b_number[srn];
-            send=1;
-            blocknum-=1;
-            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;
-            }
-            
-        }
-    }
-    for(int i=51; i<=66; i++)  {    //this loop automatically detects each combination of collision
-        if (
-            ((snake_pos.y == b_pos.y + 10)) &&
-            ((snake_pos.x + 1 == b_pos.x + i))
-        ) {
-            
-            //printf("snake collision working \n");
-            //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;
-            }
-            srn = 3;
-            blocknum = b_number[srn];
-            send=1;
-            blocknum-=1;
-            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);
-                wait(0.05);
-            }
-            else  {
-                speed = 1;
-            }
-            
-        }
-    }
-    for(int i=67; i<=83; i++)  {    //this loop automatically detects each combination of collision
-        if (
-            ((snake_pos.y == b_pos.y + 10)) &&
-            ((snake_pos.x + 1 == b_pos.x + i))
-        ) {
-            
-            //printf("snake collision working \n");
-            //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;
-            }
-            srn = 4;
-            blocknum = b_number[srn];
-            send=1;
-            blocknum-=1;
-            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;
-            }
-        }
-        
+    else  {
+        speed = 1;
     }
 }