Yufan Zhong / Mbed 2 deprecated GOLD_MINER

Dependencies:   mbed

Revision:
12:07a9f2140d9b
Parent:
11:9da147cd7c18
Child:
13:3299ab0ff2f0
--- a/MinerEngine/MinerEngine.cpp	Mon Apr 27 05:44:02 2020 +0000
+++ b/MinerEngine/MinerEngine.cpp	Mon Apr 27 16:02:47 2020 +0000
@@ -86,19 +86,23 @@
     _monster.init(_monster_speed);
     _claw.init(_winch_width);
     _gold.init(_gold_num);
-    
     _s = 1;
+    //_s = 1;
     _state = 0;
+    _claw_speed = 1;
     _catching = 0;//是否正在抓取,x轴锁定
     _claw_get = 0;//是否已经碰到金子
     _monster_collision=0;//是否撞到怪物
     _now_score = 0;
-    _highest_score = 0;
+    
     for (int i=0;i<9;i++) {
         _gold_reached_num[i] = 0;
     }
 }
 
+void MinerEngine::init_unchanged_parameter() {
+    _highest_score = 0;
+}
 
 ///////////////
 void MinerEngine::state_switch(Gamepad &pad, N5110 &lcd)
@@ -136,8 +140,38 @@
 
 void MinerEngine::instruction(Gamepad &pad, N5110 &lcd) {
         lcd.clear();
-        lcd.printString(" Instructions",4,2);//testing
+        lcd.printString("Joystick is",0,0);
+        lcd.printString("used to",0,1);
+        lcd.printString("control the",0,2);
+        lcd.printString("left & right",0,3);
+        lcd.printString("of the winch",0,4);
+        lcd.refresh();
+        wait(5.0);
+        lcd.clear();
+        lcd.printString("Press button",0,0);
+        lcd.printString(" A to put",0,1);
+        lcd.printString("the claw down",0,2);
+        lcd.printString("to catch &",0,3);
+        lcd.printString("lift the gold",0,4);
         lcd.refresh();
+        wait(5.0);
+        lcd.clear();
+        lcd.printString("The most ",0,0);
+        lcd.printString("vital thing",0,1);
+        lcd.printString("is to try",0,2);
+        lcd.printString("to avoid the",0,3);
+        lcd.printString("moving monster",0,4);
+        lcd.refresh();
+        wait(5.0);
+        lcd.clear();
+        lcd.printString("The 'c' means",0,0);
+        lcd.printString("current score",0,1);
+        lcd.printString("and 'h' means",0,2);
+        lcd.printString("highest score",0,3);
+        lcd.refresh();
+        wait(5.0);
+        _state = 0;
+        
 }
 
 void MinerEngine::menu(Gamepad &pad, N5110 &lcd) {
@@ -148,15 +182,15 @@
       lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
       lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
       lcd.printString(" Press Y or A",2,1); // print the instruction
-      lcd.printString(" Instructions",4,2);
-      lcd.printString(" Start Game",4,3);
-      lcd.printString(" Options",4,4);
+      lcd.printString(" Intro",2,2);
+      lcd.printString(" Start Game",2,3);
+      lcd.printString(" Options",2,4);
       if ( _s == 1 ) {
-          lcd.drawSprite(65,24,8,8,(int *)arrow);
+          lcd.drawSprite(70,24,8,8,(int *)arrow);
           }else if ( _s == 2) {
-            lcd.drawSprite(65,32,8,8,(int *)arrow);
+            lcd.drawSprite(70,32,8,8,(int *)arrow);
                   }else if( _s == 0) {
-                      lcd.drawSprite(65,16,8,8,(int *)arrow);
+                      lcd.drawSprite(70,16,8,8,(int *)arrow);
                       }
       lcd.refresh();
     }
@@ -185,14 +219,14 @@
       lcd.printString(" Normal",4,3);
       lcd.printString(" Hard",4,4);
       if ( _s == 0) {
-          lcd.drawSprite(63,15,8,8,(int *)arrow);
-          _monster_speed = 1; // change the speed_winch
+          lcd.drawSprite(60,15,8,8,(int *)arrow);
+          _monster.set_speed(1);
           }else if ( _s == 1 ) {
-              lcd.drawSprite(63,24,8,8,(int *)arrow);
-              _monster_speed = 2;
+              lcd.drawSprite(60,24,8,8,(int *)arrow);
+             _monster.set_speed(1.5);
               }else if ( _s == 2) {
-                  lcd.drawSprite(63,32,8,8,(int *)arrow);
-                  _monster_speed = 3;
+                  lcd.drawSprite(60,32,8,8,(int *)arrow);
+                  _monster.set_speed(2);
                   }
       lcd.refresh();
     }
@@ -200,6 +234,7 @@
 }
 
 void MinerEngine::game_run(Gamepad &pad, N5110 &lcd) {
+    int fps=12;
     _monster_collision=0;
     while(_monster_collision==0) {
         lcd.clear();
@@ -207,7 +242,7 @@
         update(pad);
         draw(lcd);
         lcd.refresh();
-        
+        wait(1.0f/fps);
     }
 }
 /////////////
@@ -220,9 +255,10 @@
         if (_gold_reached_num[i]==0) {
           Vector2D gold_pos = _gold.get_pos(i);
     
-           if (gold_pos.y <= 14) {
+           if (gold_pos.y <= 17) {
               _claw_get=0;
               _now_score++;
+              _catching = 0;
               _claw.add_now_score();
               _gold_reached_num[i] = 1;
               //pad.tone(1500.0,0.5);
@@ -243,8 +279,10 @@
     char buffer1[14];
     sprintf(buffer1,"%2d",_now_score);
     lcd.printString(buffer1,1,0);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    lcd.printString("c",WIDTH/2-18,0);
+    lcd.printString("h",WIDTH/2+18,0);
     if (_now_score>_highest_score) {
-        _highest_score = _now_score;
+         _highest_score = _now_score;
     }
     char buffer2[14];
     sprintf(buffer2,"%2d",_highest_score);
@@ -257,7 +295,7 @@
     _mag = pad.get_mag();
     if (pad.check_event(Gamepad::A_PRESSED) == true){
         _catching=1;
-        _claw.set_velocity(1.0);
+        _claw.set_velocity(_claw_speed);
     }
 }
 
@@ -287,10 +325,10 @@
     }
     Vector2D winch_pos = _winch.get_pos();
     _claw.update(_d,_mag,winch_pos);
-    wait(0.05);
+   // wait(0.05);
     
     _monster.update();
-    wait(0.05);
+   // wait(0.05);
     _gold.update();
    
     check_monster_collision(pad);
@@ -304,12 +342,23 @@
     // read current claw attributes
     Vector2D claw_pos = _claw.get_pos();
     int monster_x = _monster.get_pos();
-
-    
-      if (monster_x==claw_pos.x && claw_pos.y>=22) {  
+    float monster_speed = _monster.get_speed();
+      if ((monster_x-claw_pos.x)<=0
+      && (monster_x-claw_pos.x)>(-2)
+      && claw_pos.y>=22) {  
         _monster_collision=1;
+        // audio feedback
+        pad.tone(1500.0,0.3);
+        pad.leds_on();
+        wait(0.3);
+        pad.leds_off();
+        wait(0.2);
+        pad.tone(1500.0,1.0);
+        pad.leds_on();
+        wait(1.0);
+        pad.leds_off();
+        wait(0.5);
         _state=4;
-        pad.tone(750.0,0.1);// audio feedback
       }
 }
 
@@ -330,24 +379,19 @@
    }
    if (claw_pos.y>HEIGHT-4||_claw_get==1) {
         //_claw_get=0;
-        _claw.set_velocity(-1.0);
+        _claw.set_velocity(-(_claw_speed));
          pad.tone(1500.0,0.1);
-        pad.leds_on();
-        wait(0.2);
-        pad.leds_off();
+        //pad.leds_on();
+        //wait(0.2);
+        //pad.leds_off();
         }
     
     if (claw_pos.y<=14) {
         _claw.set_velocity(0.0);
-        _catching = 0;
+        _catching=0;
         }
 }
 
-//void MinerEngine::claw_down(Gamepad &pad){
-   
-   
-
-//}
 
 /////////////////////////////////////////////////////
 void MinerEngine::welcome(Gamepad &pad, N5110 &lcd) {
@@ -361,34 +405,39 @@
         lcd.drawLine(0,47,84,47,1); 
         lcd.refresh();
         
-       // while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-       //     play_music(pad);
-        //}
-       // screen_rollup(pad,lcd); 
-       // loading(pad,lcd);     
+        while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+           play_music(pad);
+        }
+        screen_rollup(pad,lcd); 
+        loading(pad,lcd);     
 }
   
 void MinerEngine::game_over(Gamepad &pad, N5110 &lcd) {
     lcd.clear();
-    while ( pad.check_event(Gamepad::START_PRESSED) == false 
+    if ( pad.check_event(Gamepad::START_PRESSED) == false 
     && pad.check_event(Gamepad::JOY_PRESSED) == false 
     && pad.check_event(Gamepad::BACK_PRESSED) == false) 
     {
       lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
       lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
-      lcd.printString(" !! Game Over !! ",2,2);
-      lcd.printString(" Press [back] to menu",4,3);
-      lcd.printString(" press [start] to restart",4,4);
+      lcd.printString("!!Game Over!!",2,1);
+      lcd.printString("[back][start]",2,3);
+      lcd.printString(" menu restart",2,4);
       lcd.refresh();
+      play_music(pad);
       }
       
       if (pad.check_event(Gamepad::START_PRESSED) == true
          ||pad.check_event(Gamepad::JOY_PRESSED) == true) {
+            init(12,6,9,1);
             _state=3;
+        
       }
       
       if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
+            init(12,6,9,1);;
             _state=0;
+            
       }
 }