Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Gamepad N5110 Pokemon mbed
Fork of 2645_Game_Project_2 by
Diff: main.cpp
- Revision:
- 6:6e9a415436e0
- Parent:
- 5:add0351183be
- Child:
- 7:13333933413d
--- a/main.cpp	Mon Apr 24 23:53:53 2017 +0000
+++ b/main.cpp	Tue May 02 14:47:30 2017 +0000
@@ -22,8 +22,8 @@
 
 void init();
 void drawStart();
-void select(int x, int y, int L);
-void deselect(int _x, int _y, int _L);
+void select(int x, int y);
+void deselect(int _x, int _y);
 void balls();
 float drawPartner();
 float partnerChoice(int choice);
@@ -49,18 +49,21 @@
         } else if (state == PARTNER) { //choosing your partner
             int partner = drawPartner();
             int correct = partnerChoice(partner);
-            if( correct == 1) {
-                state = PARTNER;
-            } else if(correct == 0) {
+            if( correct == 2) {
+                state = START;
+            } else if(correct == 1) {
                 choice(partner);
                 lcd.refresh();
                 state = MENU;
             }
+        }
 
-        } else if (state == FIGHT) { //fight with wild pokemon
+        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
@@ -69,19 +72,15 @@
             if (box == 0) {
                 state = FIGHT;
                 lcd.clear();
-                lcd.printString("FIGHT",8,2);
             } else if (box ==1) {
                 state = POKEMON;
                 lcd.clear();
-                lcd.printString("POKEMON",20,4);
             } else if (box ==2) {
                 state = SETTINGS;
                 lcd.clear();
-                lcd.printString("SETTINGS",50,2);
             }
             lcd.refresh();
             wait(2.0);
-            lcd.clear();
 
         } else if (state == POKEMON) { //check partner pokemon stats
             state = MENU;
@@ -126,97 +125,107 @@
         pad.leds_off();
         wait(0.5);
     }
-    wait(1.0);
+    wait(0.5);
+    pad.init();
+    lcd.clear();
 }
 
-void select(int x,int y, int L, int H) //select box's
+void select(int x,int y)
 {
-    lcd.drawRect(x,y,L,H,FILL_TRANSPARENT);
+
+    lcd.setPixel(x,y);
+    lcd.setPixel(x-1,y+1);
+    lcd.setPixel(x-1,y-1);
+    lcd.refresh();
 }
 
-void deselect(int _x,int _y, int _L, int _H) //removing select box's
+void deselect(int _x,int _y)
 {
-    lcd.drawRect(_x,_y,_L,_H, FILL_WHITE);
+
+    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(11,18);
-    sp.ball(35,18);
-    sp.ball(59,18);
+    sp.ball(lcd, pad);
     lcd.refresh();
 }
 
 float drawPartner() //choice of pokeball
 {
-    select(9,16,15,15);
-    int offset = 0;
-    int _d = pad.get_direction();
+    lcd.clear();
+    int offset = 1;
+
     while(pad.check_event(Gamepad::A_PRESSED) == false) {
-        if(_d == E && offset <= 1) {
-            offset ++;
-        } else if(_d == W && offset >=1) {
-            offset --;
+        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(57,16,15,15);
-            deselect(33,16,15,15);
-            select(9,16,15,15);
+            deselect(33,25);
+            deselect(57,25);
+            select(9,25);
             balls();
+            wait(1.0);
         } else if(offset == 1) {
-            deselect(57,16,15,15);
-            deselect(9,16,15,15);
-            select(33,16,15,15);
+            deselect(9,25);
+            deselect(57,25);
+            select(33,25);
             balls();
+            wait(1.0);
         } else if(offset == 2) {
-            deselect(9,16,15,15);
-            deselect(33,16,15,15);
-            select(57,16,15,15);
+            deselect(9,25);
+            deselect(33,25);
+            select(57,25);
             balls();
+            wait(1.0);
         }
         lcd.refresh();
-        wait(0.5);
     }
+    wait(0.5);
+    pad.init();
     return offset;
+
 }
 
 float partnerChoice(int choice) //shows pokemon inside ball, gives choice to choose that mon
 {
     lcd.clear();
-    int _d = pad.get_direction();
     int end = 0;
     if(choice == 0) {
-        sp.bulbasaur(16,0); //grass starter
+        sp.bulbasaur(lcd, pad); //grass starter
     } else if(choice == 1) {
-        sp.charmander(16,0);//fire starter
+        sp.charmander(lcd, pad);//fire starter
     } else if(choice == 2) {
-        sp.squirtle(16,0);//water starter
+        sp.squirtle(lcd, pad);//water starter
     }
+    lcd.refresh();
+    wait(3.5);
+    lcd.clear();
     lcd.printString("Are you",0,2);
     lcd.printString(" sure??",0,3);
-    lcd.printString("Yes",65,1);
-    lcd.printString("No",65,5);
-    select(62,6,10,10);
+    lcd.printString("A-Yes",50,1);
+    lcd.printString("B-No",50,5);
     lcd.refresh();
-    while(pad.check_event(Gamepad::A_PRESSED)== false) {
-        lcd.printString("Yes",65,1);
-        lcd.printString("No",65,5);
+    wait(1.0);
+    while(end == 0) {
         lcd.refresh();
-        wait(0.25);
-        int offset1 = 0;
-        if(offset1 == 0 && _d == S) { //choose no
-            deselect(62,6,10,10);
-            select(62,38,10,10);
-            end = 0;
-        } else if(offset1 == 1 && _d ==N) {//choose yes
-            deselect(62,38,10,10);
-            select(62,6,10,10);
+        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;
         }
-        return offset1;
+        lcd.refresh();
+    }
+    wait(0.5);
+    pad.init();
+    return end;
 
-    }
-    return end;
 }
 
 void choice(int p) //sets up class for your chosen starter
@@ -240,34 +249,34 @@
 void menu()
 {
     lcd.clear();
-    lcd.printString("  FIGHT",8,1);
-    lcd.printString(" POKEMON",8,3);
-    lcd.printString(" SETTINGS",8,5);
+    lcd.printString("FIGHT",8,1);
+    lcd.printString("POKEMON",8,3);
+    lcd.printString("SETTINGS",8,5);
 }
 
 float drawMenu()
 {
     menu();
-    int Xo = 19;
+    int Xo = 6;
     int Yo = 11;
-    select(Xo, Yo,42,12);
+    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 >= 1 ) {
-            deselect(Xo, Yo,48,12);
+        if (d == S && box == 0 ||d == S && box == 1) {
+            deselect(Xo,Yo);
             Yo = Yo + 16;
-            select(Xo, Yo,48,12);
+            select(Xo,Yo);
             lcd.refresh();
             box = box + 1;
             wait(1.0);
 
-        } else if (d == N && box <= 1) {
-            deselect(Xo, Yo,48,12);
+        } else if (d == N && box == 1||d == N && box == 2) {
+            deselect(Xo,Yo);
             Yo = Yo - 16;
-            select(Xo, Yo,48,12);
+            select(Xo,Yo);
             lcd.refresh();
             box = box - 1;
             wait(1.0);
@@ -279,7 +288,103 @@
 
 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 = rand() %2;
+    int elvl;
+    if (ene2 ==1) {
+        elvl = 5;
+    } else {
+        elvl = 10;
+        if ( ene <= 6 ) {
+            ene2 = ene2;
+        } else if ( ene >= 13 ) {
+            ene2 = ene2 + 4;
+        } else {
+            ene2 = ene2 + 2;
+        }
+    }
+    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);
+    char OL[15];
+    sprintf(OL,"Lvl:%u",elvl);
+    lcd.printString(OL,35,1);
+    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(buffer,35,0);
+    lcd.printString(aa,2,4);
+    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 ;
+    //this is why im using value of 200 as a gauge for who wins a battle
+    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);
+        } 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 {
+            sprintf(HP1, "HP:%u",HPAT);
+            sprintf(HP2, "HP:%u",HPOT);
+            lcd.printString(HP1,2,5);
+            lcd.printString(HP2,35,1);
+            lcd.refresh();
+        }
+        
+    }
+    if(win == 1) {
+        pk.win();
+        lcd.clear();
+        lcd.printString("WIN!, you", 15, 2);
+        lcd.printString("gained 20xp!!",12,3);
+    } else if (win == 2) {
+        lcd.clear();
+        lcd.printString("LOSE!", 26, 2);
+    } else if (win == 3) {
+        lcd.clear();
+        lcd.printString("DRAW!", 26, 2);
+    }
+    lcd.refresh();
+    wait(2.0);
+    pad.init();
+
 }
+
 void drawPoke()
 {
     lcd.clear();
@@ -310,12 +415,14 @@
 {
     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) {
-        lcd.printString("SET BRIGHTNESS", 2, 20);
-        lcd.printString("B = BACK", 4, 20);
         bright = pad.read_pot();
         float brightness = 0.5*bright;
         lcd.setBrightness(brightness);
+        lcd.refresh();
     }
 }
 
    