ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Revision:
12:825a402d230f
Parent:
11:f8478bc749e0
Child:
13:08159ea3d556
--- a/main.cpp	Sun May 01 12:15:09 2016 +0000
+++ b/main.cpp	Sun May 01 13:46:08 2016 +0000
@@ -15,66 +15,11 @@
 // Connections to SD card holder on K64F (SPI interface)
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
  
-//DigitalOut greenLed(PTC2);
-//DigitalOut redLed(PTA2);
-
-//Beep buzzer(PTA1);
-
-/* // timer to regularly read the joystick
+/// Serial for debug
+Serial serial(USBTX,USBRX);
 Ticker pollJoystick;
-// Serial for debug
-Serial serial(USBTX,USBRX);
-
-Ticker gameTicker;
+//Ticker gameTicker;
  
-/// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
-enum CurrentDirection {
-    up,
-    down,
-    left,
-    right,
-    centre,
-};
-CurrentDirection currentDirection = centre; /// intialise direction at beginning
-
-int randomX =  rand() % 83 + 1;     // randomX in the range 1 to 81
-int randomY =  rand() % 47 + 1;     // randomY in the range 1 to 47
-int randomXoddEven = randomX%2; // find out whether odd or even
-int randomYoddEven = randomY%2;
-
-int snakeTailX[100];
-int snakeTailY[100];
-
-
-int snakeTailLength = 3;
-
-int score = 0;
-int top_score = 0;
-int fruitValue = 10;
-
-int i = 41; // snake head origin x
-int j = 23; // snake head origin y
-
-int prev_i;
-int prev_j;
-int prev2_i;
-int prev2_j;
-
-
-bool gamePlaying = false;
-
-// function prototypes
-void generateFood();
-void newFruitXY();
-void moveSnake();
-void hardWall();
-void specialMap();
-void wrapAround();
-void scoreCalculation();
-void gameOver();
-void initSnakeTail();
-void snakeIntro(); */
-
 FILE *fp; // this is our file pointer
 
 int main()
@@ -97,16 +42,13 @@
     gamePlaying = true;
     initSnakeTail();
  
-    while(1) {
+    while(gamePlaying == true) {
         
+        //displayGame();
         if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
-            printFlag = 0;
             
-            if (gamePlaying == true) {
-                moveSnake();
-                }
-            else if (gamePlaying == false) {
-                }
+            printFlag = 0;
+            moveSnake();
             } 
             sleep(); // put the MCU to sleep until an interrupt wakes it up
     }
@@ -190,7 +132,6 @@
                     prev_j = prev2_j;
                  }
                 
-                 
                 
                 if (currentDirection == left) { // shift snake head coordinates according to current direction
                 i -= 2; } // move left
@@ -211,8 +152,6 @@
   
                 lcd.drawRect(randomX,randomY,1,1,1); // food
                 hardWall();
-                //specialMap();
-                //wrapAround();
                 
                 if (i == randomX && j == randomY) { // if fruit is eaten
                     greenLed = 0;
@@ -221,12 +160,13 @@
                     snakeTailLength++;
                     newFruitXY();
                     generateFood();
-                    
                     }
                 wait(0.1);
     }
  }
  
+
+ 
  
 void hardWall() {
 
@@ -349,9 +289,9 @@
       
 }
 
-/*void wrapAround() {
+void wrapAround() {
 
-    if (i == -1) {
+   /* if (i == -1) {
         i = 83;
         }
     if (i == 84) {
@@ -362,23 +302,9 @@
         }
     if (j+1 == 48) {
         j+1 = 0;
-        }    
+        }  */  
     
-    lcd.setPixel(i,j);
-    lcd.setPixel(i,j+1);
-    lcd.setPixel(i+1,j+1);
-    lcd.setPixel(i+1,j);
-    lcd.setPixel(prev_i,prev_j);
-    lcd.setPixel(prev_i,prev_j+1);
-    lcd.setPixel(prev_i+1,prev_j+1);
-    lcd.setPixel(prev_i+1,prev_j);
-    lcd.setPixel(prev2_i,prev2_j);
-    lcd.setPixel(prev2_i,prev2_j+1);
-    lcd.setPixel(prev2_i+1,prev2_j+1);
-    lcd.setPixel(prev2_i+1,prev2_j);
-    
-    lcd.refresh();
-} */
+}
 
 
 void scoreCalculation() {
@@ -438,4 +364,26 @@
     
     lcd.clear();  
     
- }   
\ No newline at end of file
+ }
+ 
+ void gamePaused() {
+     
+   if (pauseCount < 4 && button == 1) {
+        wait_ms(90);
+        pauseCount++;
+        gamePlaying = false;
+        lcd.clear();
+        lcd.printString("Paused",27,1);
+        
+        char buffer[14];
+        int length = sprintf(buffer,"%i left",3-pauseCount); // number of pauses left
+        if (length <= 14) { // if string will fit on display
+            lcd.printString(buffer,27,3); }           // display on screen
+        }
+            if(button == 1) {
+                wait_ms(50);
+                gamePlaying = true;
+                moveSnake();
+                }
+     
+}
\ No newline at end of file