Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
9:d1d79d4ee673
Parent:
8:890b986b16a4
Child:
12:1e601b176437
--- a/Snake/Snake.cpp	Tue Mar 26 18:36:50 2019 +0000
+++ b/Snake/Snake.cpp	Wed Mar 27 18:20:36 2019 +0000
@@ -15,8 +15,7 @@
     {0,1,0},
     {1,0,1},
     {0,1,0},
-
-    };
+};
     
 int m = 0; //Variable used to allow a starting location for the player.
     
@@ -34,19 +33,19 @@
     if(m == 0){
         _x = WIDTH/2;  //Spawns player sprite near the middle of the screen.
         _xa = _x;
-        _xb = _x;
-        _xc = _x;
-        _xd = _x;
-        _xe = _x;
-        _xf = _x;
-        _xg = _x;
-        _xh = _x;
-        _xi = _x;
-        _xj = _x;
-        _xk = _x;
-        _xl = _x;
-        _xm = _x;
-        _xn = _x;
+        _xb = _xa;
+        _xc = _xb;
+        _xd = _xc;
+        _xe = _xd;
+        _xf = _xe;
+        _xg = _xf;
+        _xh = _xg;
+        _xi = _xh;
+        _xj = _xi;
+        _xk = _xj;
+        _xl = _xk;
+        _xm = _xl;
+        _xn = _xm;
         _y = HEIGHT - 3; 
         m = m+1;  
         }
@@ -208,11 +207,71 @@
 
 
 
-Vector2D Snake::get_pos()
+Vector2D Snake::get_pos(int length)
 {
-    Vector2D snakepos = {_x,_y}; //Obtains the snake position.
+    if(length == 1)  {
+        Vector2D snakepos = {_x,_y}; //Obtains the snake position.
+        return snakepos;
+    }
+
+    else if(length == 2)  {
+        Vector2D snakepos = {_xa,(_y-3)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==3)  {
+        Vector2D snakepos = {_xb,(_y-6)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==4)  {
+        Vector2D snakepos = {_xc,(_y-9)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==5)  {
+        Vector2D snakepos = {_xd,(_y-12)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==6)  {
+        Vector2D snakepos = {_xe,(_y-15)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==7)  {
+        Vector2D snakepos = {_xf,(_y-18)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==8)  {
+        Vector2D snakepos = {_xg,(_y-21)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==9)  {
+        Vector2D snakepos = {_xh,(_y-24)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==10)  {
+        Vector2D snakepos = {_xi,(_y-27)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==11)  {
+        Vector2D snakepos = {_xj,(_y-30)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==12)  {
+        Vector2D snakepos = {_xk,(_y-33)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==13)  {
+        Vector2D snakepos = {_xl,(_y-36)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length == 14)  {
+        Vector2D snakepos = {_xm,(_y-39)}; //Obtains the snake position.
+        return snakepos;
+    }
+    else if(length==15)  {
+        Vector2D snakepos = {_xn,(_y-42)}; //Obtains the snake position.
+        return snakepos;
+    }
     //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    return snakepos;
+    return {0,0};
 }
 
 
@@ -455,7 +514,51 @@
         }
     }
 
+// the following makes sure that when the length is increased, the snake stays where it was when it ate food.
 
+    if(length < 2)  {
+        _xa = _x;
+    }
+    else if(length < 3)  {
+        _xb = _xa;
+    }
+    else if(length < 4)  {
+        _xc = _xb;
+    }
+    else if(length < 5)  {
+        _xd = _xc;
+    }
+    else if(length < 6)  {
+        _xe = _xd;
+    }
+    else if(length < 7)  {
+        _xf = _xe;
+    }
+    else if(length < 8)  {
+        _xg = _xf;
+    }
+    else if(length < 9)  {
+        _xh = _xg;
+    }
+    else if(length < 10)  {
+        _xi = _xh;
+    }
+    else if(length < 11)  {
+        _xj = _xi;
+    }
+    else if(length < 12)  {
+        _xk = _xj;
+    }
+    else if(length < 13)  {
+        _xl = _xk;
+    }
+    else if(length < 14)  {
+        _xm = _xl;
+    }
+    else if(length < 15)  {
+        _xn = _xm;
+    }
+    
     //Limits set so that the sprite does not travel off the screen.
     if (_y <= 0) {
         _y = 0;