Wang Lin 201090174

Dependencies:   mbed Gamepad N5110 FXOS8700Q

Revision:
13:febf9fbb502f
Parent:
12:121ba031343a
--- a/main.cpp	Fri May 03 17:12:37 2019 +0000
+++ b/main.cpp	Mon May 06 06:05:10 2019 +0000
@@ -5,31 +5,51 @@
 void welcome();
 void render();
 void initsnake();
+void speed();
+int  dif = 1;
+
+/*
+ELEC 2645 Embedded System Project
+School of Electronic & Electrical Engineering
+University of Leeds
+
+Name:Wang Lin
+Username:wang lin
+Student ID Number:201090174
+Date:May 2019
+*/
  
 int main()
 {
- 
+   // the main loop of the whole project
+    while(1){
     init();     // initialise and then display welcome screen...
     welcome();  // waiting for the user to start
-  // first draw the initial frame
+    speed(); // the page of choose difficulty
     initsnake();//intilize the variables
- initSnake();
- SpwanFood();
- 
-  while(1) {
+    defineSnake();//define the body of snake 
+    SpwanFood();//generate the firstfood
+    
+   // the loop of game, if snake is dead jump to the gameover page
+  while(live == 1 ) {
    lcd.clear();
     render();
-   drawFood();
-   DrawSnake();
+    DrawSnake();
+    drawFood();
     MoveSnake();
     EatFood();
+    Break();
     lcd.refresh();
-    Speed();
-    
+    wait(waittime/2);
+    }
+     //game over page , if peass back, jump this loop and restart
+     while(pad.check_event(Gamepad::BACK_PRESSED) == false){
+        DeadSnake();
+        lcd.refresh();
+        }
     
-    }
+   }
 }
- 
 void init()
 {
     // need to initialise LCD and Gamepad 
@@ -41,33 +61,77 @@
 void welcome() {
         
     lcd.clear();    
-    lcd.printString(" SNAKE!  ",0,1);  
-    lcd.printString(" Press Start ",0,2);
-    
+    lcd.printString("WELCOME!",19,0);
+    lcd.printString(" SNAKE!  ",20,1);  
+    lcd.printString(" Press A  ",16,2);
+    lcd.printString("to choose ",18,3);
+    lcd.printString("difficulty",17,4);
     lcd.refresh();
      
-    // wait flashing LEDs until start button is pressed 
-    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-        pad.leds_on();
-        wait(0.1);
-        pad.leds_off();
-        wait(waittime);
-        lcd.printString("LETS START!",0,2);
+    // wait flashing LEDs until A button is pressed 
+    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
+       lcd.refresh();
     }
 }
  
 void render() {
    
-    lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); 
+    lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); //daaw the map
 
 }
- 
+//initialise the varaiables of snake
 void initsnake() {
     SNAKE.snakezb[0].x = 30;
     SNAKE.snakezb[0].y = 20;
     SNAKE.n = 3;  //inital length of snake
     Dir = 3;
-  
+    live = 1;
+    Score = 0;
+   
     
 }
+// the difficulty choose module throuth joystick or direction button
+void speed(){
+    while(pad.check_event(Gamepad::START_PRESSED) == false){
+    lcd.clear();
+    lcd.printString("CHOOSE",25,0);
+    lcd.printString("difficulty",15,1);
+    lcd.printString("Press START",13,4);
+    lcd.printString("to Start",18,5);//user interface guide to choose difficulty
+    if(dif == 1){
+        waittime = 1;
+        }
+    else if (dif == 2){
+        waittime = 0.3;
+        }
+    else if (dif == 3){
+        waittime = 0.2;
+        }
+    else if (dif == 4){
+        waittime = 0.1;
+        }
+    else if (dif == 5){
+        waittime = 0.05;
+        }
+    if(dif > 5){
+        dif = 5;
+        }
+    else if(dif < 1){
+        dif = 1;
+        }
+    d = pad.get_direction();
+    //if difficulty is larger or less than the limitation , make back to the limitation 
+    if(pad.check_event(Gamepad::Y_PRESSED) || d == N){
+        dif = dif + 1;
+        }
+     if(pad.check_event(Gamepad::A_PRESSED) || d == S){
+        dif = dif - 1;
+        }
+    char buffer[1];
+    sprintf(buffer,"%d",dif);
+    lcd.printString(buffer,40,2);
+    lcd.refresh();
+        }
+    
+    }
     
\ No newline at end of file