Wang Lin 201090174

Dependencies:   mbed Gamepad N5110 FXOS8700Q

Revision:
13:febf9fbb502f
Parent:
12:121ba031343a
--- a/main.h	Fri May 03 17:12:37 2019 +0000
+++ b/main.h	Mon May 06 06:05:10 2019 +0000
@@ -1,12 +1,12 @@
 #include <stdio.h>
-#include <stdlib.h>
 #include <time.h>
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
 #define length 100
 #define SNAKE_SIZE 2 
- 
+
+/////////////// objects /////////////// 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
  
@@ -14,53 +14,58 @@
 struct Coor {
     int x;
     int y;    
-}COOR;
+}COOR;//define the variable of snake coordinate
  
 struct Snake {
-    Coor snakezb[length]; //蛇每一届的坐标 蛇最大的长度
-    int n;   //number of snake points 当前节数        
+    Coor snakezb[length]; //the max length of snake, every section length of snake
+    int n;   //number of snake points   
 }SNAKE;
 struct Food{
-   Coor fc;
+   Coor fc;//define the variable of food coordinate
 }food;
  
+/** main Class
+@author WangLin, University of Leeds
+@brief all the functions of Snake game
+@date May 2019
+*/
+
+/**define the body coordinate of snake*/ 
+void defineSnake();
+/**the music module*/
+void sound();
+/**draw snake
+@param the coor(N5110&lcd)
+ */
+void DrawSnake();
+/**make snake to move to different direction
+@param direction(int Dir)
+ */
+void MoveSnake();
+/** generate a new food when the previous food has been eated
+*/
+void RespawnFood();
+/**when snake dead print gameover and guide to restart
+*/
+void DeadSnake();
+float waittime;
+int Score ;
+int live; // Snake's live, if live=0, break the loop.
+int Dir;
+Direction d;
 
  
  
- 
- 
- 
- 
- 
- 
-
-void DrawSnake();
-//void DirSnake();
-void MoveSnake();
-int Dir;
-void RespawnFood();
-//void EatFood();
-//void BreakSnake();
-//void OverSnake();
-//void Speed();
-int waittime;
-Direction d;
- 
- 
-
- 
-
- 
- 
- 
- void initSnake(){
+ // define the coordinate of the body of snake
+ void defineSnake(){
      for(int i = 1; i < SNAKE.n; i++){
          SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
-         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y-1;
+         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y-1;//draw snake from top to bottom
          }
      }
 
  
+//draw a snake by through the whole array
 void DrawSnake()
 {
         
@@ -71,9 +76,12 @@
     
 }
  
+//implement the movement function
 void MoveSnake()
 {
-    d = pad.get_direction();
+    d = pad.get_direction();//get the direction from joystick
+    //get the direction from the button 
+    //prepare for move
     if (pad.check_event(Gamepad::Y_PRESSED) || d == N){
         Dir = 1;//up
         }
@@ -86,68 +94,113 @@
     else if (pad.check_event(Gamepad::B_PRESSED) || d == E){
         Dir = 4;//right
         }
-        
+// the main  statement to run the snake
+//to let the coordinate of the snake equals to the previous section
+//so the move statement only need to change the first section snake's coordinate
     for(int i = SNAKE.n; i >0; i--){
         SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y;
         }
-    if (Dir == 1){
+    //let snake move for different direction
+    if (Dir == 1 && Dir != 2){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y-1;
-        }
-    else if (Dir == 2){
+        }//move up
+    else if (Dir == 2 && Dir != 1){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y+1;
-        }
-    else if (Dir == 3){
+        }//move down
+    else if (Dir == 3 && Dir != 4){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x-1;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
-        }
-    else if (Dir == 4){
+        }//move left
+    else if (Dir == 4 && Dir != 3){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x+1;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
-        }
+        }//move right
     
  }
 
+//generate a radom food
 void SpwanFood(){
     srand(unsigned (time(NULL)));
-    food.fc.x = rand() % 80;
+    food.fc.x = rand() % 80; // get a radom number for the coordinate of food.
     food.fc.y = rand() % 40;
     
-    
+    }
+//print the food
+void drawFood(){
+    lcd.drawRect(food.fc.x,food.fc.y,1,1,FILL_BLACK);
+    }
+//juge if snake eat food, length increased, score +1 , play sound effect.
+void EatFood(){
+//juge if snake eat food
+    if(SNAKE.snakezb[0].x == food.fc.x && SNAKE.snakezb[0].y == food.fc.y ){
+        SNAKE.n = SNAKE.n+5;
+       SpwanFood();//generate a new food after the previous food had been eated
+       pad.tone(650,0.5);// sound effect
+       Score = Score +1;// score+1
+       char buffer[1];
+       sprintf(buffer,"%d",Score);
+       lcd.printString(buffer,70,1);//print score
+        }
+    }
+//juge if snake hit the map border or itself, if hitted then break loop, jump to the DeadSnake mode.
+void Break(){
+    //check if snake hit the map border
+    if(SNAKE.snakezb[0].x<=0 || SNAKE.snakezb[0].x >= 83 || SNAKE.snakezb[0].y <= 0  || SNAKE.snakezb[0].y >= 47)
+    {
+        live = 0;
+        
+        }
+       //check if the snake hit itself 
+        for(int i = SNAKE.n-2; i > 0; i--){
+           if( SNAKE.snakezb[0].x == SNAKE.snakezb[i].x && SNAKE.snakezb[0].y == SNAKE.snakezb[i].y){
+            live =  0;
+            
+            }
+            }
+    }
+
+//if snake dead,print gameover page and a passway to resart
+void DeadSnake(){
+    lcd.clear();
+    lcd.printString("GAME  OVER",0,1 );
+    lcd.printString("YOUR SCORE:",0,2);
+    lcd.printString("Press Back",1,4);
+    lcd.printString("TO Resart",1,5);
+    char buffer[1];
+    sprintf(buffer,"%d",Score);
+    lcd.printString(buffer,65,2);
+    lcd.refresh();
+    sound();
     
     
     }
-void drawFood(){
-    lcd.drawRect(food.fc.x,food.fc.y,1,1,FILL_BLACK);
-    }
-void EatFood(){
-
-    if(SNAKE.snakezb[0].x == food.fc.x && SNAKE.snakezb[0].y == food.fc.y ){
-        SNAKE.n = SNAKE.n+1;
-       SpwanFood();
-        }
-    }
-void DeadSnake(){
-    
-    }
-
- 
-void Speed()
-{
-    if(pad.check_event(Gamepad::L_PRESSED))
-    {
-      waittime = 0.05;
-      wait(waittime);  
-      }
-    else if(pad.check_event(Gamepad::R_PRESSED))
-    {
-      waittime = 0.5;
-      wait(waittime);  
-      }
-    else
-    {
-      wait(0.3);
-      }    
-}
\ No newline at end of file
+// the sound moudle
+void sound(){
+    pad.tone(440.0,0.25);
+       wait(0.27);
+       pad.tone(659,0.25);
+        wait(0.27);
+       pad.tone(659,0.25);
+       wait(0.27);
+       pad.tone(440.0,0.25);// 及你太美
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.2);
+       pad.tone(440.0,0.25);// baby
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.27);
+       pad.tone(659,0.25);
+        wait(0.27);
+       pad.tone(659,0.25);
+       wait(0.27);
+       pad.tone(440.0,0.25);
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.2);
+       pad.tone(440.0,0.25);
+       wait(0.8);
+    }
\ No newline at end of file