Yufan Zhong / Mbed 2 deprecated GOLD_MINER

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MinerEngine.cpp Source File

MinerEngine.cpp

00001 #include "MinerEngine.h"
00002 
00003 
00004 MinerEngine::MinerEngine()
00005 {
00006 
00007 }
00008 
00009 MinerEngine::~MinerEngine()
00010 {
00011 
00012 }
00013 
00014 //the bit map of the cover picture
00015 int sprite[48][84] =   {
00016     //                        *10                 *20                 *30                 *40                 *50                 *60                 *70                 *80
00017 /*1 */     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1 },
00018 /*2 */     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1 },
00019 /*3 */     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1 },
00020 /*4 */     {1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1 },
00021 /*5 */     {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00062 /*46 */    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00063 /*47 */    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00064 /*48 */    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
00065 };
00066 
00067 //the bit map of the arrow
00068 int arrow[8][8] = {
00069     {0,0,0,0,0,0,0,0},
00070     {0,0,0,0,0,0,0,0},
00071     {0,0,0,1,0,0,0,0},
00072     {0,0,1,0,0,0,0,0},
00073     {0,1,1,1,1,1,0,0},
00074     {0,0,1,0,0,0,0,0},
00075     {0,0,0,1,0,0,0,0},
00076     {0,0,0,0,0,0,0,0},
00077 };
00078 
00079 //the state 1 of the walking man
00080 int man_state1[10][7] = {
00081     {0,0,1,1,1,0,0},
00082     {0,0,1,0,1,0,0},
00083     {0,0,1,1,1,0,0},
00084     {1,0,0,1,0,0,0},
00085     {1,1,1,1,1,1,1},
00086     {0,0,0,1,0,0,1},
00087     {0,0,1,1,1,0,0},
00088     {0,0,1,0,1,0,0},
00089     {0,0,1,0,1,0,0},
00090     {0,0,0,0,1,0,0},
00091 };
00092 
00093 //the state 2 of the walking man
00094 int man_state2[10][7] = {
00095     {0,0,1,1,1,0,0},
00096     {0,0,1,0,1,0,0},
00097     {0,0,1,1,1,0,0},
00098     {0,0,0,1,0,0,1},
00099     {1,1,1,1,1,1,1},
00100     {1,0,0,1,0,0,0},
00101     {0,0,1,1,1,0,0},
00102     {0,0,1,0,1,0,0},
00103     {0,0,1,0,1,0,0},
00104     {0,0,1,0,0,0,0},
00105 };
00106 
00107      //initialise the MinerEngine
00108 void MinerEngine::init(int winch_width,int winch_height,int gold_num,int monster_speed) 
00109 {
00110     // initialise the game parameters
00111     _winch_width = winch_width;
00112     _winch_height = winch_height;
00113     _gold_num = gold_num;
00114     _monster_speed = monster_speed;
00115     // initialise the functions
00116     _winch.init(_winch_height,_winch_width);
00117     _monster.init(_monster_speed);
00118     _claw.init(_winch_width);
00119     _gold.init(_gold_num);
00120     //initilise the othor parameter
00121     _s = 1; //select
00122     _state = 0; //game state 
00123     _claw_speed = 1; 
00124     _catching = 0;// the flag if the claw is going down
00125     _claw_get = 0;// the flag if the claw get the gold
00126     _monster_collision=0;// the flag if the monster collides with claw
00127     _now_score = 0; //initialise the current score
00128     _h = 37;//the vertical position of the wlaking man
00129     // counting parameter for tunnel function
00130     _i = 0;
00131     _n = 0;
00132     _z = 1;
00133     _j = 0;
00134     for (int i=0;i<9;i++) {
00135         _gold_reached_num[i] = 0; //set all reached gold flag to 0
00136     }
00137 }
00138 
00139       //initialise the highest score
00140 void MinerEngine::init_unchanged_parameter() {
00141     _highest_score = 0;
00142 }
00143 
00144       //switch the state of the game
00145 void MinerEngine::state_switch(Gamepad &pad, N5110 &lcd) 
00146 {
00147     while(1){
00148         switch(_state) {
00149                     case 0:
00150                        menu(pad,lcd); //the state of menu page
00151                         break;
00152                     case 1:
00153                        instruction(pad,lcd); //the state displaying the instruction
00154                        break;
00155                     case 2:
00156                        options(pad,lcd); //the state for the difficulties selection 
00157                         break;
00158                     case 3:
00159                        game_run(pad,lcd); //the state for running the game loop
00160                         break;
00161                     case 4:
00162                         game_over(pad,lcd); //the state for displaying the game over page
00163                         break;
00164         } 
00165     }
00166 }
00167 
00168     //return the selection result 
00169 int MinerEngine::get_select(Gamepad &pad) 
00170 {     //change the value of _s to get the selection
00171      if ( pad.check_event(Gamepad::Y_PRESSED) == true && _s >0) {
00172          _s--;
00173         }
00174      else if ( pad.check_event(Gamepad::A_PRESSED) == true && _s < 2) {
00175          _s++;   
00176      }
00177     return _s;
00178 }
00179 
00180      //display the instruction
00181 void MinerEngine::instruction(Gamepad &pad, N5110 &lcd) {
00182         //button pressed to display next instruction
00183         while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00184         lcd.clear();
00185         lcd.printString("Joystick is",0,0);
00186         lcd.printString("used to",0,1);
00187         lcd.printString("control the",0,2);
00188         lcd.printString("left & right",0,3);
00189         lcd.printString("of the winch",0,4);
00190         lcd.refresh();
00191         wait(1); //wait 1 second to avoid repeat press
00192         }
00193         
00194         while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00195         lcd.clear();
00196         lcd.printString("Press button",0,0);
00197         lcd.printString(" A to put",0,1);
00198         lcd.printString("the claw down",0,2);
00199         lcd.printString("to catch &",0,3);
00200         lcd.printString("lift the gold",0,4);
00201         lcd.refresh();
00202         wait(1);
00203         }
00204         
00205         while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00206         lcd.clear();
00207         lcd.printString("The most ",0,0);
00208         lcd.printString("vital thing",0,1);
00209         lcd.printString("is to try",0,2);
00210         lcd.printString("to avoid the",0,3);
00211         lcd.printString("moving monster",0,4);
00212         lcd.refresh();
00213         wait(1);
00214         }
00215         
00216         while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00217         lcd.clear();
00218         lcd.printString("The 'c' means",0,0);
00219         lcd.printString("current score",0,1);
00220         lcd.printString("and 'h' means",0,2);
00221         lcd.printString("highest score",0,3);
00222         lcd.refresh();
00223         wait(1);
00224         }
00225         
00226         _state = 0; //back to menu state
00227       
00228 }
00229 
00230      //display the menu page
00231 void MinerEngine::menu(Gamepad &pad, N5110 &lcd) {
00232    //button pressed to confirm the choice
00233    while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00234       lcd.clear();
00235       _s = get_select(pad); // get the selection
00236       lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00237       lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
00238       lcd.printString(" Press Y or A",2,1); // print the instruction
00239       lcd.printString(" Intro",2,2);
00240       lcd.printString(" Start Game",2,3);
00241       lcd.printString(" Options",2,4);
00242         if ( _s == 1 ) { //move the arrow up and down
00243           lcd.drawSprite(70,24,8,8,(int *)arrow);
00244            }else if ( _s == 2) {
00245               lcd.drawSprite(70,32,8,8,(int *)arrow);
00246                   }else if( _s == 0) {
00247                       lcd.drawSprite(70,16,8,8,(int *)arrow); }
00248       lcd.refresh();
00249       wait(1); }
00250      if ( _s == 0) { // select the options
00251          _state = 1; } //instruction
00252      else if ( _s == 1){
00253          _state = 3; }//game run
00254      else if (_s == 2){
00255          _state = 2; }//select difficulties           
00256 }
00257 
00258       //select difficulties
00259 void MinerEngine::options(Gamepad &pad, N5110 &lcd) {
00260     //button pressed to confirm the choice
00261     while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
00262       lcd.clear();
00263       int _s = get_select(pad);
00264       lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00265       lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
00266       lcd.printString(" Press Y or A",2,1); //print options
00267       lcd.printString(" Easy",4,2);
00268       lcd.printString(" Normal",4,3);
00269       lcd.printString(" Hard",4,4);
00270       if ( _s == 0) {
00271           lcd.drawSprite(60,15,8,8,(int *)arrow);
00272           _monster.set_speed(1.0); // easy set the speed to 1.0
00273           }else if ( _s == 1 ) {
00274               lcd.drawSprite(60,24,8,8,(int *)arrow);
00275              _monster.set_speed(1.5); // normal set the speed to 1.5
00276               }else if ( _s == 2) {
00277                   lcd.drawSprite(60,32,8,8,(int *)arrow);
00278                   _monster.set_speed(2.0); }// hard set the speed to 2.0
00279       lcd.refresh();
00280       wait(1); }
00281     _state = 0; //back to mune state
00282 }
00283 
00284      //run the game loop
00285 void MinerEngine::game_run(Gamepad &pad, N5110 &lcd) {
00286 
00287     tunnel(pad,lcd); //play the tunnel animation 
00288     int fps=12; //set 12 frame per second
00289     _monster_collision=0; //set collision flag to 0
00290     while(_monster_collision==0) { // run the game while no monster collision
00291         lcd.clear();
00292         read_input(pad); //read the gamepad input
00293         update(pad); //update all components
00294         draw(lcd); //draw all components
00295         lcd.refresh();
00296         wait(1.0f/fps);
00297     }
00298 }
00299 
00300      //check if the gold reached the upper boundary
00301 void MinerEngine::check_gold_collision(Gamepad &pad)
00302 {
00303     for (int i=0;i<9;i++) { //check the gold with serial number from 0 to 8
00304         if (_gold_reached_num[i]==0) {
00305           Vector2D gold_pos = _gold.get_pos(i);
00306     
00307            if (gold_pos.y <= 17) {
00308               _claw_get=0; //set the claw free
00309               _now_score++; //add score
00310               _catching = 0; //set catching flag to 0
00311               _claw.add_now_score();
00312               _gold_reached_num[i] = 1; //set the gold to be reached
00313               pad.leds_on(); //turn on and off the leds
00314               wait(0.5);
00315               pad.leds_off();
00316             }
00317         }
00318     }
00319 }
00320 
00321      //print the score on screen
00322 void MinerEngine::print_scores(N5110 &lcd) {                                
00323 
00324     // get highest scores from winchs
00325     int highest_score = _winch.get_highest_score();
00326     // print to LCD 
00327     char buffer1[14]; //print current score
00328     sprintf(buffer1,"%2d",_now_score);
00329     lcd.printString(buffer1,1,0);  
00330     lcd.printString("c",WIDTH/2-18,0);
00331     lcd.printString("h",WIDTH/2+18,0);
00332     if (_now_score>_highest_score) {
00333          _highest_score = _now_score; //update the highest score if current score is higher
00334     }
00335     char buffer2[14]; //print highest score
00336     sprintf(buffer2,"%2d",_highest_score);
00337     lcd.printString(buffer2,WIDTH-13,0);
00338 }
00339 
00340      //read input from gamepad
00341 void MinerEngine::read_input(Gamepad &pad)
00342 {
00343     _d = pad.get_direction(); //get the direction of the joystick
00344     _mag = pad.get_mag(); //get the magnitude of the joystick
00345     if (pad.check_event(Gamepad::A_PRESSED) == true){ //check if the button which makes the claw down is pressed
00346         _catching=1; //lower the claw
00347         _claw.set_velocity(_claw_speed); //set the speed of claw
00348     }
00349 }
00350      
00351      //draw all components on the LCD
00352 void MinerEngine::draw(N5110 &lcd)
00353 {
00354     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); 
00355     //horizontal line
00356     lcd.drawLine(0,13,WIDTH,13,1);
00357     lcd.drawLine(0,0,WIDTH,0,0);
00358     print_scores(lcd); //print scores
00359     _winch.draw(lcd); //draw the components
00360     _claw.draw(lcd);
00361     _gold.draw(lcd);
00362     _monster.draw(lcd);
00363 }
00364 
00365      //update all position of all components
00366 void MinerEngine::update(Gamepad &pad)
00367 {
00368     if (_catching==0) {
00369         _winch.update(_d,_mag); //move the winch only the claw does not do down
00370     }
00371     Vector2D winch_pos = _winch.get_pos(); //make the claw follow the winch
00372     _claw.update(winch_pos);
00373     _monster.update(); 
00374     _gold.update();
00375      // check all collisions
00376     check_monster_collision(pad);
00377     check_claw_collision(pad);
00378     check_gold_collision(pad);
00379     check_gold_reload(); //check if gold reloading is needed
00380 }
00381 
00382      //check the collision of monster and claw
00383 void MinerEngine::check_monster_collision(Gamepad &pad)
00384 {
00385     // read current claw position
00386     Vector2D claw_pos = _claw.get_pos();
00387     int monster_x = _monster.get_pos();
00388     float monster_speed = _monster.get_speed(); //if the x distance between claw and monster less than speed
00389       if ((monster_x-claw_pos.x)<=0            //then there is a collide
00390       && (monster_x-claw_pos.x)>(-2)
00391       && claw_pos.y>=22) {  
00392         _monster_collision=1; //collision happens
00393         // audio and LEDs feedback
00394         pad.tone(1500.0,0.3);
00395         pad.leds_on();
00396         wait(0.3);
00397         pad.leds_off();
00398         wait(0.2);
00399         pad.tone(1500.0,1.0);
00400         pad.leds_on();
00401         wait(1.0);
00402         pad.leds_off();
00403         wait(0.5);
00404         _state=4; //switch to game over state
00405       }
00406 }
00407 
00408      //check the claw collision 
00409 void MinerEngine::check_claw_collision(Gamepad &pad)
00410 {
00411     Vector2D claw_pos = _claw.get_pos();// read current claw position
00412     for (int i=0;i<9;i++) {
00413     if (_gold_reached_num[i]==0) {
00414     Vector2D gold_pos = _gold.get_pos(i); //get the gold position
00415      if (claw_pos.x-gold_pos.x<5&&
00416         claw_pos.x-gold_pos.x>-5&&
00417         (claw_pos.y+3)-gold_pos.y>-2) {
00418         _claw_get=1; //this gold is caught by claw
00419         _gold.gold_caught(i);
00420        }
00421      }
00422    }
00423    if (claw_pos.y>HEIGHT-4||_claw_get==1) { 
00424         _claw.set_velocity(-(_claw_speed)); //inverse the claw speed if collision
00425          // audio feedback
00426          pad.tone(1000.0,0.1); }
00427     if (claw_pos.y<=14) { 
00428         _claw.set_velocity(0.0); //stop the claw when back to upper boundary
00429         _catching=0; //set catching flag to 0
00430         }
00431 }
00432 
00433      //display the welcome page on LCD
00434 void MinerEngine::welcome(Gamepad &pad, N5110 &lcd) {
00435         
00436         lcd.drawSprite(0,0,48,84,(int *)sprite); //draw the cover picture
00437         lcd.drawRect(4,31,25,9, FILL_WHITE); // draw the boundary
00438         lcd.drawRect(4,39,31,10, FILL_WHITE); 
00439         lcd.printString("GOLD",5,4);
00440         lcd.printString("MINER",5,5);
00441         lcd.drawLine(0,47,84,47,1); 
00442         lcd.refresh();
00443         
00444         while ( pad.check_event(Gamepad::START_PRESSED) == false) { 
00445            play_music(pad); //play the BGM while waiting for start
00446         }
00447         screen_rollup(pad,lcd); //roll up the picture
00448         loading(pad,lcd);  //display the loading page
00449 }
00450   
00451      //display the game over page 
00452 void MinerEngine::game_over(Gamepad &pad, N5110 &lcd) {
00453     lcd.clear();
00454     if ( pad.check_event(Gamepad::START_PRESSED) == false 
00455     && pad.check_event(Gamepad::JOY_PRESSED) == false 
00456     && pad.check_event(Gamepad::BACK_PRESSED) == false) 
00457     {
00458       lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); 
00459       lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
00460       lcd.printString("!!Game Over!!",2,1);  //display the messages
00461       lcd.printString("[back][start]",2,3);
00462       lcd.printString(" menu restart",2,4);
00463       lcd.refresh();
00464       play_music(pad); }//play the BGM
00465       if (pad.check_event(Gamepad::START_PRESSED) == true
00466          ||pad.check_event(Gamepad::JOY_PRESSED) == true) {
00467             init(12,6,9,1); //press start to restart the game 
00468             _state=3; }//restart the game 
00469       if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
00470             init(12,6,9,1); //press back to back to menu page
00471             _state=0; //back to menu state
00472       }
00473 }
00474 
00475      //play the BGM and turn on and off the leds
00476 void MinerEngine::play_music(Gamepad &pad) {
00477         pad.tone(370,0.15); 
00478         wait(0.35);
00479         pad.leds_on();
00480         wait(0.02);
00481         pad.leds_off();
00482         wait(0.02);
00483         pad.tone(554,0.15); 
00484         wait(0.35);
00485         pad.leds_on();
00486         wait(0.02);
00487         pad.leds_off();
00488         wait(0.02);
00489         pad.tone(493,0.15); 
00490         wait(0.35);
00491         pad.leds_on();
00492         wait(0.02);
00493         pad.leds_off();
00494         wait(0.02);
00495         pad.tone(554,0.15); 
00496         wait(0.2);
00497         pad.leds_on();
00498         wait(0.02);
00499         pad.leds_off();
00500         wait(0.02);
00501         pad.tone(659,0.15); 
00502         wait(0.35);
00503         pad.leds_on();
00504         wait(0.02);
00505         pad.leds_off();
00506         wait(0.02);
00507         pad.tone(554,0.15); 
00508         wait(0.35);
00509         pad.leds_on();
00510         wait(0.02);
00511         pad.leds_off();
00512         wait(0.02);
00513         pad.tone(493,0.15); 
00514         wait(0.35);
00515         pad.leds_on();
00516         wait(0.02);
00517         pad.leds_off();
00518         wait(0.02);
00519         pad.tone(554,0.15); 
00520         wait(0.35);
00521         pad.leds_on();
00522         wait(0.02);
00523         pad.leds_off();
00524         wait(0.02);
00525         pad.tone(554,0.15); 
00526         wait(0.2);
00527         pad.leds_on();
00528         wait(0.02);
00529         pad.leds_off();
00530         wait(0.02);   
00531 }        
00532 
00533      //roll up the picture 
00534 void MinerEngine::screen_rollup(Gamepad &pad, N5110 &lcd) {
00535     
00536         lcd.clear();
00537         for (int i=0;i<47;i++) {  //update all the pixel one by one 
00538            for(int m=0;m<47;m++) { 
00539               for (int n=0;n<84;n++) {
00540             sprite[m][n]= sprite[m+1][n];
00541             sprite[47][n]= 0;
00542               }
00543            }
00544             lcd.drawSprite(0,0,48,84,(int *)sprite); //draw the picture after each updating
00545             lcd.refresh();
00546             wait(0.1);
00547         }
00548 }
00549 
00550       //play the loading animation 
00551 void MinerEngine::loading(Gamepad &pad, N5110 &lcd) {
00552              
00553         lcd.printString("Loading....",12,0);
00554         lcd.drawRect(20,12,50,10,FILL_TRANSPARENT);
00555              
00556         for (int i=0;i<50;i++) {
00557             lcd.drawRect(20,12,i,10,FILL_BLACK); // update and gradually fill the progress bar
00558             lcd.refresh();
00559             wait(0.05);
00560         }
00561         lcd.printString("Complete!",20,4); //display complete message
00562         lcd.refresh();
00563         wait(0.8);
00564         lcd.clear();
00565 }
00566 
00567       //check if gold reloading is needed
00568 void MinerEngine::check_gold_reload() {
00569      int gold_left = _gold.get_left_num(); //get the left gold number
00570      if (gold_left<1) {
00571          _gold.init(9); //if the left number is less than 1 then reload the golds
00572          for (int i=0;i<9;i++) {
00573              _gold_reached_num[i] = 0; //initialise the reached flag of golds
00574          }
00575      }
00576 }
00577 
00578      //play the tunnel animation
00579 void MinerEngine::tunnel(Gamepad &pad, N5110 &lcd) {
00580     while(_h>24) {
00581         draw_tunnel(lcd); //draw the tunnel while the walking man dose not reach the destination
00582         _j++; //counting parameter for update the position of the walking man
00583         if (_j==5) {
00584             _j=0; //update the frame for 5 times then lift the man for 1 pixel
00585           if (_h>24) {
00586             _h--;}
00587           else {
00588             _h=24;} }// if the man get the center then stop
00589        draw_man(lcd); //draw the walking man 
00590         _i++; //update the counting parameters
00591         _n++;
00592         if (_i==10) {
00593             _i=0;} // the circle(loop) of 10 frame
00594         if (_n==8) {
00595             _n=0;}  //the circle(loop) of 8 frame
00596         lcd.refresh();
00597         wait(0.1); }
00598         tunnel_print(pad,lcd); //print the message
00599 }
00600 
00601      //draw the tunnel
00602 void MinerEngine::draw_tunnel(N5110 &lcd) {
00603         lcd.clear();
00604         lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); 
00605         lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
00606         lcd.drawLine(41,24,18,47,1); //draw the track
00607         lcd.drawLine(42,24,19,47,1);
00608         lcd.drawLine(43,24,66,47,1);
00609         lcd.drawLine(42,24,65,47,1);
00610         lcd.drawCircle(42,24,2+_i,FILL_TRANSPARENT); //draw the wall of the tunnel(4 circles)
00611         lcd.drawCircle(42,24,12+_i,FILL_TRANSPARENT); 
00612         lcd.drawCircle(42,24,22+_i,FILL_TRANSPARENT);
00613         lcd.drawCircle(42,24,32+_i,FILL_TRANSPARENT);
00614         for(int _k=0;_k<23;_k++) {
00615         lcd.drawLine(42-_k,25+_k,42+_k,25+_k,0); //clear inside the track 
00616         }
00617         lcd.drawLine(38-_n,24+_n,46+_n,24+_n,1); //draw the track sleeper
00618         lcd.drawLine(38-_n,25+_n,46+_n,25+_n,1);
00619         lcd.drawLine(30-_n,32+_n,54+_n,32+_n,1);
00620         lcd.drawLine(30-_n,33+_n,54+_n,33+_n,1);
00621         lcd.drawLine(22-_n,40+_n,62+_n,40+_n,1);
00622         lcd.drawLine(22-_n,41+_n,62+_n,41+_n,1);
00623 }
00624 
00625      //draw the walking man
00626 void MinerEngine::draw_man(N5110 &lcd) {
00627             
00628         if(_z==1) {
00629           lcd.drawSprite(39,_h,10,7,(int *)man_state2); //print the state 2 man
00630           lcd.refresh();
00631           
00632         }         
00633         if(_z==-1) {
00634           lcd.drawSprite(39,_h,10,7,(int *)man_state1); //print the state 1 man
00635           
00636           lcd.refresh();
00637         }
00638        _z=-_z; //switch between the 2 state
00639 }
00640 
00641 void MinerEngine::tunnel_print(Gamepad &pad, N5110 &lcd) {
00642        //audio feedback
00643        pad.tone(500,0.2);
00644        wait(0.5);
00645        pad.tone(500,0.5);
00646        wait(0.5);
00647        lcd.printString("  WE ARE",2,1); //print the messages
00648        lcd.refresh();
00649        wait(1);
00650        lcd.printString("  IN THE",2,2);
00651        lcd.refresh();
00652        wait(1);
00653        lcd.printString("  MINE!!!",2,3);
00654        lcd.refresh();
00655        wait(1);
00656 }