Daniel Hamilton / Lab3-SnakeGame

Dependencies:   MCP23S17 mbed

Revision:
1:5fcb94bb03db
Parent:
0:dc906408980e
Child:
2:9c075a0a6d4e
diff -r dc906408980e -r 5fcb94bb03db main.cpp
--- a/main.cpp	Wed Oct 09 16:23:20 2013 +0000
+++ b/main.cpp	Thu Oct 17 04:32:58 2013 +0000
@@ -1,13 +1,72 @@
 #include "mbed.h"
 #include "ledCube.h"
 #include "snake.h"
+#include "main.h"
+
+snake mySnake(snakeStartRow,snakeStartCol);
+food myFood(foodStartRow, foodStartCol);
+ledCube cube;
+AnalogIn joyVer(p19);
+AnalogIn joyHor(p18);
+DigitalIn select(p17);
 
 int main()
 {
-    ledCube cube;
-    snake mySnake(0, 0);
-    
+    printf("Start\n");
+    int snakeUpdateCounter = 0;
+    cube.turnOnLed(snakeStartRow, snakeStartCol, 0);
+    updateFoodLed();
+    printf("Setup Complete\n");
+
     while(1) {
-        
+        // Update snake position if we are greater than the set movement speed
+        if(snakeUpdateCounter++ >= mySnake.movementSpeed) {
+            snakeUpdateCounter = 0;
+            if(mySnake.moveSnake(cube)) {
+                cube.blink();
+            }
+            if(checkForSnakeEating()) {
+                myFood.moveFood(rand() % 5, rand() % 5);
+                updateFoodLed();
+            }
+        }
+        updateDirectionInput();
+        wait(.1);
     }
+    
 }
+
+bool checkForSnakeEating()
+{
+    return mySnake.isEating(myFood.currRow, myFood.currCol);
+}
+
+void updateFoodLed()
+{
+    cube.turnOnLed(myFood.currRow, myFood.currCol, 0);
+}
+
+void updateDirectionInput(){
+   float verValue, horValue;
+    int pushed;
+    select.mode(PullUp);
+        verValue = joyVer;
+        horValue = joyHor;
+        pushed = select;
+        if(horValue < .4){
+            mySnake.movementDirection = Left;
+            printf("Left\n");
+        }
+        else if(horValue > .6){
+            mySnake.movementDirection = Right;
+            printf("Right\n");
+        }
+        if(verValue < .4){
+            mySnake.movementDirection = Down;
+            printf("Down\n");
+        }
+        else if(verValue > .6){
+            mySnake.movementDirection = Up;
+            printf("Up\n");
+        }
+}
\ No newline at end of file