ELEC2645 (2017/18) / Mbed OS el16ajm
Revision:
5:a3a9e0417e04
Parent:
4:6353f829c56c
Child:
7:c1e0593bfc99
--- a/Snek/Snek.cpp	Sun Apr 29 14:59:14 2018 +0000
+++ b/Snek/Snek.cpp	Sun Apr 29 18:03:45 2018 +0000
@@ -14,27 +14,33 @@
 void Snek::init(int x, int y)
 {
     //Inital values for variables
-    printf ("floats: %i %i \n", _x, _y);
     _length = 3;
+    
+    _oldDirection = 'N';
 
     for (int i = 0; i < _length; i++) {
         _x[i] = x;
-        _y[i] = y;
+        _y[i] = y + i;
     }
 }
 
 void Snek::update(Direction d)
 {
-    if (d == N) {
+    for (int i = _length-1; i >= 1; i--) {
+            _x[i] =  _x[i-1];
+            _y[i] =  _y[i-1];
+    }
+    
+    if (d == N && _oldDirection != 'S') { //checks the current direction so the snake cannot go back on itself
         _y[0] -= 1;
         _oldDirection = 'N';
-    } else if (d == S) {
+    } else if (d == S && _oldDirection != 'N') {
         _y[0] += 1;
         _oldDirection = 'S';
-    } else if (d == E) {
+    } else if (d == E && _oldDirection != 'W') {
         _x[0] += 1;
         _oldDirection = 'E';
-    }   else if (d == W) {
+    }   else if (d == W && _oldDirection != 'E') {
         _x[0] -= 1;
         _oldDirection = 'W';
     } else {
@@ -47,12 +53,7 @@
         }  else if (_oldDirection == 'W') {
             _x[0] -= 1;
         }
-    }
-
-    for (int i = _length; i >= 1; i--) {
-        _x[i] =  _x[i-1];
-        _y[i] =  _y[i-1];
-    }
+    }    
 }
 
 int Snek::getX(int ref)
@@ -69,3 +70,8 @@
 {
     return _length;
 }
+
+void Snek::grow()
+{
+    _length+=1;
+}