ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Revision:
15:a5590211888c
Parent:
14:56e355c5cfc9
Child:
16:68b9460d4c76
--- a/main.cpp	Sun May 01 16:50:37 2016 +0000
+++ b/main.cpp	Wed May 04 12:33:52 2016 +0000
@@ -1,4 +1,9 @@
+/**
+@file main.cpp
 
+@brief Program implementation of Snake Game
+
+*/
  
 #include "mbed.h"
 #include "main.h"
@@ -6,6 +11,7 @@
 #include "beep.h"
 #include "SDFileSystem.h"
 #include "Joystick.h"
+#include "fsmMenu.h"
  
 //         VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
 N5110 lcd (PTD3, PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
@@ -45,10 +51,11 @@
     button.rise(&buttonISR);
     gameTicker.attach(&timer_isr,0.1);
  
-    while(gamePlaying == true) {
+  /*  while(gamePlaying == true) {
         
         lcd.setBrightness(1-pot); // turn pot right for brightness
-        if (buttonFlag) {
+        
+        if (buttonFlag ==1) {
             buttonFlag = 0;  
             if (pauseCount < 3) {
                 gamePaused();
@@ -64,7 +71,7 @@
             moveSnake();
             } 
             sleep(); // put the MCU to sleep until an interrupt wakes it up
-    }
+    } */
 }
  
 
@@ -120,20 +127,14 @@
 
             lcd.clear();
             greenLed = 1;
-            if (snakeTailLength > 4) {
-            for (int q=1; q<snakeTailLength; q++) {
-                if (snakeTailX[q] == i && snakeTailY[q] == j) {
-                    gameOver();
-                    }
-                }
-            }
+            
             
             prev_i = snakeTailX[0];
             prev_j = snakeTailY[0]; 
             snakeTailX[0] = i; 
             snakeTailY[0] = j;
 
-            for (int a=1; a<snakeTailLength; a++) {
+            for (int a=1; a<snakeTailLength; a++) { // loops through snakeTail array and assigns previous seg's coordinates to current one
                 prev2_i = snakeTailX[a]; 
                 prev2_j = snakeTailY[a];
                 snakeTailX[a] = prev_i;
@@ -166,12 +167,20 @@
             
             if (i == randomX && j == randomY) { // if fruit is eaten
                 greenLed = 0;
-                //buzzer.beep(2000,0.2); 
+                buzzer.beep(2000,0.2); 
                 scoreCalculation();
                 snakeTailLength++;
                 newFruitXY();
                 generateFood();
                 }
+                
+            if (snakeTailLength > 4) { // if snake eats itself
+            for (int q=1; q<snakeTailLength; q++) {
+                if (snakeTailX[q] == i && snakeTailY[q] == j) {
+                    gameOver();
+                    }
+                }
+            }
 
 }
  
@@ -214,17 +223,17 @@
     }
      gamePlaying = false;
         lcd.clear();
-        //buzzer.beep(294,0.5);
+        buzzer.beep(294,0.5);
         wait(0.5);
         lcd.printString("Game Over",15,0); // width(6) * n(9) = 54, 84-54 = 30, 30/2 = 15
-        //buzzer.beep(277,0.5);
+        buzzer.beep(277,0.5);
         wait(0.5);
         
         char buffer[14];
         int length = sprintf(buffer,"Score:%i",score); // display score of round
         if (length <= 14) { // if string will fit on display
             lcd.printString(buffer,0,1); }           // display on screen
-        //buzzer.beep(262,2);
+        buzzer.beep(262,2);
         
          ////////////////////// Simple reading example //////////////////////////
 
@@ -358,16 +367,16 @@
     }  // x,y,radius,white fill
     
     lcd.printString("Snake",27,0);
-    //buzzer.beep(2000,0.5);   
+    buzzer.beep(2000,0.5);   
     wait(0.5);
     lcd.printString("by",36,2);
-    //buzzer.beep(1000,0.5);
+    buzzer.beep(1000,0.5);
     wait(0.5);
     lcd.printString("Meurig",24,3);
-    //buzzer.beep(500,0.5);
+    buzzer.beep(500,0.5);
     wait(0.5);
     lcd.printString("Phillips",18,4);
-    //buzzer.beep(250,2);
+    buzzer.beep(250,2);
     wait(2);
     
     lcd.clear();