ELEC2645 (2017/18) / Mbed OS el16ajm
Revision:
7:c1e0593bfc99
Parent:
6:ed553fd191c2
Child:
8:9d01fd4a63ad
--- a/Engine/Engine.cpp	Mon Apr 30 15:07:08 2018 +0000
+++ b/Engine/Engine.cpp	Mon Apr 30 18:14:24 2018 +0000
@@ -32,6 +32,15 @@
             }
         }
     }
+    
+    lcd.printString("Score",48,0);
+
+
+    int _score = (_solid.getLength() - 3);
+
+    char buffer1[14];
+    sprintf(buffer1,"%2d",_score);
+    lcd.printString(buffer1,48,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
 
     if (_gameOver) {
         for (int i = 0; i <= 22; i++) {
@@ -41,16 +50,7 @@
                 lcd.refresh();
             }
         }
-    }
-
-    lcd.printString("Score",48,0);
-
-
-    int _score = (_solid.getLength() - 3);
-
-    char buffer1[14];
-    sprintf(buffer1,"%2d",_score);
-    lcd.printString(buffer1,48,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    }    
 }
 
 void Engine::update(Gamepad &pad)
@@ -73,7 +73,17 @@
 
     if (_grid[_solid.getX(0)][_solid.getY(0)] == 2) {
         if (_solid.getLength()<100) {
-            _solid.grow();
+
+            _solid.grow();  //makes snake longer
+
+            bool empty = false; //makes a varible for finding an empty square
+            while (!empty) {    //loops until empty square found
+                _noodles.random(); //randomise position of noodles
+                if ( _grid[_noodles.getY()][_noodles.getY()] == 0) {
+                    empty = true; //stops looping when free space is found
+                    // no need to set the grid as it will put the noodles in the new location the next time 'update' is called
+                }
+            }
         }
     }
 }