ELEC2645 (2018/19) / Mbed 2 deprecated el17ttds

Dependencies:   mbed N5110_tf

main.cpp

Committer:
el17ttds
Date:
2019-05-09
Revision:
7:08f78909dda7
Parent:
4:3446009e2f38
Child:
8:d1c04f0e4890

File content as of revision 7:08f78909dda7:

#include "main.h"

/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Engine engine;

int main() {

    init();
    welcome();
    string option = menu();
    begin(option);

    lcd.printString("     END",0,5);
    lcd.refresh();
}

void init() {
  alive = true;
  // initialise display and peripherals
  lcd.init();
  pad.init();

    // initialise any in game functions (e.g: sprites)
    engine.init(MAP_WIDTH,MAP_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
}

void welcome() {
    // Draw coin
    // wait
    // Draw protagonist
    // wait
    // draw enemies
    // wait
    lcd.printString("     GAME!    ",0,0);
    lcd.refresh();
    wait(1.0);
    lcd.printString("By Thomas",0,2);
    lcd.printString("Foster",0,3);
    lcd.refresh();
    wait(2.0);

    while (pad.check_event(Gamepad::START_PRESSED) == false) {
        lcd.printString("Press Start ",0,5);
        lcd.refresh();
        wait(0.2);
        lcd.printString("Press Start. ",0,5);
        lcd.refresh();
        wait(0.2);
        lcd.printString("Press Start.. ",0,5);
        lcd.refresh();
        wait(0.2);
        lcd.printString("Press Start... ",0,5);
        wait(0.2);
    }
}

string menu() {

    string option = "0";
    lcd.init();

    while(option == "0") {

        lcd.printString("A - Play Now",0,1);
        lcd.printString("B - Tutorial",0,2);
        lcd.printString("X - Highscores",0,3);
        lcd.printString("Y - Credits",0,4);
        lcd.refresh();

        if (pad.check_event(Gamepad::A_PRESSED) == true) {
            option = "A";
        } else if (pad.check_event(Gamepad::B_PRESSED) == true) {
            option = "B";
        } else if (pad.check_event(Gamepad::X_PRESSED) == true) {
            option = "X";
        } else if (pad.check_event(Gamepad::Y_PRESSED) == true) {
            option = "Y";
        }

        wait(0.2);
    }
    if (option == "A") {
        init();
        lcd.printString("  Play game?  ",0,0);
        ask(option);
    } else if (option == "B") {
        init();
        lcd.printString("Play tutorial?",0,0);
        ask(option);
    }
    return option;
}

void ask(string option) {

    int start = -1;
    lcd.printString("Are you sure?",0,3);
    lcd.printString("Back?   Start?",0,5);
    lcd.refresh();

    while (start == -1) {

        if (pad.check_event(Gamepad::START_PRESSED) == true) {
            start = 1;
        } else if(pad.check_event(Gamepad::BACK_PRESSED) == true) {
            start = 0;
        }
        wait(0.2);
    }

    if (start == 0) {
        menu();
    }
}

void begin(string option) {

    init();
    if (option == "A") {
        play();
    } else if (option == "B") {
        tutorial();
    } else if (option == "X") {
        highscores();
    } else {
        credits();
    }
}

void play() { // My game loop
  lcd.refresh();
  wait(0.2);
  gameLoop();
}

void gameLoop() {
  while ( (alive == true) || (pad.check_event(Gamepad::START_PRESSED) == false) ) {
    lcd.refresh();
    engine.read(pad);
    alive = engine.write(MAX_SPEED, pad);
    engine.render(lcd);
    wait(0.1);
  }
  if (alive) {
    while (pad.check_event(Gamepad::START_PRESSED) == false) {
      lcd.clear();
      lcd.printString("    PAUSED    ", 0, 0);
      lcd.refresh();
      wait(0.5);
      lcd.printString("Press Start", 0, 3);
      lcd.refresh();
      wait(0.2);
      lcd.printString("Press Start.", 0, 3);
      lcd.refresh();
      wait(0.2);
      lcd.printString("Press Start .", 0, 3);
      lcd.refresh();
      wait(0.2);
      lcd.printString("Press Start  .", 0, 3);
      lcd.refresh();
      wait(0.2);
    }
  } else {
    lcd.clear();
    lcd.printString("YOU HELLA DEAD", 0, 0);
  }
}

void tutorial() {

    lcd.printString(" Tutorial!! ",0,0);
    lcd.refresh();
}

void highscores() {

    lcd.printString(" Highscores!! ",0,0);
    lcd.refresh();
}

void credits() {

    lcd.printString("   Credits!!   ",0,0);
    lcd.refresh();
}