ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Files at this revision

API Documentation at this revision

Comitter:
meurigp
Date:
Sun May 01 16:50:37 2016 +0000
Parent:
13:08159ea3d556
Child:
15:a5590211888c
Commit message:
gameTicker implemented, double pause glitch to be fixed

Changed in this revision

Joystick.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/Joystick.lib	Sun May 01 14:43:01 2016 +0000
+++ b/Joystick.lib	Sun May 01 16:50:37 2016 +0000
@@ -1,1 +1,1 @@
-Joystick#c222483c1f05
+Joystick#e444008676a4
--- a/main.cpp	Sun May 01 14:43:01 2016 +0000
+++ b/main.cpp	Sun May 01 16:50:37 2016 +0000
@@ -18,16 +18,16 @@
 /// Serial for debug
 Serial serial(USBTX,USBRX);
 Ticker pollJoystick;
-//Ticker gameTicker;
+Ticker gameTicker;
+
+volatile int game_timer_flag = 0;
  
 FILE *fp; // this is our file pointer
 
-int main()
-{
+void timer_isr();
 
-    wait(1);
-    
-    
+int main()
+{    
     calibrateJoystick();  // get centred values of joystick
     pollJoystick.attach(&updateJoystick,0.5/10.0);  // read joystick 20 times per second
     
@@ -37,16 +37,29 @@
     redLed = 0;
     lcd.init();
     //snakeIntro();
-    hardWall();
+    //hardWall();
+    wrapAround();
     generateFood();
     gamePlaying = true;
     initSnakeTail();
+    button.rise(&buttonISR);
+    gameTicker.attach(&timer_isr,0.1);
  
     while(gamePlaying == true) {
         
-        gameLogic();
+        lcd.setBrightness(1-pot); // turn pot right for brightness
+        if (buttonFlag) {
+            buttonFlag = 0;  
+            if (pauseCount < 3) {
+                gamePaused();
+            }
+        }
+        
+        if(game_timer_flag) {
+            game_timer_flag = 0;
+            gameLogic();
+        }
         if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
-            
             printFlag = 0;
             moveSnake();
             } 
@@ -57,14 +70,10 @@
 
 void generateFood()
 {
-    while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
+    while (randomXoddEven ==1 || randomYoddEven ==1 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
         
         randomX =  rand() % 83 + 1;     // randomX in the range 1 to 83
-        randomY =  rand() % 47 + 1;     // randomY in the range 1 to 47
-   
-    // serial.printf("X = %i\n",randomX); // debug
-    // serial.printf("Y = %i\n\n",randomY);
-        
+        randomY =  rand() % 47 + 1;     // randomY in the range 1 to 47     
         randomXoddEven = randomX%2; // find out whether odd or even
         randomYoddEven = randomY%2;  
         }
@@ -152,7 +161,8 @@
             lcd.refresh();
 
             lcd.drawRect(randomX,randomY,1,1,1); // food
-            hardWall();
+            //hardWall();
+            wrapAround();
             
             if (i == randomX && j == randomY) { // if fruit is eaten
                 greenLed = 0;
@@ -162,7 +172,7 @@
                 newFruitXY();
                 generateFood();
                 }
-            wait(0.1);
+
 }
  
  
@@ -289,18 +299,18 @@
 
 void wrapAround() {
 
-   /* if (i == -1) {
-        i = 83;
+    if (i == -2) {
+        i = 82;
         }
     if (i == 84) {
         i = 0;
         }
-    if (j == -1) {
-        j = 47;
+    if (j == -2) {
+        j = 46;
         }
-    if (j+1 == 48) {
-        j+1 = 0;
-        }  */  
+    if (j == 48) {
+        j = 0;
+        }    
     
 }
 
@@ -366,22 +376,29 @@
  
  void gamePaused() {
      
-   if (pauseCount < 4 && button == 1) {
-        wait_ms(90);
+        wait_ms(100);
         pauseCount++;
-        gamePlaying = false;
+        //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();
-                }
-     
+            lcd.printString(buffer,24,3); }           // display on screen
+            
+        lcd.printString("Paused 5",18,1);
+        wait(1);
+        lcd.printString("Paused 4",18,1);
+        wait(1);
+        lcd.printString("Paused 3",18,1);
+        wait(1);
+        lcd.printString("Paused 2",18,1);
+        wait(1);
+        lcd.printString("Paused 1",18,1);
+        wait(1);
+              
+}
+
+void timer_isr()
+{
+game_timer_flag = 1;   
 }
\ No newline at end of file
--- a/main.h	Sun May 01 14:43:01 2016 +0000
+++ b/main.h	Sun May 01 16:50:37 2016 +0000
@@ -16,12 +16,15 @@
 @brief GPIO output for green LED
 @namespace redLed
 @brief GPIO output for red LED
+@namespace pot
+@brief GPIO input for the POT
 @namespace buzzer
 @brief GPIO output for buzzer
 */
 
 DigitalOut greenLed(PTC2);
 DigitalOut redLed(PTA2);
+AnalogIn pot(PTB10);
 //Beep buzzer(PTA1);
 
 /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
@@ -34,7 +37,14 @@
 };
 CurrentDirection currentDirection = centre; /// intialise direction at beginning
 
-float gameSpeed = 0.1; /*!< determines speed of game according to pot value */
+/// create enumerated type (0,1,2 etc. for different game modes on the menu)
+enum GameType {
+    classic,
+    infiniteMap,
+    hardMap,
+};
+
+
 int randomX =  rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */
 int randomY =  rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */  
 int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */
@@ -45,8 +55,8 @@
 int score = 0; /*!< score for current round */
 int top_score = 0; /*!< top score read and write from the SD card */
 int fruitValue = 10; /*!< value of the fruit */
-int i = 41; /*!< x origin of snake head, intialised at 41 */
-int j = 23; /*!< y origin of snake head, intialised at 23 */
+int i = 40; /*!< x origin of snake head, intialised at 41 */
+int j = 22; /*!< y origin of snake head, intialised at 23 */
 int prev_i; /*!< integer to store previous value of x/i */
 int prev_j; /*!< integer to store previous value of y/j */
 int prev2_i; /*!< integer to store previous, previous value of x/i */