submit

Dependencies:   mbed Gamepad N5110

Engine/Engine.cpp

Committer:
694617778
Date:
2019-04-21
Revision:
12:684f3ff512d8
Parent:
11:543c62bed764
Child:
13:32772a08841e

File content as of revision 12:684f3ff512d8:

#include "Engine.h"
#include "snake.h"
int sprite[8][8] = {
    {0,0,0,1,1,0,0,0},
    {0,0,1,1,0,0,0,0},
    {0,1,1,0,0,0,0,0},
    {1,1,1,1,1,1,1,0},
    {1,1,1,1,1,1,1,0},
    {0,1,1,0,0,0,0,0},
    {0,0,1,1,0,0,0,0},
    {0,0,0,1,1,0,0,0},
};

Engine::Engine()
{

}

Engine::~Engine()
{

}

void Engine::init() {
    direction = 0;
    p = 1.0;
    s = 1;
}

int Engine::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 Engine::menu(Gamepad &pad, N5110 &lcd) {
   while(1){
   while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
      lcd.clear();
      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(" Play",4,3);
      lcd.printString(" Setting",4,4);
      if ( s == 1 || s == 0) {
          //lcd.printString("#",55,3);
          lcd.drawSprite(60,23,8,8,(int *)sprite);
          }else if ( s == 2) {
            //lcd.printString("#",55,4);
            lcd.drawSprite(60,32,8,8,(int *)sprite);
                  }
      lcd.refresh();
    }
     if ( s == 1 || s == 0){
         select(pad,lcd);
         break;
         }else if ( s == 2){
             setting(pad,lcd);
            }
    }
}

void Engine::setting(Gamepad &pad, N5110 &lcd) {
    while ( pad.check_event(Gamepad::BACK_PRESSED) == false ) {
    lcd.clear();
      int s = get_select(pad);
      float pot = pad.read_pot();
      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(" Light",4,3);
      lcd.printString(" Contrast",4,4);
      if ( s == 1 || s == 0){
         //lcd.printString("#",65,3);
         lcd.drawSprite(60,23,8,8,(int *)sprite);
         lcd.drawRect(12,10,60,10,FILL_TRANSPARENT);
         lcd.drawRect(12,10,60*pot,10,FILL_BLACK);
         lcd.setBrightness(pot);
         }else if ( s == 2){
            //lcd.printString("#",65,4);
            lcd.drawSprite(60,32,8,8,(int *)sprite);
            lcd.drawRect(12,10,60,10,FILL_TRANSPARENT);
            lcd.drawRect(12,10,60*pot,10,FILL_BLACK);
            lcd.setContrast(pot);
            }
       lcd.refresh();
    }
}

void Engine::select(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.printString(" #",50,2);
          lcd.drawSprite(63,15,8,8,(int *)sprite);
          p = 1.5;
          }else if ( s == 1 ) {
              //lcd.printString(" #",50,3);
              lcd.drawSprite(63,24,8,8,(int *)sprite);
              p = 1.0;
              }else if ( s == 2) {
                  lcd.drawSprite(63,32,8,8,(int *)sprite);
                  p = 0.5;
                  }
      lcd.refresh();
     }
}


int Engine::get_direction(Gamepad &pad) {
    if (pad.check_event(Gamepad::A_PRESSED) == true && direction != 3){
         direction = 1;
        } else if (pad.check_event(Gamepad::B_PRESSED) == true && direction != 2){
             direction = 0;
            } else if (pad.check_event(Gamepad::X_PRESSED) == true && direction != 0){
                direction = 2;
                } else if (pad.check_event(Gamepad::Y_PRESSED) == true && direction != 1){
                    direction = 3;
                    }
    if (pad.get_direction() == S && direction != 3){
         direction = 1;
        } else if (pad.get_direction() == E && direction != 2){
             direction = 0;
            } else if (pad.get_direction() == W && direction != 0){
                direction = 2;
                } else if (pad.get_direction() == N && direction != 1){
                    direction = 3;
                    }
    return direction;
    
}

// simple splash screen displayed on start-up
void Engine::welcome(Gamepad &pad, N5110 &lcd) {
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
    lcd.printString(" ###SNAKE### ",4,1);
    lcd.printString(" Qi Minghong ",4,2);
    lcd.printString(" 201089396 ",10,3);
    lcd.printString(" Press Start ",4,4);
    lcd.refresh();
     
    // wait flashing LEDs until start button is pressed 
    while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}
// simple splash screen displayed on pasue
void Engine::pause(Gamepad &pad, N5110 &lcd) {
    pad.tone(500.0,0.5);
    lcd.drawRect(22,13,40,14,FILL_TRANSPARENT);
    lcd.printString("PAUSE",28,2);
    lcd.refresh();
     
    // wait flashing LEDs until start button is pressed 
    while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
}

// simple splash screen of game over
void Engine::gameover(Gamepad &pad, N5110 &lcd, int score,int hscore) {
    pad.tone(500.0,0.5);
    lcd.clear();
    pad.init();
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
    lcd.printString("  GAME OVER ",4,1); 
     // we need an array of chars to store the message
    char buffer[14];  // max screen witdth is 14
    // print message to buffer
    sprintf(buffer,"  Score : %d ",score);
    lcd.printString(buffer,2,2);
    char hbuffer[14];  // max screen witdth is 14
    // print message to buffer
    sprintf(hbuffer,"Maximun : %d",hscore);
    lcd.printString(hbuffer,8,3);
    lcd.printString(" press back",8,4);
    lcd.refresh();
     
    // wait flashing LEDs until start button is pressed 
    while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
        pad.leds_on();
        wait(0.1);
        pad.leds_off();
        wait(0.1);
    }
 
}