Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
24:1c118b071430
Parent:
22:ee698f66146f
Child:
25:e827f1a8fadc
diff -r 592345a70329 -r 1c118b071430 Snake/Snake.cpp
--- a/Snake/Snake.cpp	Wed Apr 10 09:00:45 2019 +0000
+++ b/Snake/Snake.cpp	Wed Apr 10 10:59:15 2019 +0000
@@ -54,169 +54,24 @@
        }
         NVIC_SystemReset(); //Software Reset. //change this condition to loosing screen.
     }
-    else if(length == 1)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    }
-    else if(length == 2)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    }
-    else if(length == 3)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    }
-    else if(length == 4)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    }
-    else if(length == 5)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    }
-    else if(length == 6)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    }
-    else if(length == 7)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    }
-    else if(length == 8)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    }
-    else if(length == 9)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    }
-    else if(length == 10)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
+    
+    for(int a=1; a<=15; a++)  {
+        if(length == a)  {
+            for(int i=0; i<=a-1; i++)  {
+                lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite.
+            }
+        }
     }
-    else if(length == 11)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite);
-    }
-    else if(length == 12)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite);
-    }
-    else if(length == 13)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite);
-    }
-    else if(length == 14)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[13],_y[13],3,3,(int *)snake_sprite);
-    }
-    else if(length == 15)  {
-    lcd.drawSprite(_x[0],_y[0],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-    lcd.drawSprite(_x[1],_y[1],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[2],_y[2],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[3],_y[3],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[4],_y[4],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[5],_y[5],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[6],_y[6],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[7],_y[7],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[8],_y[8],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[9],_y[9],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[10],_y[10],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[11],_y[11],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[12],_y[12],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[13],_y[13],3,3,(int *)snake_sprite);
-    lcd.drawSprite(_x[14],_y[14],3,3,(int *)snake_sprite);
-    }
-    else if(length > 15)  {
+    
+    if(length > 15)  {
         lcd.clear();
         lcd.printString("Level Complete",0,1);
+        lcd.printString("Press Start",10,3);
+        lcd.printString("to Proceed",12,4);
         lcd.refresh();
-        wait(2);
-        lcd.clear();
-        lcd.printString("Press Start",10,1);
-        lcd.printString("to Proceed",12,3);
-        lcd.refresh();
-        int sound = 1000; //helps to set max freq for random sound
         while ( pad.check_event(Gamepad::START_PRESSED) == false) {
             pad.leds_on();
-            pad.tone(rand()%sound,0.1);
+            pad.tone(rand()%1001,0.1); //helps to set max freq for random sound
             wait(0.1);
             pad.leds_off();
             wait(0.1);
@@ -233,619 +88,200 @@
     return length;
 }
 
+
 Vector2D Snake::get_pos(int length)
 {
-    if(length == 1)  {
-        Vector2D snakepos = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length == 2)  {
-        Vector2D snakepos = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==3)  {
-        Vector2D snakepos = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==4)  {
-        Vector2D snakepos = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==5)  {
-        Vector2D snakepos = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==6)  {
-        Vector2D snakepos = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==7)  {
-        Vector2D snakepos = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==8)  {
-        Vector2D snakepos = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos;
+    for(int i=1; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position.
+            return snakepos;
+        }
     }
-    else if(length==9)  {
-        Vector2D snakepos = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==10)  {
-        Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==11)  {
-        Vector2D snakepos = {_x[10],_y[10]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==12)  {
-        Vector2D snakepos = {_x[11],_y[11]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==13)  {
-        Vector2D snakepos = {_x[12],_y[12]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length == 14)  {
-        Vector2D snakepos = {_x[13],_y[13]}; //Obtains the snake position.
-        return snakepos;
-    }
-    else if(length==15)  {
-        Vector2D snakepos = {_x[14],_y[14]}; //Obtains the snake position.
-        return snakepos;
-    }
+    Vector2D snakepos = {-20,-20}; //Obtains the snake position.
+    return snakepos;
     //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos = {-20,-20}; //Obtains the snake position.
-        return snakepos;
-    }
 }
 
 Vector2D Snake::get_pos_before1(int length)  //this sends data of following sprites.
 {
-    if(length == 2)  {
-        Vector2D snakepos_b1 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==3)  {
-        Vector2D snakepos_b1 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==4)  {
-        Vector2D snakepos_b1 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==5)  {
-        Vector2D snakepos_b1 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==6)  {
-        Vector2D snakepos_b1 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==7)  {
-        Vector2D snakepos_b1 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b1 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b1;
+    for(int i=2; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position.
+            return snakepos_b1;
+        }
     }
-    else if(length==9)  {
-        Vector2D snakepos_b1 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b1 = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b1 = {_x[10],_y[10]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b1 = {_x[11],_y[11]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length == 14)  {
-        Vector2D snakepos_b1 = {_x[12],_y[12]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b1 = {_x[13],_y[13]}; //Obtains the snake position.
-        return snakepos_b1;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b1;
-    }
+    Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b1;
+    //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y);
 }
 
 Vector2D Snake::get_pos_before2(int length)  //this sends data of following sprites.
 {
-    if(length == 3)  {
-        Vector2D snakepos_b2 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==4)  {
-        Vector2D snakepos_b2 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==5)  {
-        Vector2D snakepos_b2 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==6)  {
-        Vector2D snakepos_b2 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==7)  {
-        Vector2D snakepos_b2 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b2 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b2 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b2;
+    for(int i=3; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position.
+            return snakepos_b2;
+        }
     }
-    else if(length==10)  {
-        Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b2 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b2 = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b2 = {_x[10],_y[10]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b2 = {_x[11],_y[11]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    else if(length == 15)  {
-        Vector2D snakepos_b2 = {_x[12],_y[12]}; //Obtains the snake position.
-        return snakepos_b2;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b2;
-    }
+    Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b2;
+    //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y);
 }
 
 Vector2D Snake::get_pos_before3(int length)  //this sends data of following sprites.
 {
-    if(length == 4)  {
-        Vector2D snakepos_b3 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==5)  {
-        Vector2D snakepos_b3 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==6)  {
-        Vector2D snakepos_b3 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==7)  {
-        Vector2D snakepos_b3 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b3 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b3 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b3;
+    for(int i=4; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position.
+            return snakepos_b3;
+        }
     }
-    else if(length==10)  {
-        Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b3 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b3 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b3 = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b3 = {_x[10],_y[10]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b3 = {_x[11],_y[11]}; //Obtains the snake position.
-        return snakepos_b3;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b3;
-    }
+    Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b3;
+    //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y);
 }
 
 Vector2D Snake::get_pos_before4(int length)  //this sends data of following sprites.
 {
-    if(length == 5)  {
-        Vector2D snakepos_b4 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==6)  {
-        Vector2D snakepos_b4 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==7)  {
-        Vector2D snakepos_b4 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b4 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b4 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b4;
+    for(int i=5; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position.
+            return snakepos_b4;
+        }
     }
-    else if(length==11)  {
-        Vector2D snakepos_b4 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b4 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b4 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b4 = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b4 = {_x[10],_y[10]}; //Obtains the snake position.
-        return snakepos_b4;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b4;
-    }
+    Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b4;
+    //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y);
 }
 
 Vector2D Snake::get_pos_before5(int length)  //this sends data of following sprites.
 {
-    if(length == 6)  {
-        Vector2D snakepos_b5 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==7)  {
-        Vector2D snakepos_b5 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b5 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b5 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b5;
+    for(int i=6; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position.
+            return snakepos_b5;
+        }
     }
-    else if(length==11)  {
-        Vector2D snakepos_b5 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b5 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b5 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b5 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b5 = {_x[9],_y[9]}; //Obtains the snake position.
-        return snakepos_b5;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b5;
-    }
+    Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b5;
+    //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y);
 }
 
 Vector2D Snake::get_pos_before6(int length)  //this sends data of following sprites.
 {
-    if(length == 7)  {
-        Vector2D snakepos_b6 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==8)  {
-        Vector2D snakepos_b6 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b6 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b6 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b6;
+    for(int i=7; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position.
+            return snakepos_b6;
+        }
     }
-    else if(length==12)  {
-        Vector2D snakepos_b6 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b6 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b6 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b6 = {_x[8],_y[8]}; //Obtains the snake position.
-        return snakepos_b6;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b6;
-    }
+    Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b6;
+    //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y);
 }
 
 Vector2D Snake::get_pos_before7(int length)  //this sends data of following sprites.
 {
-    if(length == 8)  {
-        Vector2D snakepos_b7 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==9)  {
-        Vector2D snakepos_b7 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b7 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b7;
+    for(int i=8; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position.
+            return snakepos_b7;
+        }
     }
-    else if(length==12)  {
-        Vector2D snakepos_b7 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b7 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b7 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b7 = {_x[7],_y[7]}; //Obtains the snake position.
-        return snakepos_b7;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b7;
-    }
+    Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b7;
+    //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y);
 }
 
 Vector2D Snake::get_pos_before8(int length)  //this sends data of following sprites.
 {
-    if(length == 9)  {
-        Vector2D snakepos_b8 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    else if(length==10)  {
-        Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b8 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b8 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b8;
+    for(int i=9; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position.
+            return snakepos_b8;
+        }
     }
-    else if(length==13)  {
-        Vector2D snakepos_b8 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b8 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b8 = {_x[6],_y[6]}; //Obtains the snake position.
-        return snakepos_b8;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b8;
-    }
+    Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b8;
+    //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y);
 }
 
 Vector2D Snake::get_pos_before9(int length)  //this sends data of following sprites.
 {
-    if(length == 10)  {
-        Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b9;
-    }
-    else if(length==11)  {
-        Vector2D snakepos_b9 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b9;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b9 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b9;
+    for(int i=10; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position.
+            return snakepos_b9;
+        }
     }
-    else if(length==13)  {
-        Vector2D snakepos_b9 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b9;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b9 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b9;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b9 = {_x[5],_y[5]}; //Obtains the snake position.
-        return snakepos_b9;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b9;
-    }
+    Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b9;
+    //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y);
 }
 
 Vector2D Snake::get_pos_before10(int length)  //this sends data of following sprites.
 {
-    if(length == 11)  {
-        Vector2D snakepos_b10 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b10;
-    }
-    else if(length==12)  {
-        Vector2D snakepos_b10 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b10;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b10 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b10;
+    for(int i=11; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b10 = {_x[i-11],_y[i-11]}; //Obtains the snake position.
+            return snakepos_b10;
+        }
     }
-    else if(length==14)  {
-        Vector2D snakepos_b10 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b10;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b10 = {_x[4],_y[4]}; //Obtains the snake position.
-        return snakepos_b10;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b10 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b10;
-    }
+    Vector2D snakepos_b10 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b10;
+    //printf("snakepos_b10 from player = %f %f \n", snakepos_b10.x, snakepos_b10.y);
 }
 
 Vector2D Snake::get_pos_before11(int length)  //this sends data of following sprites.
 {
-    if(length == 12)  {
-        Vector2D snakepos_b11 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b11;
-    }
-    else if(length==13)  {
-        Vector2D snakepos_b11 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b11;
+    for(int i=12; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b11 = {_x[i-12],_y[i-12]}; //Obtains the snake position.
+            return snakepos_b11;
+        }
     }
-    else if(length==14)  {
-        Vector2D snakepos_b11 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b11;
-    }
-    else if(length==15)  {
-        Vector2D snakepos_b11 = {_x[3],_y[3]}; //Obtains the snake position.
-        return snakepos_b11;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b11 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b11;
-    }
+    Vector2D snakepos_b11 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b11;
+    //printf("snakepos_b11 from player = %f %f \n", snakepos_b11.x, snakepos_b11.y);
 }
 
 Vector2D Snake::get_pos_before12(int length)  //this sends data of following sprites.
 {
-    if(length == 13)  {
-        Vector2D snakepos_b12 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b12;
-    }
-    else if(length==14)  {
-        Vector2D snakepos_b12 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b12;
+    for(int i=13; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b12 = {_x[i-13],_y[i-13]}; //Obtains the snake position.
+            return snakepos_b12;
+        }
     }
-    else if(length==15)  {
-        Vector2D snakepos_b12 = {_x[2],_y[2]}; //Obtains the snake position.
-        return snakepos_b12;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b12 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b12;
-    }
+    Vector2D snakepos_b12 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b12;
+    //printf("snakepos_b12 from player = %f %f \n", snakepos_b12.x, snakepos_b12.y);
 }
 
 Vector2D Snake::get_pos_before13(int length)  //this sends data of following sprites.
 {
-    if(length == 14)  {
-        Vector2D snakepos_b13 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b13;
+    for(int i=14; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b13 = {_x[i-14],_y[i-14]}; //Obtains the snake position.
+            return snakepos_b13;
+        }
     }
-    else if(length==15)  {
-        Vector2D snakepos_b13 = {_x[1],_y[1]}; //Obtains the snake position.
-        return snakepos_b13;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
-    else  {
-        Vector2D snakepos_b13 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b13;
-    }
+    Vector2D snakepos_b13 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b13;
+    //printf("snakepos_b13 from player = %f %f \n", snakepos_b13.x, snakepos_b13.y);
 }
 
 Vector2D Snake::get_pos_before14(int length)  //this sends data of following sprites.
 {
-    if(length==15)  {
-        Vector2D snakepos_b14 = {_x[0],_y[0]}; //Obtains the snake position.
-        return snakepos_b14;
+    for(int i=15; i<=15; i++)  {
+        if(length == i)  {
+            Vector2D snakepos_b14 = {_x[i-15],_y[i-15]}; //Obtains the snake position.
+            return snakepos_b14;
+        }
     }
-    else  {
-        Vector2D snakepos_b14 = {-20,-20}; //Obtains the snake position.
-        return snakepos_b14;
-    }
-    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
+    Vector2D snakepos_b14 = {-20,-20}; //Obtains the snake position.
+    return snakepos_b14;
+    //printf("snakepos_b14 from player = %f %f \n", snakepos_b14.x, snakepos_b14.y);
 }