Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
63:205f0ca48473
Parent:
53:527cf297b088
Child:
68:b9cfd27987ac
--- a/GameObjects/Snake/Snake.cpp	Tue Apr 30 14:53:38 2019 +0000
+++ b/GameObjects/Snake/Snake.cpp	Wed May 01 22:35:40 2019 +0000
@@ -27,10 +27,10 @@
 void Snake::draw(N5110 &lcd, int length, int level)
 {
     _length = length;
-    if(m == 0){
+    if(m == 0) {
         _x[0] = WIDTH/2;  //Spawns player sprite near the middle of the screen.
         _y[0] = HEIGHT - 3;
-        
+
         for(int i=0; i<=13; i++)  {
             _x[i+1] = _x[i];
             _y[i+1] = _y[i] - 3;
@@ -39,8 +39,8 @@
         b[14] = 1;
         m = m+1;
     }
-    //printf("SPRITE %d %d \n", _x[0], _y[0]); 
-    
+    //printf("SPRITE %d %d \n", _x[0], _y[0]);
+
     for(int a=1; a<=10; a++)  {
         if(_length == a)  {
             for(int i=0; i<=a-1; i++)  {
@@ -67,8 +67,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position.
-            return snakepos;
+        Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position.
+        return snakepos;
     }
     Vector2D snakepos = {-20,-20}; //Obtains the snake position.
     return snakepos;
@@ -84,8 +84,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position.
-            return snakepos_b1;
+        Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position.
+        return snakepos_b1;
     }
     Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position.
     return snakepos_b1;
@@ -101,8 +101,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position.
-            return snakepos_b2;
+        Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position.
+        return snakepos_b2;
     }
     Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position.
     return snakepos_b2;
@@ -118,8 +118,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position.
-            return snakepos_b3;
+        Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position.
+        return snakepos_b3;
     }
     Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position.
     return snakepos_b3;
@@ -135,8 +135,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position.
-            return snakepos_b4;
+        Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position.
+        return snakepos_b4;
     }
     Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position.
     return snakepos_b4;
@@ -152,8 +152,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position.
-            return snakepos_b5;
+        Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position.
+        return snakepos_b5;
     }
     Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position.
     return snakepos_b5;
@@ -169,8 +169,8 @@
         }
     }
     if(_length >= 10)  {
-            Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position.
-            return snakepos_b6;
+        Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position.
+        return snakepos_b6;
     }
     Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position.
     return snakepos_b6;
@@ -230,44 +230,56 @@
 
 void Snake::update(Direction d, int* b)
 {
-    if(_length >= 10) {_length = 10;} //to stop the snake length virtually at 10 when it goes past it.
-    
+    if(_length >= 10) {
+        _length = 10;   //to stop the snake length virtually at 10 when it goes past it.
+    }
+
     //this makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop
     //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. b[i] also signifies the specific beed number by i.
     for(int i=0; i<=13; i++)  {
         if((_length > i+1)&&(_x[i] != _x[i+1]))  {
             if ((_x[i] > _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1))  {
-                _x[i]-=_speed;    
+                _x[i]-=_speed;
             }
             if ((_x[i] < _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1))  {
-                _x[i]+=_speed;    
+                _x[i]+=_speed;
             }
         }
     }
-   //this makes the controls of W/E directions only exclusive to the top beed in the snake
+    //this makes the controls of W/E directions only exclusive to the top beed in the snake
     for(int i=14; i>=0; i--)  {
-            if((_length == i+1)&&(b[i] == 1))  {
-                
-                if (d == E) {_x[i]+= _speed;}
-                
-                if (d == W) {_x[i]-= _speed;}
-                
+        if((_length == i+1)&&(b[i] == 1))  {
+
+            if (d == E) {
+                _x[i]+= _speed;
             }
+
+            if (d == W) {
+                _x[i]-= _speed;
+            }
+
+        }
     }
 
 // the following makes sure that when the length is increased, the snake stays where it was when it ate food.
 
     for(int i=2; i<=15; i++)  {
-            
-            if(_length < i)  {_x[i-1] = _x[i-2];}
+
+        if(_length < i)  {
+            _x[i-1] = _x[i-2];
+        }
     }
-    
+
     //Limits set so that the snake does not travel off the screen.
     for(int i=0; i<=14; i++)  {
-            
-            if (_x[i] <= 0) {_x[i] = 0;}
-            
-            if (_x[i] > 81) {_x[i] = 81;}
+
+        if (_x[i] <= 0) {
+            _x[i] = 0;
+        }
+
+        if (_x[i] > 81) {
+            _x[i] = 81;
+        }
     }
 }