ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Game_Project_2

Dependencies:   Gamepad N5110 Pokemon mbed

Fork of 2645_Game_Project_2 by ELEC2645 (2016/17)

main.cpp

Committer:
200923317
Date:
2017-05-03
Revision:
7:13333933413d
Parent:
6:6e9a415436e0
Child:
8:e47c48551124

File content as of revision 7:13333933413d:

/** Pokemon Game
@brief Game based on the main series Pokemon games by the pokemon company and Gamefreak.
@brief whole game is a battle simulator, where you can level up your companion by battling other wild pokemon
@brief You can view your pokemons stats and change the screen brightness from the menus.

@brief Revision 1.1

@author Aaron Lad
@date   2nd May 2017

@code
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Pokemon.h"
#include "Images.h"
#include "Bitmap.h"
#include "Script.h"

//rewritten code to implement new/better ideas. part of the code is same as before but lots of changes were needed.

//-------------------------------- objects -------------------------------------
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Pokemon pk;
Images sp;
Script sc;
//--------------------------- Structs/Typedefs ---------------------------------
struct joyInput {
    Direction d; //direction of joystick to navigate menu's
};

typedef enum State {START, PARTNER, MENU, FIGHT, POKEMON, SETTINGS} Gamepage;
//assigning names to different states

void init();
void drawStart();
void select(int x, int y);
void deselect(int _x, int _y);
void balls();
float drawPartner();
float partnerChoice(int choice);
void choice(int p);
float drawMenu();
void drawFight();
void drawPoke();
void menu();
void settings();
void ledCycle();
float YesNo();
void battleStats();
float rndm(int RA);
//-----------------------------Functions----------------------------------------

int main()
{
    init();  //initialising screen and gamepad
    drawStart();  //screen shows startup screen
    Gamepage state = START;  //go to start state
//-----------------------GAME LOOP START-------------------------------
    while(1) {
        if (state == START) {

            state = PARTNER;

        } else if (state == PARTNER) { //choosing your partner

            int partner = drawPartner();
            int correct = partnerChoice(partner);
            if( correct == 2) {
                state = START;
            } else if(correct == 1) {
                choice(partner);
                lcd.refresh();
                state = MENU;
            }
        }

        else if (state == FIGHT) { //fight with wild pokemon

            drawFight();
            state = MENU;

        } else if (state == SETTINGS) {  //settings, screen brightness

            settings();
            state = MENU;

        } else if (state == MENU) { // main menu

            wait(1.0);
            int box = drawMenu();
            if (box == 0) {
                state = FIGHT;
                lcd.clear();
            } else if (box ==1) {
                state = POKEMON;
                lcd.clear();
            } else if (box ==2) {
                state = SETTINGS;
                lcd.clear();
            }
            lcd.refresh();
            wait(2.0);

        } else if (state == POKEMON) { //check partner pokemon stats

            state = MENU;
            drawPoke();
        }
    }
}

/** initialise screen and pad
 *powers up the display and configures the gamepad
 *sets the screen brightness to 100%
*/
void init()
{
    pad.init();
    lcd.init();
    lcd.setBrightness(1.0);
}

void ledCycle()
{
    pad.led(1,1); //cycle through LED's on gamepad
    pad.led(6,1);
    wait(0.5);
    pad.led(2,1);
    pad.led(5,1);
    wait(0.5);
    pad.leds_on();
    wait(0.5);
    pad.led(1,0);
    pad.led(6,0);
    wait(0.5);
    pad.led(2,0);
    pad.led(5,0);
    wait(0.5);
    pad.leds_off();
    wait(0.5);
}

/**
 *
*/
void  drawStart()
{
    sc.Start(lcd);

    while( pad.check_event(Gamepad::START_PRESSED) == false) { // until start is pressed, this loop cycles
        ledCycle();
    }
    wait(0.5);
    pad.init();
    lcd.clear();
}

void select(int x,int y)
{

    lcd.setPixel(x,y);
    lcd.setPixel(x-1,y+1);
    lcd.setPixel(x-1,y-1);
    lcd.refresh();
}

void deselect(int _x,int _y)
{

    lcd.setPixel(_x,_y,false);
    lcd.setPixel(_x-1,_y+1,false);
    lcd.setPixel(_x-1,_y-1,false);
    lcd.refresh();
}

void balls() //show the pokeballs on the screen for player to choose from
{
    sp.ball(lcd, pad);
    lcd.refresh();
    wait(1.0);
}

float drawPartner() //choice of pokeball
{
    lcd.clear();
    int offset = 1;
    while(pad.check_event(Gamepad::A_PRESSED) == false) {
        int _d = pad.get_direction();
        if(_d == E && offset == 1||_d == E && offset == 0) {
            offset = offset + 1;
        } else if(_d == W && offset == 1 || _d == W && offset == 2 ) {
            offset = offset - 1;
        }
        if(offset == 0) {
            deselect(33,25);
            deselect(57,25);
            select(9,25);
            balls();
        } else if(offset == 1) {
            deselect(9,25);
            deselect(57,25);
            select(33,25);
            balls();
        } else if(offset == 2) {
            deselect(9,25);
            deselect(33,25);
            select(57,25);
            balls();
        }
    }
    wait(0.5);
    pad.init();
    return offset;
}

float YesNo()
{
    lcd.printString("Are you",0,2);
    lcd.printString(" sure??",0,3);
    lcd.printString("A-Yes",50,1);
    lcd.printString("B-No",50,5);
    lcd.refresh();
    int end = 0;
    wait(1.0);
    while(end == 0) {
        lcd.refresh();
        if(pad.check_event(Gamepad::A_PRESSED)== true) { //choose yes
            end = 1;
        } else if(pad.check_event(Gamepad::B_PRESSED)== true) {//choose no
            end = 2;
        }
        lcd.refresh();
    }
    return end;
}

float partnerChoice(int choice) //shows pokemon inside ball, gives choice to choose that mon
{
    lcd.clear();
    if(choice == 0) {
        sp.bulbasaur(lcd, pad); //grass starter
    } else if(choice == 1) {
        sp.charmander(lcd, pad);//fire starter
    } else if(choice == 2) {
        sp.squirtle(lcd, pad);//water starter
    }
    lcd.refresh();
    wait(3.5);
    lcd.clear();
    int end = YesNo();
    wait(0.5);
    pad.init();
    return end;

}

void choice(int p) //sets up class for your chosen starter
{
    lcd.clear();
    if (p == 0) {
        pk.setType(Grass);
        lcd.printString("You Chose",16,2);
        lcd.printString("Bulbasaur",15,3);
    } else if (p == 1) {
        pk.setType(Fire);
        lcd.printString("You Chose",16,2);
        lcd.printString("Charmander",15,3);
    } else if (p == 2) {
        pk.setType(Water);
        lcd.printString("You Chose ",16,2);
        lcd.printString("Squirtle",15,3);
    }
}

void menu()
{
    lcd.clear();
    lcd.printString("FIGHT",8,1);
    lcd.printString("POKEMON",8,3);
    lcd.printString("SETTINGS",8,5);
}

float drawMenu()
{
    menu();
    int Xo = 6;
    int Yo = 11;
    select(Xo, Yo);
    lcd.refresh();
    wait(1.0);
    int box = 0;
    while( pad.check_event(Gamepad::A_PRESSED) == false) {
        int d = pad.get_direction();
        if (d == S && box == 0 ||d == S && box == 1) {
            deselect(Xo,Yo);
            Yo = Yo + 16;
            select(Xo,Yo);
            lcd.refresh();
            box = box + 1;
            wait(1.0);
        } else if (d == N && box == 1||d == N && box == 2) {
            deselect(Xo,Yo);
            Yo = Yo - 16;
            select(Xo,Yo);
            lcd.refresh();
            box = box - 1;
            wait(1.0);
        }
    }
    return box;
}

float rndm(int RA)
{
    int R = rand() %2;

    if (R ==1) {
    } else {
        if ( RA <= 6 ) {
            R = R;
        } else if ( RA >= 13 ) {
            R = R + 4;
        } else {
            R = R + 2;
        }
    }
    return R;
}
void battleStats()
{
    std::string pkn = pk.Name();
    std::string pkl = pk.Level();
    char aa[50];
    char bb[50];
    strncpy(aa, pkn.c_str(), sizeof(aa));
    strncpy(bb, pkl.c_str(), sizeof(bb));
    lcd.printString(aa,2,4);
    lcd.printString(bb,2,5);
    lcd.refresh();
    wait(2.0);
    lcd.clear();
    lcd.printString(aa,2,4);
}

void drawFight()
{
    lcd.clear();
    srand(time(NULL));
    std::string PokemonNames[18] = {"Charmander","Cyndaquil","Torchic","Chimchar","Tepig","Fennekin","Bulbasaur","Chikorita","Treeko","Turtwig","Snivy","Chespin","Squirtle","Totodile","Mudkip","Piplup","Oshawott","Froakie"};
    Pokemon enemy[6] = {Pokemon(5,20,Fire), Pokemon(10,30,Fire), Pokemon(5,20,Grass), Pokemon(10,30,Grass), Pokemon(5,20,Water), Pokemon(10,30,Water)};
    int ene = rand() % 18;
    int ene2 = rndm(ene);
    std::string Pokename = PokemonNames[ene];
    Pokemon opponent = enemy[ene2];
    char buffer[64];
    std::string nameStr = Pokename;
    strncpy(buffer, nameStr.c_str(), sizeof(buffer));
    lcd.printString("A wild",25,2);
    lcd.printString(buffer,18,3);
    lcd.printString("appeared",20,4);
    lcd.refresh();
    wait(2.0);
    lcd.clear();
    lcd.printString(buffer,35,0);
    lcd.printString("LVL:??",35,1);
    battleStats();
    lcd.printString(buffer,35,0);
    char HP1[10];
    char HP2[10];
    int HPOT;
    int HPAT;
    int HPO = HPOT = pk.HPO(opponent);
    int HPA = HPAT = pk.HPA();
    int win = 0; //1 = win, 2 = loss, 3 = draw
    // After HP cycles past 0, The HP counter goes to above 429,000,000 ;
    //
    while ( win == 0 ) {
        HPAT = HPAT - pk.OpponentTurn(opponent);
        HPOT = HPOT - pk.YourTurn(opponent);
        wait(1.5);
        if( HPOT >> HPO) {
            win = 1;
            lcd.printString("HP:0",2,5);
            pk.win(lcd);
        } else if( HPAT >> HPA) {
            win = 2;
            lcd.printString("HP:0",35,1);
        } else if( HPOT >> HPO && HPAT >> HPA) {
            win = 3;
            lcd.printString("HP:0",2,5);
            lcd.printString("HP:0",35,1);
        } else {
            lcd.drawRect(2,40,50,8,FILL_WHITE);
            lcd.drawRect(35,8,50,8,FILL_WHITE);
            sprintf(HP1, "HP:%u",HPAT);
            sprintf(HP2, "HP:%u",HPOT);
            lcd.printString(HP1,2,5);
            lcd.printString(HP2,35,1);
            lcd.refresh();
        }
    }
    sc.Battle(lcd, win);
    lcd.refresh();
    wait(2.0);
    pad.init();
}

void drawPoke()
{
    lcd.clear();
    while(pad.check_event(Gamepad::B_PRESSED) == false) {
        std::string pkn = pk.Name();
        std::string pkl = pk.Level();
        std::string pkh = pk.HP();
        std::string pkt = pk.Type();
        char a[50];
        char b[50];
        char c[50];
        char d[50];
        strncpy(a, pkn.c_str(), sizeof(a));
        strncpy(b, pkl.c_str(), sizeof(b));
        strncpy(c, pkt.c_str(), sizeof(c));
        strncpy(d, pkh.c_str(), sizeof(d));
        lcd.printString("Pokemon:",2,0);
        lcd.printString(a,2,1);
        lcd.printString(b,2,2);
        lcd.printString(c,2,3);
        lcd.printString(d,2,4);
        lcd.printString(" B = Back ",24,5);
        lcd.refresh();
    }
}

void settings()
{
    lcd.clear();
    int bright = 1;
    lcd.printString("SET BRIGHTNESS", 2, 20);
    lcd.printString("B = BACK", 4, 20);
    lcd.refresh();
    while(pad.check_event(Gamepad::B_PRESSED) == false) {
        bright = pad.read_pot();
        float brightness = 0.5*bright;
        lcd.setBrightness(brightness);
        lcd.refresh();
    }
}