Snake game for ELEC2645 project

Dependencies:   N5110 mbed

Revision:
2:fae6fef4f5ad
Parent:
1:2ab3e61fa258
--- a/main.cpp	Fri Apr 29 16:12:09 2016 +0000
+++ b/main.cpp	Thu May 05 14:53:32 2016 +0000
@@ -3,11 +3,11 @@
 
 //  VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
-BusOut leds(PTC5 , PTC7);
+//BusOut leds(PTC5 , PTC7);
 AnalogIn pot(PTB10);
 
 // change this to alter tolerance of joystick direction
-#define DIRECTION_TOLERANCE 0.05
+//#define DIRECTION_TOLERANCE 0.05L
 
 // connections for joystick
 DigitalIn button(PTB11);
@@ -15,30 +15,35 @@
 AnalogIn yPot(PTB3);
 
 
+int x = 42;
+int y = 24;
+//int gameStart = 0;
+
 int nx= 84;
 int ny= 48;
-int i;
-int j;
+
 int NextGen[84][48]= {0};
 void splashScreen();
 void clearCells();
 void Wall();
 void snakeBody(char x,char y);
+//void joystickMovement();
 
 // timer to regularly read the joystick
-Ticker pollJoystick;
+//Ticker pollJoystick;
 
 // create enumerated type (0,1,2,3 etc. for direction)
 // could be extended for diagonals etc.
+/*
 enum DirectionName {
     UP,
     DOWN,
     LEFT,
     RIGHT,
-    CENTRE,
-    UNKNOWN
+    CENTRE
 };
-
+*/
+/*
 // struct for Joystick
 typedef struct JoyStick Joystick;
 struct JoyStick {
@@ -49,15 +54,15 @@
     int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
     DirectionName direction;  // current direction
 };
-
+*/
 // create struct variable
-Joystick joystick;
+//Joystick joystick;
 
-int printFlag = 0;
+//int printFlag = 0;
 
 // function prototypes
-void calibrateJoystick();
-void updateJoystick();
+//void calibrateJoystick();
+//void updateJoystick();
 
 int main()
 {
@@ -66,49 +71,129 @@
     wait(2);
     lcd.init();
     splashScreen();
-    wait(2);
     clearCells();
-    Wall();
     wait(0.5);
-    snakeBody(42,24);
-    calibrateJoystick();  // get centred values of joystick
-    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
-    while (1){
-      
-       lcd.setBrightness(pot*0.001f);
-   }
-   }
+    Wall();
+    snakeBody(x,y);
+//    calibrateJoystick();  // get centred values of joystick
+//    pollJoystick.attach(&updateJoystick,1.0);  // read joystick 10 times per second
+//    gameStart = 1;
+    while (1)
+    {
+        int x_joystick = xPot.read() * 1000; // float (0->1) to int (0-1000)
+        int y_joystick = yPot.read() * 1000;
+        
+        wait(0.05);
+        
+        if ( ((x_joystick > 30) && (x_joystick < 100)) && ((y_joystick > 0) && (y_joystick < 5)) )
+        {// UP
+            lcd.clearPixel(x,y);
+            lcd.refresh();
+            if (y > 0)
+                --y;
+            lcd.setPixel(x,y);
+            lcd.refresh();
+        }
+        else if ( ((x_joystick > 400) && (x_joystick < 600)) && ((y_joystick > 950) && (y_joystick < 1000)) )
+        { // DOWN
+            lcd.clearPixel(x,y);
+            lcd.refresh();
+            if (y < 48)
+                ++y;
+            lcd.setPixel(x,y);
+            lcd.refresh();
+        }
+        else if ( ((x_joystick > 330) && (x_joystick < 399)) && ((y_joystick > 450) && (y_joystick < 550)) )
+        {// RIGHT
+            lcd.clearPixel(x,y);
+            lcd.refresh();
+            if (x < 84)
+                ++x;
+            lcd.setPixel(x,y);
+            lcd.refresh();
+        }
+        else if ( ((x_joystick > 230) && (x_joystick < 280)) && ((y_joystick > 450) && (y_joystick < 550)) )
+         {//LEFT
+            lcd.clearPixel(x,y);
+            lcd.refresh();
+            if (x > 0)
+                --x; 
+            lcd.setPixel(x,y); 
+            lcd.refresh();      
+        }
+        
+//        joystickMovement();
+//        lcd.setBrightness(pot*0.001f);
+    }
+}
+/*
+// read default positions of the joystick to calibrate later readings
+void calibrateJoystick()
+{
+    button.mode(PullDown);
+    // must not move during calibration
+
+    joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
+    joystick.y0 = yPot;
+}
+*/
+
+/*
+void updateJoystick()
+{
+    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
+    joystick.x = xPot - joystick.x0;
+    joystick.y = yPot - joystick.y0;
+    // read button state
+    joystick.button = button;
+
+    // calculate direction depending on x,y values
+    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
+    if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = CENTRE;
+    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = UP;
+    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = DOWN;
+    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+        joystick.direction = RIGHT;
+    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+        joystick.direction = LEFT;
+    }
+
+    // set flag for printing
+    printFlag = 1;
+}
+*/
+
+/*
+void joystickMovement()
 
 
-int main()
 {
 
-
-    while(1) {
-
+    while(gameStart) {
         if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
             printFlag = 0;
-            serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
 
             // check joystick direction
             if (joystick.direction == UP)
-                serial.printf(" UP\n");
+                j--;
             if (joystick.direction == DOWN)
-                serial.printf(" DOWN\n");
+                j++;
             if (joystick.direction == LEFT)
-                serial.printf(" LEFT\n");
+                i--;
             if (joystick.direction == RIGHT)
-                serial.printf(" RIGHT\n");
-            if (joystick.direction == CENTRE)
-                serial.printf(" CENTRE\n");
-            if (joystick.direction == UNKNOWN)
-                serial.printf(" Unsupported direction\n");
+                i++;
+//           if (joystick.direction == CENTRE)
+//               lcd.printString(" CENTRE ",5,2);
+
 
         }
-
+        sleep();
     }
 }
-
+*/
 
 void splashScreen() //This is the first screen the user sees when the game is turned on
 {
@@ -135,34 +220,8 @@
     wait(0.3);
     lcd.printString("EPIC GAME",15,4);
     wait(0.3);
-//    lcd.drawRect(0,0,82,46,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,78,42,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,74,38,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,70,34,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,66,30,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,62,26,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,58,22,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,54,18,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,50,14,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,46,10,0);
-//   wait(0.1);
-//    lcd.drawRect(0,0,42,6,0);
-//    wait(0.1);
-//    lcd.drawRect(0,0,38,2,0);
-//    wait(0.1);
-    
-    
-    
-    }
+
+}
 
 
 void clearCells()
@@ -178,56 +237,35 @@
 }
 
 
- 
+
 void snakeBody(char x, char y)
 
 {
     lcd.setPixel(x,y);
-    lcd.setPixel(x+1,y);
-    lcd.setPixel(x+2,y);
-    lcd.setPixel(x+3,y);
-    lcd.setPixel(x+4,y);
-    lcd.setPixel(x+5,y);
-    lcd.setPixel(x+6,y);
-    lcd.setPixel(x+7,y);
-    lcd.setPixel(x+8,y);
-    lcd.setPixel(x+9,y);
-    lcd.setPixel(x+10,y);
-    lcd.setPixel(x,y+1);
-    lcd.setPixel(x+1,y+1);
-    lcd.setPixel(x+2,y+1);
-    lcd.setPixel(x+3,y+1);
-    lcd.setPixel(x+4,y+1);
-    lcd.setPixel(x+5,y+1);
-    lcd.setPixel(x+6,y+1);
-    lcd.setPixel(x+7,y+1);
-    lcd.setPixel(x+8,y+1);
-    lcd.setPixel(x+9,y+1);
-    lcd.setPixel(x+10,y+1);
-    
-    
     lcd.refresh();
-    
+
 }
 
+/*
 int Rand (int Min = 1, int Max = 83)
-            {
-                static bool First = true;
-                if (First) srand (time (NULL)), First = false;
-                while (7)
-                    {
-                        int S = rand () % (Max - Min + 1) + Min;
-                        if (!((S - 1)  % 3)) return S;
-                    }
+{
+    static bool First = true;
+    if (First) srand (time (NULL)), First = false;
+    while (7) {
+        int S = rand () % (Max - Min + 1) + Min;
+        if (!((S - 1)  % 3)) return S;
+    }
 }
 
+*/
+
 void Wall ()
 
 {
-    
+
     lcd.drawRect(0,0,82,46,0);
     lcd.drawRect(1,1,80,44,0);
-    
+
 }