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:
- 7:13333933413d
- Parent:
- 6:6e9a415436e0
- Child:
- 8:e47c48551124
--- a/main.cpp	Tue May 02 14:47:30 2017 +0000
+++ b/main.cpp	Wed May 03 15:16:27 2017 +0000
@@ -1,9 +1,23 @@
+/** 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.
 
@@ -12,6 +26,7 @@
 Gamepad pad;
 Pokemon pk;
 Images sp;
+Script sc;
 //--------------------------- Structs/Typedefs ---------------------------------
 struct joyInput {
     Direction d; //direction of joystick to navigate menu's
@@ -33,8 +48,11 @@
 void drawPoke();
 void menu();
 void settings();
-
-//------------------------------------------------------------------------------
+void ledCycle();
+float YesNo();
+void battleStats();
+float rndm(int RA);
+//-----------------------------Functions----------------------------------------
 
 int main()
 {
@@ -44,9 +62,11 @@
 //-----------------------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) {
@@ -59,14 +79,17 @@
         }
 
         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) {
@@ -83,14 +106,17 @@
             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();
@@ -98,32 +124,35 @@
     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()
 {
-
-    lcd.printString("  Welcome to  ",0,0);
-    lcd.printString(" the World of ",0,1);
-    lcd.printString("    Pokemon   ",0,2);
-    lcd.printString("  Press Start ",0,4);
-    lcd.refresh();
+    sc.Start(lcd);
 
     while( pad.check_event(Gamepad::START_PRESSED) == false) { // until start is pressed, this loop cycles
-        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);
+        ledCycle();
     }
     wait(0.5);
     pad.init();
@@ -152,13 +181,13 @@
 {
     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) {
@@ -171,32 +200,47 @@
             deselect(57,25);
             select(9,25);
             balls();
-            wait(1.0);
         } else if(offset == 1) {
             deselect(9,25);
             deselect(57,25);
             select(33,25);
             balls();
-            wait(1.0);
         } else if(offset == 2) {
             deselect(9,25);
             deselect(33,25);
             select(57,25);
             balls();
-            wait(1.0);
         }
-        lcd.refresh();
     }
     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();
-    int end = 0;
     if(choice == 0) {
         sp.bulbasaur(lcd, pad); //grass starter
     } else if(choice == 1) {
@@ -207,21 +251,7 @@
     lcd.refresh();
     wait(3.5);
     lcd.clear();
-    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();
-    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();
-    }
+    int end = YesNo();
     wait(0.5);
     pad.init();
     return end;
@@ -272,7 +302,6 @@
             lcd.refresh();
             box = box + 1;
             wait(1.0);
-
         } else if (d == N && box == 1||d == N && box == 2) {
             deselect(Xo,Yo);
             Yo = Yo - 16;
@@ -280,12 +309,43 @@
             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();
@@ -293,20 +353,7 @@
     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;
-        }
-    }
+    int ene2 = rndm(ene);
     std::string Pokename = PokemonNames[ene];
     Pokemon opponent = enemy[ene2];
     char buffer[64];
@@ -319,22 +366,9 @@
     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("LVL:??",35,1);
+    battleStats();
     lcd.printString(buffer,35,0);
-    lcd.printString(aa,2,4);
     char HP1[10];
     char HP2[10];
     int HPOT;
@@ -343,7 +377,7 @@
     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);
@@ -351,6 +385,7 @@
         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);
@@ -359,30 +394,19 @@
             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();
         }
-        
     }
-    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);
-    }
+    sc.Battle(lcd, win);
     lcd.refresh();
     wait(2.0);
     pad.init();
-
 }
 
 void drawPoke()
    