Yufan Zhong / Mbed 2 deprecated GOLD_MINER

Dependencies:   mbed

MinerEngine/MinerEngine.cpp

Committer:
ZhongYufan
Date:
2020-04-25
Revision:
3:f894534ca2d1
Parent:
2:fe0dbd155c4b
Child:
4:71d01af424c1

File content as of revision 3:f894534ca2d1:

#include "MinerEngine.h"


MinerEngine::MinerEngine()
{

}

MinerEngine::~MinerEngine()
{

}

int sprite[48][84] =   {
    //                        *10                 *20                 *30                 *40                 *50                 *60                 *70                 *80
/*1 */     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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 },
/*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 },
/*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 },
/*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 },
/*5 */     {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,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1,1,1,1,1,1,1,1,1,1 },
/*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 },
/*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 },
/*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 },
};

int arrow[8][8] = {
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,1,0,0,0,0},
    {0,0,1,0,0,0,0,0},
    {0,1,1,1,1,1,0,0},
    {0,0,1,0,0,0,0,0},
    {0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,0},
};

void MinerEngine::init(int winch_width,int winch_height,int gold_num,int monster_speed)
{
    // initialise the game parameters
    _winch_width = winch_width;
    _winch_height = winch_height;
    _gold_num = gold_num;
    _monster_speed = monster_speed;

    _winch.init(_winch_height,_winch_width);
    _monster.init(_monster_speed);
    _claw.init(_winch_width);
    _gold.init(_gold_num);
    
    _s = 1;
    _state = 0;
    _catching = 0;//是否正在抓取,x轴锁定
    _claw_get = 0;//是否已经碰到金子
    _now_score = 0;
    _highest_score = 0;
}


///////////////
void MinerEngine::state_switch()
{
    while(1){
        switch(_state) {
                    case 0:
                       menu(pad,lcd) ;
                        break;
                    case 1:
                       instruction(pad,lcd);
                       break;
                    case 2:
                       options(pad,lcd);
                        break;
                    case 3:
                       game_run(pad,lcd);
                        break;
                    case 4:
                        game_over(pad,lcd);
                        break;
                } 
    }
}

int MinerEngine::get_select(Gamepad &pad) {
     if ( pad.check_event(Gamepad::Y_PRESSED) == true && _s >0) {
         _s--;
     }else if ( pad.check_event(Gamepad::A_PRESSED) == true && _s < 2) {
         _s++;
     }
    return _s;
}

void MinerEngine::menu(Gamepad &pad, N5110 &lcd) {
  
   while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
      lcd.clear();
      _s = get_select(pad); // get the selection
      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);
      if ( _s == 1 ) {
          lcd.drawSprite(65,24,8,8,(int *)arrow);
          }else if ( _s == 2) {
            lcd.drawSprite(65,32,8,8,(int *)arrow);
                  }else if( _s == 0) {
                      lcd.drawSprite(65,16,8,8,(int *)arrow);
                      }
      lcd.refresh();
         }
   } 
     if ( _s == 0 ){ // select the game
         //select(pad,lcd);
         _state = 1;
         break;}
     else if ( _s == 1){
         _staet = 3;
         break;}
     else if (_s == 2){
         _state = 2;
         break;}
        }              
}

void MinerEngine::options(Gamepad &pad, N5110 &lcd) {
    while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
      lcd.clear();
      int _s = get_select(pad);
      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);
      lcd.printString(" Easy",4,2);
      lcd.printString(" Normal",4,3);
      lcd.printString(" Hard",4,4);
      if ( _s == 0) {
          lcd.drawSprite(63,15,8,8,(int *)arrow);
          _monster_speed = 0.5; // change the speed_winch
          }else if ( _s == 1 ) {
              lcd.drawSprite(63,24,8,8,(int *)arrow);
              _monster_speed = 1.0;
              }else if ( _s == 2) {
                  lcd.drawSprite(63,32,8,8,(int *)arrow);
                  _monster_speed = 1.5;
                  }
      lcd.refresh();
    }
    _state = 0;
}

void MinerEngine::game_run(Gamepad &pad, N5110 &lcd){
    int fps = 8;
    while(1){
        readinput(pad);//get _d &_mag
        update(pad);
        check_goal(pad);
        print_scores(lcd);
        draw(lcd);
        lcd.refresh();
        wait(1.0f/fps);
        }
    
    
}
/////////////
/////////////

void MinerEngine::check_gold_collision(Gamepad &pad)//只检查金子Y<=13
{
    for (int i=0;i<9;i++) {
        
    Vector2D gold_pos = _gold.get_pos(i);
    
    if (gold_pos.y < 14) {
        _now_score++;
        _claw.add_now_score();
        pad.tone(1500.0,0.5);
        pad.leds_on();
        wait(0.5);
        pad.leds_off();
    }
}

void MinerEngine::print_scores(N5110 &lcd)//显示左上角即时分
                                        //右上最高分
{
    // get scores from winchs
    int highest_score = _winch.get_highest_score();
    
    // print to LCD 
    char buffer1[14];
    sprintf(buffer1,"%2d",_now_score);
    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
    char buffer2[14];
    sprintf(buffer2,"%2d",_highest_score);
    lcd.printString(buffer2,WIDTH/2 + 4,1);
}

void MinerEngine::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void MinerEngine::draw(N5110 &lcd)
{
    // draw the elements in the LCD buffer
    // pitch
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    //horizontal line
    lcd.drawLine(0,13,WIDTH,13,1);
    //score
    print_scores(lcd);
    // winchs
    _winch.draw(lcd);
    _claw.draw(lcd);
    _gold.draw(lcd);
    _monster.draw(lcd);

void MinerEngine::update(Gamepad &pad)
{
    check_goal(pad);
    _winch.update(_d,_mag);
    _claw.update(_d,_mag);
    _monster.update();
    
    _gold.update();

    check_monster_collision(pad);
    check_gold_collision(pad);
}

void MinerEngine::check_monster_collision(Gamepad &pad)
{
    // read current claw attributes
    Vector2D claw_pos = _claw.get_pos();
    ///检查怪物和爪子X是否一样
    
    
    
    
    Vector2D claw_velocity = _claw.get_velocity();//////////////

    // check if hit top wall
    if (claw_pos.y <= 1) {  //  1 due to 1 pixel boundary/////改/检查怪物和爪子X是否一样
        claw_pos.y = 1;  // bounce off ceiling without going off screen
        claw_velocity.y = -claw_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }
    // check if hit bottom wall
    else if (claw_pos.y + _claw_size >= (HEIGHT-1) ) { // bottom pixel is 47
        // hit bottom
        claw_pos.y = (HEIGHT-1) - _claw_size;  // stops claw going off screen
        claw_velocity.y = -claw_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }

    // update claw parameters
    _claw.set_velocity(claw_velocity);
    _claw.set_pos(claw_pos);
    
}

void MinerEngine::check_claw_collision(Gamepad &pad)
{
    // read current claw attributes
    Vector2D claw_pos = _claw.get_pos();
    Vector2D claw_velocity = _claw.get_velocity();

    // check winch first
    Vector2D winch_pos = _winch.get_pos();

    // see if claw has hit the winch by checking for overlaps
    if (
        (claw_pos.y >= winch_pos.y) && //top
        (claw_pos.y <= winch_pos.y + _winch_height) && //bottom
        (claw_pos.x >= _winch_x) && //left
        (claw_pos.x <= _winch_x + _winch_width)  //right
    ) {
        // if it has, fix position and reflect x velocity
        claw_pos.x = _winch_x + _winch_width;
        claw_velocity.x = -claw_velocity.x;
        // audio feedback
        pad.tone(1000.0,0.1);
    }
}

void MinerEngine::claw_down(){
    _catching = 1;
    claw.set_velocity(1);
    Vector2D claw_pos = claw.get_pos();
    
    if (claw_pos.y>HEIGHT-4||_claw_get==1) {
        _claw_get==0;
        claw.set_velocity(-1);
        }
    
    if (claw_pos.y<=14) {
        claw.set_velocity(0);
        _catching = 0;
        }
}


/////////////////////////////////////////////////////
void MinerEngine::welcome(Gamepad &pad, N5110 &lcd) {
    
   
        lcd.drawSprite(0,0,48,84,(int *)sprite);
        lcd.drawRect(4,31,25,9, FILL_WHITE); 
        lcd.drawRect(4,39,31,10, FILL_WHITE); 
        lcd.printString("GOLD",5,4);
        lcd.printString("MINER",5,5);
        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);     
}
  
  
void MinerEngine::play_music(Gamepad &pad) {
        pad.tone(370,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(554,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(493,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(554,0.15); 
        wait(0.2);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(659,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(554,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(493,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(554,0.15); 
        wait(0.35);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);
        pad.tone(554,0.15); 
        wait(0.2);
        pad.leds_on();
        wait(0.02);
        pad.leds_off();
        wait(0.02);   
}        

void MinerEngine::screen_rollup(Gamepad &pad, N5110 &lcd) {
    
        lcd.clear();
        for (int i=0;i<47;i++) {
           for(int m=0;m<47;m++) { 
              for (int n=0;n<84;n++) {
            sprite[m][n]= sprite[m+1][n];
            sprite[47][n]= 0;
              }
           }
            lcd.drawSprite(0,0,48,84,(int *)sprite);
            lcd.refresh();
            wait(0.1);
        }
}

void MinerEngine::loading(Gamepad &pad, N5110 &lcd) {
             
        lcd.printString("Loading....",12,0);
        lcd.drawRect(20,12,50,10,FILL_TRANSPARENT);
             
        for (int i=0;i<50;i++) {
            lcd.drawRect(20,12,i,10,FILL_BLACK); 
            lcd.refresh();
            wait(0.05);
        }
        lcd.printString("Complete!",20,4);
        lcd.refresh();
        wait(0.8);
        lcd.clear();
}
// check claw next
    /*
    Vector2D claw_pos = _claw.get_pos();

    // see if claw has hit the winch by checking for overlaps
    if (
        (claw_pos.y >= claw_pos.y) && //top
        (claw_pos.y <= claw_pos.y + _winch_height) && //bottom
        (claw_pos.x + _claw_size >= _claw_x) && //left
        (claw_pos.x + _claw_size <= _claw_x + _winch_width)  //right
    ) {
        // if it has, fix position and reflect x velocity
        claw_pos.x = _claw_x - _claw_size;
        claw_velocity.x = -claw_velocity.x;
        // audio feedback
        pad.tone(1000.0,0.1);
    }

    // write new attributes
    _claw.set_velocity(claw_velocity);
    _claw.set_pos(claw_pos);
    */