Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
32:3a3bdeffdf62
Parent:
30:461231877c89
Child:
33:249cf423fb18
--- a/Snake/Snake.cpp	Mon Apr 15 06:27:23 2019 +0000
+++ b/Snake/Snake.cpp	Mon Apr 15 13:58:09 2019 +0000
@@ -42,6 +42,7 @@
     }
     //printf("SPRITE %d %d \n", _x[0], _y[0]); 
     if(length == 0)  {
+        pad.init();
         while ((pad.check_event(Gamepad::BACK_PRESSED) == false)) {
             lcd.clear();
             lcd.printString("Game",33,1);
@@ -61,14 +62,13 @@
         }
     }
     
-    if(length > 15)  {
+    if(length >= 16)  {
         lcd.clear();
         lcd.printString("Level Complete",0,1);
         lcd.printString("Press Start",10,3);
         lcd.printString("to Proceed",12,4);
         lcd.refresh();
-        pad.init();
-        while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+        while (pad.check_event(Gamepad::START_PRESSED) == false) {
             pad.leds_on();
             pad.tone(rand()%1001,0.1); //helps to set max freq for random sound
             wait(0.1);
@@ -289,8 +289,9 @@
     _speed = speed;  //Speed changes depending on how much you push the joystick.(As Of Now)
     
     //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.
     for(int i=0; i<=13; i++)  {
-        if(length > i+1)  {
+        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;    
             }
@@ -302,34 +303,27 @@
    //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 (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=3; i<=15; i++)  {
-            if(length < 2)  {
-                _x[1] = _x[0];
-            }
-            else if(length < i)  {
-                _x[i-1] = _x[i-2];
-            }
+    for(int i=2; i<=15; i++)  {
+            
+            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;}
     }
 }