Li Ruofan 201199450

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
DannyLee
Date:
Sun May 24 08:16:35 2020 +0000
Parent:
0:80a59a49d504
Commit message:
Li Ruofan 201199450

Changed in this revision

Snake/Snake.cpp Show annotated file Show diff for this revision Revisions of this file
Snake/Snake.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Snake/Snake.cpp	Sat May 23 19:01:15 2020 +0000
+++ b/Snake/Snake.cpp	Sun May 24 08:16:35 2020 +0000
@@ -26,38 +26,37 @@
 }
 
 pos Snake::getPos(){
-        return apple;   //set apple's pos
+        return apple;
 } //end producing
 
-//***Functions***//
-void Snake::death(N5110 &lcd){
+void Snake::gameover(N5110 &lcd){
         
-        Life--; 
+        Life--;
         for (;Life==0;){ 
         
             lcd.clear();
-            lcd.printString("     Dead     ",0,1);
-            lcd.printString("   Score:  ",0,2); 
+            lcd.printString("  You dead  ",0,1);
+            lcd.printString("   Score:  ",0,3);  
             
             char buffer[2];
             sprintf(buffer,"%2d",L-6);
-            lcd.printString(buffer,60,2);
+            lcd.printString(buffer,60,3);
             
-            lcd.printString(" Reset to New",0,4);
-            
+            lcd.printString(" Reset the pad",0,4);
+            lcd.printString("  to restart  ",0,5);
             lcd.refresh(); 
-        }  
-        
-        init(beginX,beginY,beginL,Life);
-        
-}
+        }
+            init(beginX,beginY,beginL,Life);
+            
+    }  //Reset the pad to restart the game.
 
+ 
 void Snake::WallMeet(N5110 &lcd){
     
     if (greedy.bodyX[L]==84 || greedy.bodyX[L]==0          //snake is also called greedy in this game
             || greedy.bodyY[L]==0 || greedy.bodyY[L]==48 ) //if greedy touch the wall
         {     
-            death(lcd);
+            gameover(lcd);
         }
 
 }//end WallMeet
@@ -67,7 +66,7 @@
         while (i < L - 1){
                 if (greedy.bodyY[L-1]==greedy.bodyY[i] && greedy.bodyX[L-1]==greedy.bodyX[i]  )
                         {
-                            death(lcd);
+                            gameover(lcd);
                         }
                          i++;
                 }
@@ -101,13 +100,13 @@
 }//end of init
 
 void Snake::draw(N5110 &lcd){
-        WallMeet(lcd);     // if head==wall game over
-        checkBodyMeet(lcd);     //checking body whether meet wall
+        WallMeet(lcd);   
+        checkBodyMeet(lcd);     //checke if greedy touch the wall
             if (Life != 0 ){
                 lcd.clear();
                 wait(0.15-L*0.001);                
-                drawApple(lcd);//make first apple
-                lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);//draw battlefield
+                drawApple(lcd);//produce apple
+                lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);//create battlefield
                 int i = 0;
                 while (i < L){
                               lcd.setPixel(greedy.bodyX[i],greedy.bodyY[i]);
@@ -173,6 +172,7 @@
             
             printf("updated ");
             
+
             if(eatApple()){           
                 greedy.bodyX[L] = greedy.bodyX[L+1];
                 greedy.bodyY[L] = greedy.bodyY[L+1];
--- a/Snake/Snake.h	Sat May 23 19:01:15 2020 +0000
+++ b/Snake/Snake.h	Sun May 24 08:16:35 2020 +0000
@@ -46,7 +46,7 @@
         
         void update(Gamepad &pad);
         
-        void death(N5110 &lcd);
+        void gameover(N5110 &lcd);
         
         void produceBody();
         
--- a/main.cpp	Sat May 23 19:01:15 2020 +0000
+++ b/main.cpp	Sun May 24 08:16:35 2020 +0000
@@ -21,16 +21,14 @@
 Gamepad pad;
 Snake snake;
 
-int bgm_();
 int sound_();
 void begin();
 void init(Gamepad &pad);
-void game_tips();
+void game_rules();
 
 int main()
 {
     init(pad);
-    // game loop
     while (1) {
             snake.draw(lcd);
             snake.update(pad);
@@ -45,8 +43,8 @@
     begin();
     snake.init(25,25,6,1);
     // initialise parameters of the project
-    // display tips
-    game_tips();
+    // display rules
+    game_rules();
 }
 
 void begin(){
@@ -69,7 +67,7 @@
     
 }
 
-void game_tips(){
+void game_rules(){
     lcd.clear(); 
     lcd.printString(" Greedy Snake! ",0,0);
     lcd.printString(" Move the snake ",0,1);  
@@ -81,9 +79,9 @@
 while (pad.check_event(Gamepad::START_PRESSED) == false) {
         pad.leds_on();
         wait(0.2);
-        pad.leds_off();
-        wait(0.2);
-}
+    }
+    
+    lcd.clear();
 }
 
 int sound_(){
@@ -167,7 +165,6 @@
     if(pad.check_event(Gamepad::X_PRESSED) == true){
         return 0;
         }
-    
     pad.tone(440.0,0.3);
     wait(0.3);
     if(pad.check_event(Gamepad::X_PRESSED) == true){
@@ -178,7 +175,110 @@
     if(pad.check_event(Gamepad::X_PRESSED) == true){
         return 0;
         } 
+    pad.tone(659.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
         return 0;
-    }//play the Chinese song "East is Red" 
-
-    
\ No newline at end of file
+        }
+    pad.tone(659.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(587.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(523.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(523.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(659.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(784.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(1046.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(1046.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(1046.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(880.0f,0.5f);
+    wait(0.5f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(784.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(784.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(880.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(784.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(659.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(523.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(587.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(587.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(659.0f,0.3f);
+    wait(0.3f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }
+    pad.tone(587.0f,0.2f);
+    wait(0.2f);
+    if(pad.check_event(Gamepad::X_PRESSED) == true){
+        return 0;
+        }   
+        return 0;
+    }//play Chinese song "East is Red" and Australian song "Sheep clippling"