renhao dai / Mbed 2 deprecated Joystick_snake_game

Dependencies:   N5110 mbed

Revision:
4:41acda9d68c7
Parent:
3:04ff8cd27dd1
Child:
5:d8a06e7c54fb
--- a/main.cpp	Thu Apr 30 20:34:29 2015 +0000
+++ b/main.cpp	Fri May 01 10:11:41 2015 +0000
@@ -7,10 +7,14 @@
 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
 
 // connections for joystick
-DigitalIn button(p19);
+DigitalIn button(p17);
 AnalogIn xPot(p15);
 AnalogIn yPot(p16);
-//InterruptIn button(p19);
+PwmOut buzzer(p21);
+PwmOut blacklight(p26);
+PwmOut led1(p24);
+PwmOut led2(p23);
+
 int i;
 int j;
 int X;
@@ -88,32 +92,36 @@
     //int X = rand()%84;
     //int Y = rand()%48;
     if(nutFlag==1){
-    lcd.drawCircle(nut.X,nut.Y,2,1);
+    lcd.drawCircle(nut.X,nut.Y,1,1);
     }
 }
 
 void moveSnake()
 {
   
-    if(joystick.direction == UNKNOWN||joystick.direction == CENTRE){
-        snake.snakeX[0]+=1;
+    if(joystick.direction == CENTRE){
+        //snake.snakeX[0]+=1;
+        joystick.direction = CENTRE;
     }
-    
+    //else if(joystick.button == button){
+      //  snake.snakeX[i]=snake.snakeX[i];
+       // snake.snakeY[i]=snake.snakeY[i];
+       // }
     else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) {
 
-        if(joystick.direction == DOWN&&snake.direction!=LEFT) {
+        if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) {
             snake.direction= RIGHT;
-            snake.snakeX[0]+=1;
+            //snake.snakeX[0]+=1;
    
-        } else if(joystick.direction == UP&&snake.direction!=RIGHT) {
+        } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) {
             snake.direction=LEFT;
-            snake.snakeX[0]-=1;
-        } else if(joystick.direction == LEFT&&snake.direction!=DOWN) {
+            //snake.snakeX[0]-=1;
+        } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) {
             snake.direction=DOWN;
-            snake.snakeY[0]-=1;
-        } else if(joystick.direction == RIGHT&&snake.direction!=UP) {
+            //snake.snakeY[0]-=1;
+        } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) {
             snake.direction=UP;
-            snake.snakeY[0]+=1;
+            //snake.snakeY[0]+=1;
         }
 
     }
@@ -125,23 +133,24 @@
 
     snake.snakeX[0] = 10; //coordinate of head
     snake.snakeY[0] = 10;
-    snake.snakeX[1] = 12;
+    snake.snakeX[1] = 11;
     snake.snakeY[1] = 10;
-    snake.snakeX[2] = 14;
+    snake.snakeX[2] = 12;
     snake.snakeY[2] = 10;
-    snake.snakeX[3] = 16;
+    snake.snakeX[3] = 13;
     snake.snakeY[3] = 10;
-    snake.node = 4; //node of snake
+    snake.node = 5; //node of snake
+    snake.direction = RIGHT;
 
 }
 
 void drawSnake()
 {
 
-    for(i=0; i<snake.node; i++) {
+    for(i=0; i<snake.node-1; i++) {
 
-        lcd.drawRect(snake.snakeX[i],snake.snakeY[i],1,1,1);
-        //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
+        //lcd.drawRect(snake.snakeX[i],snake.snakeY[i],3,1,1);
+        lcd.setPixel(snake.snakeX[i],snake.snakeY[i]);
     }
 }
 
@@ -156,8 +165,8 @@
     //snake.direction=LEFT;
 
     //if(joystick.direction == UNKNOWN)
-    //{
-    if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut
+    
+    if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1)) { // if snake got nut
         snake.node++;
         nutFlag=0;   // new nut appear
     }
@@ -175,11 +184,18 @@
             // 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];
-    }
+     
+        for(i=snake.node-1; i>0; i--) { //move snake
+           if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47){   
+           snake.snakeX[i]=snake.snakeX[i-1];
+           snake.snakeY[i]=snake.snakeY[i-1];
+           }
+           else{
+               snake.snakeX[i]=snake.snakeX[i];
+               snake.snakeY[i]=snake.snakeY[i];
+           }
+          }
+     
 
     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
@@ -191,10 +207,13 @@
         //break;
     }
     
-    //}
+    
 
 }
 
+//void printScore()
+    
+
 
 //while(joystick.direction == UNKNOWN) {
 
@@ -248,13 +267,17 @@
     // 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;
+        //LED OFF
     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
         joystick.direction = UP;
+        //LIGHT LED1
         //snake.direction = UP;
     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
         joystick.direction = DOWN;
+        //LIGHT LED2
     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
         joystick.direction = RIGHT;
+        //LIGHT LED3
     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
         joystick.direction = LEFT;
     } else {
@@ -267,33 +290,39 @@
 void updateSnake()
 {
     // get direction of joystick and update snake
-    if (joystick.direction == UP) {
+    if (snake.direction == LEFT) {
         snake.snakeX[0]--;
-        if(snake.snakeY[0]<1) {
-            snake.snakeY[0]=1;
+        //LED1
+        if(snake.snakeX[0]<1) {
+            snake.snakeX[0]=1;
+            
         }//will stop at the top edge
 
     }
-    if (joystick.direction == DOWN) {
+    if (snake.direction == RIGHT) {
         snake.snakeX[0]++;
-        if(snake.snakeY[0]>43) {
-            snake.snakeY[0]=43;
+        //LED2
+        if(snake.snakeX[0]>83) {
+            snake.snakeX[0]=83;
         }//will stop at the bottom edge
     }
-    if (joystick.direction == LEFT) {
-        snake.snakeY[0]--;
-        if(snake.snakeX[0]<1) {
-            snake.snakeX[0]=1;
+    if (snake.direction == UP) {
+        snake.snakeY[0]++;
+        if(snake.snakeY[0]>47) {
+            snake.snakeY[0]=47;
         }//will stop at the left edge
 
     }
-    if (joystick.direction == RIGHT) {
-        snake.snakeY[0]++;
-        if(snake.snakeX[0]>87) {
-            snake.snakeX[0]=87;
+    if (snake.direction == DOWN) {
+        snake.snakeY[0]--;
+        if(snake.snakeY[0]<1) {
+            snake.snakeY[0]=1;
         }//will stop at the right edge
 
     }
+    if(snake.direction == CENTRE){
+        snake.snakeX[0]+=1;
+        }
 
 }
 
@@ -310,6 +339,7 @@
     lcd.refresh();
     initNut();
     initSnake();
+    //printScore();
 
 //infinite while loop
     while(1) {