Fiifi Mills / Mbed 2 deprecated Snake

Dependencies:   N5110 PinDetect PowerControl mbed

Revision:
4:07acce768fb6
Parent:
3:8fa09c9a93f3
Child:
5:6dd0bec55e52
diff -r 8fa09c9a93f3 -r 07acce768fb6 main.cpp
--- a/main.cpp	Sun May 03 11:30:19 2015 +0000
+++ b/main.cpp	Sun May 03 15:10:24 2015 +0000
@@ -75,13 +75,22 @@
 
 int printFlag = 0;
 
+int score = 0;
+char buffer [14];
+
 bool gameOver;  // boolean variable for game over
 
 void pressedA()
 {
-    if (currentGameMenu == STARTUP)   // if currentGameMenu is startup and A is pressed, segue to gameplay
+    if (currentGameMenu == STARTUP){   // if currentGameMenu is startup and A is pressed, segue to gameplay
         lcd.clear();
-        currentGameMenu = GAMEPLAY;  // segue to gameplay
+        currentGameMenu = SELECTSPEED;
+        }   
+        
+    else if (currentGameMenu == SELECTSPEED){
+        lcd.clear();
+        currentGameMenu = GAMEPLAY;
+        }
 }
 
 void pressedB()
@@ -89,7 +98,8 @@
     if (currentGameMenu == GAMEPLAY && gameOver == true) { // if current game menu is gameplay AND the game is over, segue to startup
         lcd.clear();
         gameOver = false;  // set gameOver to false
-        currentGameMenu = STARTUP;  // go back to startup screen
+        currentGameMenu = STARTUP;  // go back to startup screen/select speed screen?
+        score = 0;
     }
 
 }
@@ -152,6 +162,20 @@
             Sleep();
             
         }
+        
+        if (currentGameMenu == SELECTSPEED) {
+            
+            serial.printf("Game menu = SELECTSPEED");
+            
+            // code for speed select
+            lcd.printString("SLOW",13,1);
+            lcd.printString("MEDIUM",13,3);
+            lcd.printString("FAST",13,5);
+            
+            // lcd.drawCircle(6,11,3,1);  // slow selection circle
+            // lcd.drawCircle(6,27,3,1);  // medium selection circle
+            // lcd.drawCircle(6,43,3,1);  // fast selection circle
+            }
 
         if (currentGameMenu == GAMEPLAY) {  // in game
 
@@ -184,10 +208,17 @@
             }
 
             if (gameOver == true) {
+                
+                Snake.clear();  // clear snake vector
+                
+                // game over splash screen
+                lcd.printString("Game Over!",13,1);
+                lcd.printString("Press B",22,4);
+                int printScore = sprintf(buffer,"Score = %d",score*5);
+                if (printScore <= 14)
+                    lcd.printString(buffer,13,3);
 
-                lcd.printString("Game Over!",10,2);
-                lcd.printString("Press B",20,3);
-                Snake.clear();
+                
                 Sleep();
             }
         }
@@ -236,28 +267,27 @@
 {
     srand(time(NULL));
     
-    // int foodX = rand()%84;  // random x coordinate between 0-83
-    // int foodY = rand()%48;  // random y coordinate between 0-47
-    int foodX = rand()%30 + 30;
-    int foodY = rand()%24 + 12;
+    int foodX = rand()%83+1;  // random x coordinate between 1-83
+    int foodY = rand()%47+1;  // random y coordinate between 1-47
+                              
     
-    lcd.setPixel(foodX,foodY);
+    lcd.setPixel(foodX,foodY);  // set pixel of this coordinate (used for food)
     lcd.refresh();
     
-    // eventual border
+    // draw border
     lcd.drawRect(0,0,83,47,0);  // x-origin, y-origin, width, height, fill
     lcd.refresh();
 
     while (1) {
 
-        if (joystick.direction == CENTRE) {
-            currentDirection = previousDirection;
+        if (joystick.direction == CENTRE) {  // if the joystick is in the centre
+            currentDirection = previousDirection;  // current direction is the previous direction of movement
         }
-        if (joystick.direction == UNKNOWN) {
-            currentDirection = previousDirection;
+        if (joystick.direction == UNKNOWN) {  // same for when joystick direction is unknown
+            currentDirection = previousDirection;  // current direction is the previous direction
         }
-        if (joystick.direction == LEFT) {
-            currentDirection = joystick.direction;
+        if (joystick.direction == LEFT) {  // if joystick direction is left, right, up or down
+            currentDirection = joystick.direction;  // set the current direction to be th joystick's direction
         }
         if (joystick.direction == RIGHT) {
             currentDirection = joystick.direction;
@@ -271,7 +301,7 @@
 
         lcd.clearPixel(Snake.front().x, Snake.front().y);  // clearing tail pixel of snake
 
-        for (int i=0; i<Snake.size()-1; i++) {
+        for (int i=0; i<Snake.size()-1; i++) {  // shift along each element in the vector so that..
             Snake.at(i) = Snake.at(i+1);  // value of element 0=1, 1=2 etc.
         }
         
@@ -284,8 +314,8 @@
                     currentDirection = DOWN;  // if so, "do nothing"
                     Snake.back().y++;
                 } else {  
-                    previousDirection = UP;  // if the previous direction is also current direction
-                    Snake.back().y--;  // move in current direction
+                    previousDirection = UP;  // else, set the previous direction
+                    Snake.back().y--;  // increment appropiate coordinate value
                 }
                 break;
 
@@ -328,63 +358,47 @@
             switch (currentDirection) {  // check the current direction
 
                 case UP:
-                    
-                    Snake.push_back(Snake.back());  // add an element in front of the snake head in the direction of movement
-                    Snake.back().y--;  
+                    Snake.push_back(Snake.back());  // add an element to the vector past the last element, equal to snakeHead's coordinates
+                    Snake.back().y--;  // increment appropiate coordinate
+                    foodX = rand()%83 + 1;  // x coordinate of food is now a new random number between 1-83
+                    foodY = rand()%47 + 1;  // y coordinate of food is now a new random number between 1-47
                     
-                    //foodX = rand()%84;
-                    //foodY = rand()%48;
-                    foodX = rand()%30 + 30;  // for debugging purposes
-                    foodY = rand()%24 + 12;
-                    lcd.setPixel(foodX,foodY);
+                    lcd.setPixel(foodX,foodY);  // set pixel of new food
                     lcd.refresh();
-                    
+                    score++;
                     break;
 
                 case DOWN:
-                   
                     Snake.push_back(Snake.back());
                     Snake.back().y++;
-                    
-                    
+                    foodX = rand()%83 + 1;
+                    foodY = rand()%47 + 1;
                     
-                    //foodX = rand()%84;
-                    //foodY = rand()%48;
-                    foodX = rand()%30 + 30;
-                    foodY = rand()%24 + 12;
                     lcd.setPixel(foodX,foodY);
                     lcd.refresh();
-                    
+                    score++;
                     break;
 
                 case LEFT:
-                    
                     Snake.push_back(Snake.back());
                     Snake.back().x--;
-                    
+                    foodX = rand()%83 + 1;
+                    foodY = rand()%47 + 1;
                     
-                    //foodX = rand()%84;
-                    //foodY = rand()%48;
-                    foodX = rand()%30 + 30;
-                    foodY = rand()%24 + 12;
                     lcd.setPixel(foodX,foodY);
                     lcd.refresh();
-                    
+                    score++;
                     break;
 
                 case RIGHT:
-                    
                     Snake.push_back(Snake.back());
                     Snake.back().x++;
-                    
+                    foodX = rand()%83 + 1;
+                    foodY = rand()%47 + 1;
                     
-                    //foodX = rand()%84;
-                    //foodY = rand()%48;
-                    foodX = rand()%30 + 30;
-                    foodY = rand()%24 + 12;
                     lcd.setPixel(foodX,foodY);
                     lcd.refresh();
-                    
+                    score++;
                     break;
 
                 default:
@@ -394,22 +408,22 @@
         }
 
 
-        if (Snake.back().x > 82) {
+        if (Snake.back().x > 82) {  // if snakeHead's x coordinate is larger than 83 (i.e touching the right hand side border)
+            gameOver = true;  // game over is true
+            break;  // break out of infinite loop
+        }
+
+        if (Snake.back().x < 1) {  // touching left hand side border
             gameOver = true;
             break;
         }
 
-        if (Snake.back().x < 1) {
+        if (Snake.back().y > 46) {  // touching top border
             gameOver = true;
             break;
         }
 
-        if (Snake.back().y > 46) {
-            gameOver = true;
-            break;
-        }
-
-        if (Snake.back().y < 1) {
+        if (Snake.back().y < 1) {  // touching bottom border
             gameOver = true;
             break;
         }
@@ -423,10 +437,8 @@
         break;
     }
 
-        lcd.setPixel(Snake.back().x, Snake.back().y);
+        lcd.setPixel(Snake.back().x, Snake.back().y);  // set snakeHead's pixel
         lcd.refresh();
-        
-        
         wait(0.1);