Completed Snake Program

Dependencies:   N5110 PinDetect PowerControl mbed

Fork of DocTest by Craig Evans

Files at this revision

API Documentation at this revision

Comitter:
MBirney
Date:
Thu Apr 30 20:53:56 2015 +0000
Parent:
7:2942e97924f0
Child:
9:77b83754460c
Commit message:
Game working with 2 extra added modes (classic and boundaries)

Changed in this revision

N5110.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/N5110.lib	Thu Apr 30 15:39:20 2015 +0000
+++ b/N5110.lib	Thu Apr 30 20:53:56 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/eencae/code/N5110/#780a542d5f8b
+http://mbed.org/users/eencae/code/N5110/#ba8addc061ea
--- a/main.cpp	Thu Apr 30 15:39:20 2015 +0000
+++ b/main.cpp	Thu Apr 30 20:53:56 2015 +0000
@@ -17,8 +17,13 @@
     HARD,
 };
 
+enum GameMode {
+    CLASSIC,
+    BOUNDARY,
+};
+
 Difficulty currentDifficulty=EASY;// initialise to easy mode
-
+GameMode   selectedGameMode=CLASSIC;
 
 // struct for Joystick
 typedef struct JoyStick Joystick;
@@ -74,7 +79,7 @@
 
 void displaySplash()
 {
- 
+
     lcd.inverseMode();      // change colour mode
     lcd.setBrightness(0.5); // put LED backlight on 50%
 
@@ -114,7 +119,7 @@
 
     gameSpeed= 1.0/5; // set easy game speed(for game time)
 }
-void mediumSelected() // display when medium is selected 
+void mediumSelected() // display when medium is selected
 {
     currentDifficulty=MEDIUM;
     lcd.clear();
@@ -127,7 +132,7 @@
     lcd.drawCircle(10,27,2,0);
     lcd.drawCircle(10,35,2,1);
     lcd.drawCircle(10,43,2,0);
-    gameSpeed=1.0/7; // set medium game speed 
+    gameSpeed=1.0/7; // set medium game speed
 }
 
 void hardSelected() // display when hard is selected
@@ -146,6 +151,76 @@
     gameSpeed=1.0/12; // set hard game speed
 }
 
+void classicModeSelected()
+{
+
+
+    selectedGameMode=CLASSIC;
+    lcd.clear();
+    lcd.printString("Please Select",2,0);
+    lcd.printString("Game Mode:",2,1);
+    lcd.printString("Classic",20,3);
+    lcd.printString("Boundary",20,4);
+    lcd.refresh();
+    lcd.drawCircle(10,27,2,1);
+    lcd.drawCircle(10,35,2,0);
+
+}
+
+void boundaryModeSelected()
+{
+    selectedGameMode=BOUNDARY;
+    lcd.clear();
+    lcd.printString("Please Select",2,0);
+    lcd.printString("Game Mode:",2,1);
+    lcd.printString("Classic",20,3);
+    lcd.printString("Boundary",20,4);
+    lcd.refresh();
+    lcd.drawCircle(10,27,2,0);
+    lcd.drawCircle(10,35,2,1);
+
+}
+
+
+void checkSelectedGameMode()
+{
+
+    switch(selectedGameMode) {
+        case CLASSIC://classicMode;
+
+            switch (joystick.direction) {
+
+                case UP:
+                    boundaryModeSelected();
+                    break;
+
+
+                case DOWN:
+                    boundaryModeSelected();
+                    break;
+            }
+            break;
+
+
+        case BOUNDARY:
+
+            switch (joystick.direction) {
+
+                case UP:
+                    classicModeSelected();
+                    break;
+
+
+                case DOWN:
+                    classicModeSelected();
+                    break;
+            }
+
+            break;
+    }
+    wait(0.2);
+
+}
 
 void checkSelectedDifficulty()
 {
@@ -210,8 +285,8 @@
 
 void startingSnake()
 {
-   snakeX.resize(5);
-   snakeY.resize(5);
+    snakeX.resize(5);
+    snakeY.resize(5);
     snakeX[0]=20;
     snakeX[1]=22;
     snakeX[2]=24;
@@ -222,7 +297,7 @@
     snakeY[2]=27;
     snakeY[3]=27;
     snakeY[4]=27;
-    
+
 
     for (int i=0; i<5; i++) {
         // lcd.setPixel(snakeX[i],snakeY[i]);
@@ -261,7 +336,7 @@
 //serial.printf("%d",randomX);
 }
 
-void Boundary()
+void hardBoundary()
 {
 
 
@@ -281,9 +356,26 @@
     lcd.refresh();
 
 }
+void classicBoundary()
+{
 
 
+    for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=8
+        lcd.setPixel(x,8);
+    }
+    for(int x = 1; x< 83; x+=2) { // draw 1 to 82 at y=47
+        lcd.setPixel(x,47);
+    }
+    for(int y = 8; y< 48; y+=2) {// draw 8 tp 47 at x=1
+        lcd.setPixel(1,y);
+    }
+    for(int y = 8; y< 48; y+=2) {// draw 8 to 47 at x =82
+        lcd.setPixel(82,y);
+    }
 
+    lcd.refresh();
+
+}
 
 
 void updateSnakeArray()
@@ -323,33 +415,38 @@
         case UP:
             snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2];
             snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]-2;
-            
- if(snakeY[snakeY.size()-1] <9) snakeY[snakeY.size()-1]=45;
+
+           if (selectedGameMode==CLASSIC){
+             if(snakeY[snakeY.size()-1] <9) snakeY[snakeY.size()-1]=45;
+             }
             break;
 
         case DOWN:
-          snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2];
+            snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2];
             snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2]+2;
             
+          if (selectedGameMode==CLASSIC){
             if(snakeY[snakeY.size()-1] >45) snakeY[snakeY.size()-1]=9;
+            }
             break;
 
         case LEFT:
             snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]-2;
+            snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
             
-            if(snakeX[snakeX.size()-1] <2) snakeX[snakeX.size()-1]=80;
-    
-            snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
+             if (selectedGameMode==CLASSIC){
+             if(snakeX[snakeX.size()-1] <2) snakeX[snakeX.size()-1]=80;
+             }
             break;
 
         case RIGHT:
             snakeX[snakeX.size()-1]= snakeX[snakeX.size()-2]+2;
-          
-          if(snakeX[snakeX.size()-1] >80) snakeX[snakeX.size()-1]=2;
-         
+            snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
             
-            snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
-          
+             if (selectedGameMode==CLASSIC){
+             if(snakeX[snakeX.size()-1] >80) snakeX[snakeX.size()-1]=2;
+             }
+
             break;
 
         case CENTRE:
@@ -365,20 +462,20 @@
 }
 void playTune()
 {
-    
+
     //float frequency[]={440,659};
-   // float beat[]={1,1,};
-    // Buzzer=0.3; 
-      //  Buzzer.period(1/(440));// time =1/f 
-        //wait(1);
-        //Buzzer.period(1/(659));
-     
-        //Buzzer=0;
-        
-           // set PWM period
-             
-// set duty cycle 
-     //wait(0.5*beat[i]);     
+    // float beat[]={1,1,};
+    // Buzzer=0.3;
+    //  Buzzer.period(1/(440));// time =1/f
+    //wait(1);
+    //Buzzer.period(1/(659));
+
+    //Buzzer=0;
+
+    // set PWM period
+
+// set duty cycle
+    //wait(0.5*beat[i]);
 }
 void gameOver()
 {
@@ -412,11 +509,11 @@
     lcd.printString("Press Reset" ,2,3);
     lcd.printString("Button To" ,10,4);
     lcd.printString("Play Again" ,20,5);
-    
+
     //
     lcd.refresh();
-   playTune();
-    
+    playTune();
+
     //gamePlaying=0;
 
 
@@ -499,8 +596,14 @@
 
 void startButtonPressed()
 {
-    gamePlaying=1;
-    // myleds=15;
+    if (menuState==0) {
+        menuState=1;
+    }
+
+  else  if (menuState==1) {
+        menuState=2;
+    }
+
 }
 
 
@@ -561,22 +664,30 @@
 }
 void startUpMenu()
 {
-lcd.inverseMode();
+    lcd.inverseMode();
     easySelected();
     joystick.direction=UNKNOWN;
     calibrateJoystick();  // get centred values of joystick
-     
+
+}
+
+void ModeMenu()
+{
+    classicModeSelected();
+    joystick.direction=UNKNOWN;
+    calibrateJoystick();  // get centred values of joystick
 }
 
 void resetButtonPressed()
 {
-    gamePlaying=0;
+    //gamePlaying=0;
+    menuState=0;
 }
 
 
 int main()
 {
-    leds=1;
+   
     lcd.init();
     resetButton.mode(PullDown);
     startButton.mode(PullDown);
@@ -584,36 +695,56 @@
     startButton.rise(&startButtonPressed);
     resetButton.rise(&resetButtonPressed);
     button.rise(&pauseButtonPressed);
-      displaySplash();
+    displaySplash();
+     leds=1;
     wait(4);
 //serial.printf("starting");
     while(1) {
+        leds=1;
         startUpMenu();
-        while (gamePlaying==0) {
+        while (menuState==0) {
             updateJoystick();
             checkSelectedDifficulty();
             updateBrightness();
             //serial.printf("check difficulty loop");
+            
+             if (menuState==1){
+                ModeMenu();
+                }
         }
 
-         if (gamePlaying==1) {
+
+        while(menuState==1) {
+           // ModeMenu();
+           
+            updateJoystick();
+            checkSelectedGameMode();
+            updateBrightness();
+           
+        }
+
+        if (menuState==2) {
 
             lcd.clear();
-            lcd.normalMode();      // normal colour mode
-            Boundary();
+            lcd.normalMode();  
+            if (selectedGameMode==BOUNDARY) hardBoundary();
+            if (selectedGameMode==CLASSIC) classicBoundary();
+            
+   // normal colour mode
+         
             previousDirection=RIGHT;
-            joystick.direction=RIGHT; // make sure when game reset that joystick is reset to right 
+            joystick.direction=RIGHT; // make sure when game reset that joystick is reset to right
             startingSnake();
             randomiseFood();
             int score=5;
-    int length = sprintf(buffer,"%2d",score);
+            int length = sprintf(buffer,"%2d",score);
             if (length <= 14)  // if string will fit on display
                 lcd.printString(buffer,0,0);
             // lcd.drawRect(0,0,83,7,0);
-            lcd.refresh();    
-             startGame.attach(&updateGameISR,gameSpeed);
-           
-            while (gamePlaying==1) {
+            lcd.refresh();
+            startGame.attach(&updateGameISR,gameSpeed);
+
+            while (menuState==2) {
 
                 //  serial.printf("enter game loop");
                 if(updateGameFlag==1) {
@@ -626,20 +757,20 @@
                     updateSnakeArray();
                     for (int i=0; i<snakeX.size(); i++) {
                         lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
-                  // lcd.setPixel(snakeX[i],snakeY[i]);
-
-
+                        // lcd.setPixel(snakeX[i],snakeY[i]);
                     }
                     lcd.refresh();
                     previousDirection=joystick.direction;
 
-                    //checkForCollision();
+                    if (selectedGameMode==BOUNDARY) checkForCollision();
+                    
+                    
                     checkForFood();
 
 
                     for (int i=0; i<snakeX.size()-1; i++) {
                         checkForCollisionWithSelf(i);
-                         updateBrightness();
+                        updateBrightness();
                         //serial.printf("%d",snakeX.size());
                         // printVectorContent();
 
--- a/main.h	Thu Apr 30 15:39:20 2015 +0000
+++ b/main.h	Thu Apr 30 20:53:56 2015 +0000
@@ -10,16 +10,18 @@
 #include <vector>
 #include "mbed.h"
 #include "N5110.h"
-//#include "PinDetect.h"
-
-// change this to alter tolerance of joystick direction
 #define DIRECTION_TOLERANCE 0.1
 
 AnalogIn pot(p20);
 PwmOut Buzzer(p21);
 
 //        VCC,SCE,RST,D/C,MOSI,SCLK,LED
+/** 
+@namespace lcd
+@brief connections lcd mosi connections for nokia N5110
+*/ 
 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
+
 BusOut myleds(LED1,LED2,LED3,LED4);
 BusOut leds(p22,p23,p24);
 InterruptIn startButton(p5);
@@ -42,11 +44,9 @@
 bool gamePaused=0;
 vector<int> snakeX (5);
 vector<int> snakeY (5,27);
-vector<int> shape(5);
-int foodX[2];
-int foodY[2];
-int gameState=0;
-int gameMode=0;
+int foodX[1];
+int foodY[1];
+int menuState=0;
 
 
 float gameSpeed;//change depending on difficulty selected