renhao dai / Mbed 2 deprecated Joystick_snake_game

Dependencies:   N5110 mbed

Revision:
1:f682aeb462f1
Parent:
0:026fa541af7a
Child:
2:d7b17623ba26
diff -r 026fa541af7a -r f682aeb462f1 main.cpp
--- a/main.cpp	Sun Mar 08 16:43:02 2015 +0000
+++ b/main.cpp	Tue Apr 28 12:27:00 2015 +0000
@@ -1,22 +1,21 @@
-/* Joystick
-
-Example code of how to read a joystick
-
-https://www.sparkfun.com/products/9032
-
-Craig A. Evans
-7 March 2015
-*/
-
 #include "mbed.h"
-
-// change this to alter tolerance of joystick direction
+#include "N5110.h"
 #define DIRECTION_TOLERANCE 0.05
 
+
+//pins in
+N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
+
 // connections for joystick
-DigitalIn button(p18);
-AnalogIn xPot(p19);
-AnalogIn yPot(p20);
+DigitalIn button(p19);
+AnalogIn xPot(p15);
+AnalogIn yPot(p16);
+
+int i;
+int j;
+
+int snakeX[100];
+int snakeY[100];
 
 // timer to regularly read the joystick
 Ticker pollJoystick;
@@ -50,39 +49,155 @@
 int printFlag = 0;
 
 // function prototypes
-void calibrateJoystick();
-void updateJoystick();
+//void setPixel(int x, int y);
+//void clearPixel(int x, int y);
+
 
-int main()
-{
-    calibrateJoystick();  // get centred values of joystick
-    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
+typedef struct Nut Nut;
+struct Nut {
+    int X;
+    int Y;
+    int yes;
+};
+Nut nut;
 
-    while(1) {
-
-        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);
+typedef struct Snake Snake;
+struct Snake {
+    int snakeX[100];
+    int snakeY[100];
+    int node;
+    int life;
+    DirectionName direction;
+};
+Snake snake;
 
-            // check joystick direction
-            if (joystick.direction == UP)
-                serial.printf(" UP\n");
-            if (joystick.direction == DOWN)
-                serial.printf(" DOWN\n");
-            if (joystick.direction == LEFT)
-                serial.printf(" LEFT\n");
-            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");
+DirectionName snakeDirection;
+
+//void drawNut()
+//{
+//    int X = rand()%84;
+//    int Y = rand()%48;
+//    lcd.drawCircle(X,Y,1,1);
+//}
 
+void moveSnake()
+{
+
+    if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) {
+        if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
+            snake.snakeX[0]-=1;
+
+        } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
+            snake.snakeX[0]+=1;
+        } else if(joystick.direction == UP&&snake.direction!=DOWN) {
+            snake.snakeY[0]-=1;
+        } else if(joystick.direction == DOWN&&snake.direction!=UP) {
+            snake.snakeY[0]+=1;
         }
 
     }
 }
 
+void drawSnake()
+{
+
+    snake.snakeX[0] = 10; //coordinate of head
+    snake.snakeY[0] = 10;
+    snake.snakeX[1] = 11;
+    snake.snakeY[1] = 10;
+    snake.snakeX[2] = 12;
+    snake.snakeY[2] = 10;
+    snake.snakeX[3] = 13;
+    snake.snakeY[3] = 10;
+    snake.node = 5; //node of snake
+
+    for(i=0; i<snake.node-1; i++) {
+
+        lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
+    }
+}
+
+/*
+
+*/
+
+void startGmae()
+{
+    nut.yes = 1;
+    snake.life = 0;
+    snake.direction=LEFT;
+
+    //if(joystick.direction == UNKNOWN)
+    //{
+
+    if(nut.yes==1) { //set nut randomly
+        nut.X = rand()%83;
+        nut.Y = rand()%47;
+        nut.yes = 0;
+        lcd.drawCircle(nut.X,nut.Y,1,1);
+    }
+   
+
+    for(i=3; i<snake.node; i++) { //if snake collide itself snake die
+        if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) {
+            snake.life=1;
+            // break;
+        }
+    }
+
+    for(i=snake.node-1; i>0; i--) { //move snake
+        snake.snakeX[i]=snake.snakeX[i-1];
+        snake.snakeY[i]=snake.snakeY[i-1];
+    }
+
+    if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) {
+        snake.life=1; // snake collide the corrider snake die
+        //break;
+    }
+
+
+    if(snake.life==1) {
+        //break;
+    }
+    if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut
+        snake.node++;
+        nut.yes=1;   // new nut appear
+    }
+    //}
+
+}
+
+
+//while(joystick.direction == UNKNOWN) {
+
+
+//switch(snake.direction)
+//{
+// case RIGHT:snake.X[0]+=1;
+//  break;    
+//  case LEFT:snake.X[0]-=1;
+//  break;     
+//  case UP:snake.Y[0]-=1;
+//  break;
+//   case DOWN:snake.Y[0]+=1;
+//  break;
+// }
+
+//end of while(joystick.direction = UNKNOWN)
+
+
+// if(joystick.direction == UP&&snake.direction!=DOWN) {
+//    snake.direction=UP;
+// } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) {
+//     snake.direction= RIGHT;
+// } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) {
+//     snake.direction=LEFT;
+//  } else(joystick.direction == DOWN&&snake.direction!=UP) {
+//     snake.direction=DOWN;
+//  }
+
+
+
 // read default positions of the joystick to calibrate later readings
 void calibrateJoystick()
 {
@@ -91,6 +206,7 @@
     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)
@@ -105,6 +221,7 @@
         joystick.direction = CENTRE;
     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
         joystick.direction = UP;
+        //snake.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) {
@@ -114,7 +231,75 @@
     } else {
         joystick.direction = UNKNOWN;
     }
-
     // set flag for printing
     printFlag = 1;
-}
\ No newline at end of file
+}
+
+void updateSnake()
+{
+    // get direction of joystick and update snake
+    if (joystick.direction == UP) {
+        snake.snakeY[0]--;
+        if(snake.snakeY[0]<1) {
+            snake.snakeY[0]=1;
+        }//will stop at the top edge
+
+    }
+    if (joystick.direction == DOWN) {
+        snake.snakeY[0]++;
+        if(snake.snakeY[0]>43) {
+            snake.snakeY[0]=43;
+        }//will stop at the bottom edge
+    }
+    if (joystick.direction == LEFT) {
+        snake.snakeX[0]--;
+        if(snake.snakeX[0]<1) {
+            snake.snakeX[0]=1;
+        }//will stop at the left edge
+
+    }
+    if (joystick.direction == RIGHT) {
+        snake.snakeX[0]++;
+        if(snake.snakeX[0]>87) {
+            snake.snakeX[0]=87;
+        }//will stop at the right edge
+
+    }
+
+}
+
+
+//main code
+int main()
+{
+    lcd.init();
+    lcd.setBrightness(0.5); // put LED backlight on 50%
+    lcd.printString("press to start",0,0);
+
+    calibrateJoystick();  // get centred values of joystick
+    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
+    lcd.refresh();
+
+    //for(i=20,i<83,i++){
+    // i=i;
+    // j=j;
+    // wait(0.5);
+    // }
+
+//infinite while loop
+    while(1) {
+        lcd.clear();
+        updateSnake();
+        drawSnake();
+        // drawNut();
+        moveSnake();
+        startGmae();
+        lcd.refresh();
+        //wait(0.5);
+        //if(snake.life==1) {
+        // lcd.printString("lost",24,42);
+        // break;
+    }
+
+}
+