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
main.cpp@8:e47c48551124, 2017-05-03 (annotated)
- Committer:
- 200923317
- Date:
- Wed May 03 21:44:43 2017 +0000
- Revision:
- 8:e47c48551124
- Parent:
- 7:13333933413d
- Child:
- 9:67427ba150c8
FINAL
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 200923317 | 7:13333933413d | 1 | /** Pokemon Game |
| 200923317 | 7:13333933413d | 2 | @brief Game based on the main series Pokemon games by the pokemon company and Gamefreak. |
| 200923317 | 7:13333933413d | 3 | @brief whole game is a battle simulator, where you can level up your companion by battling other wild pokemon |
| 200923317 | 7:13333933413d | 4 | @brief You can view your pokemons stats and change the screen brightness from the menus. |
| 200923317 | 7:13333933413d | 5 | |
| 200923317 | 8:e47c48551124 | 6 | @brief Revision 2 |
| 200923317 | 7:13333933413d | 7 | |
| 200923317 | 7:13333933413d | 8 | @author Aaron Lad |
| 200923317 | 7:13333933413d | 9 | @date 2nd May 2017 |
| 200923317 | 7:13333933413d | 10 | |
| 200923317 | 7:13333933413d | 11 | @code |
| 200923317 | 7:13333933413d | 12 | */ |
| 200923317 | 7:13333933413d | 13 | |
| 200923317 | 0:cd3f75767e71 | 14 | #include "mbed.h" |
| 200923317 | 0:cd3f75767e71 | 15 | #include "Gamepad.h" |
| 200923317 | 0:cd3f75767e71 | 16 | #include "N5110.h" |
| 200923317 | 0:cd3f75767e71 | 17 | #include "Pokemon.h" |
| 200923317 | 3:b4de529de482 | 18 | #include "Images.h" |
| 200923317 | 4:713ac9379ac6 | 19 | #include "Bitmap.h" |
| 200923317 | 7:13333933413d | 20 | #include "Script.h" |
| 200923317 | 0:cd3f75767e71 | 21 | |
| 200923317 | 0:cd3f75767e71 | 22 | //rewritten code to implement new/better ideas. part of the code is same as before but lots of changes were needed. |
| 200923317 | 8:e47c48551124 | 23 | //tried several attempts to Use a GameEngine class rather than using the main.cpp but ultimately was not able to |
| 200923317 | 0:cd3f75767e71 | 24 | |
| 200923317 | 0:cd3f75767e71 | 25 | //-------------------------------- objects ------------------------------------- |
| 200923317 | 0:cd3f75767e71 | 26 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| 200923317 | 0:cd3f75767e71 | 27 | Gamepad pad; |
| 200923317 | 0:cd3f75767e71 | 28 | Pokemon pk; |
| 200923317 | 3:b4de529de482 | 29 | Images sp; |
| 200923317 | 7:13333933413d | 30 | Script sc; |
| 200923317 | 0:cd3f75767e71 | 31 | //--------------------------- Structs/Typedefs --------------------------------- |
| 200923317 | 0:cd3f75767e71 | 32 | struct joyInput { |
| 200923317 | 0:cd3f75767e71 | 33 | Direction d; //direction of joystick to navigate menu's |
| 200923317 | 0:cd3f75767e71 | 34 | }; |
| 200923317 | 0:cd3f75767e71 | 35 | |
| 200923317 | 0:cd3f75767e71 | 36 | typedef enum State {START, PARTNER, MENU, FIGHT, POKEMON, SETTINGS} Gamepage; |
| 200923317 | 0:cd3f75767e71 | 37 | //assigning names to different states |
| 200923317 | 0:cd3f75767e71 | 38 | |
| 200923317 | 0:cd3f75767e71 | 39 | void init(); |
| 200923317 | 0:cd3f75767e71 | 40 | void drawStart(); |
| 200923317 | 6:6e9a415436e0 | 41 | void select(int x, int y); |
| 200923317 | 6:6e9a415436e0 | 42 | void deselect(int _x, int _y); |
| 200923317 | 2:a2bb794f830c | 43 | void balls(); |
| 200923317 | 2:a2bb794f830c | 44 | float drawPartner(); |
| 200923317 | 2:a2bb794f830c | 45 | float partnerChoice(int choice); |
| 200923317 | 2:a2bb794f830c | 46 | void choice(int p); |
| 200923317 | 0:cd3f75767e71 | 47 | float drawMenu(); |
| 200923317 | 0:cd3f75767e71 | 48 | void drawFight(); |
| 200923317 | 0:cd3f75767e71 | 49 | void drawPoke(); |
| 200923317 | 1:af881f58c4f9 | 50 | void settings(); |
| 200923317 | 7:13333933413d | 51 | void ledCycle(); |
| 200923317 | 7:13333933413d | 52 | float YesNo(); |
| 200923317 | 7:13333933413d | 53 | void battleStats(); |
| 200923317 | 7:13333933413d | 54 | float rndm(int RA); |
| 200923317 | 7:13333933413d | 55 | //-----------------------------Functions---------------------------------------- |
| 200923317 | 1:af881f58c4f9 | 56 | |
| 200923317 | 1:af881f58c4f9 | 57 | int main() |
| 200923317 | 1:af881f58c4f9 | 58 | { |
| 200923317 | 4:713ac9379ac6 | 59 | init(); //initialising screen and gamepad |
| 200923317 | 4:713ac9379ac6 | 60 | drawStart(); //screen shows startup screen |
| 200923317 | 4:713ac9379ac6 | 61 | Gamepage state = START; //go to start state |
| 200923317 | 8:e47c48551124 | 62 | /*-----------------------GAME LOOP START------------------------------- |
| 200923317 | 8:e47c48551124 | 63 | Uses a game loop to move through the states of the game system |
| 200923317 | 8:e47c48551124 | 64 | */ |
| 200923317 | 2:a2bb794f830c | 65 | while(1) { |
| 200923317 | 2:a2bb794f830c | 66 | if (state == START) { |
| 200923317 | 7:13333933413d | 67 | |
| 200923317 | 8:e47c48551124 | 68 | state = PARTNER;//change next state to Partner state |
| 200923317 | 3:b4de529de482 | 69 | |
| 200923317 | 5:add0351183be | 70 | } else if (state == PARTNER) { //choosing your partner |
| 200923317 | 7:13333933413d | 71 | |
| 200923317 | 2:a2bb794f830c | 72 | int partner = drawPartner(); |
| 200923317 | 2:a2bb794f830c | 73 | int correct = partnerChoice(partner); |
| 200923317 | 6:6e9a415436e0 | 74 | if( correct == 2) { |
| 200923317 | 8:e47c48551124 | 75 | state = START; //change next state to Start state |
| 200923317 | 6:6e9a415436e0 | 76 | } else if(correct == 1) { |
| 200923317 | 2:a2bb794f830c | 77 | choice(partner); |
| 200923317 | 2:a2bb794f830c | 78 | lcd.refresh(); |
| 200923317 | 8:e47c48551124 | 79 | state = MENU; //change next state to Menu state |
| 200923317 | 2:a2bb794f830c | 80 | } |
| 200923317 | 6:6e9a415436e0 | 81 | } |
| 200923317 | 3:b4de529de482 | 82 | |
| 200923317 | 6:6e9a415436e0 | 83 | else if (state == FIGHT) { //fight with wild pokemon |
| 200923317 | 7:13333933413d | 84 | |
| 200923317 | 6:6e9a415436e0 | 85 | drawFight(); |
| 200923317 | 8:e47c48551124 | 86 | state = MENU;//change next state to Menu state |
| 200923317 | 3:b4de529de482 | 87 | |
| 200923317 | 4:713ac9379ac6 | 88 | } else if (state == SETTINGS) { //settings, screen brightness |
| 200923317 | 7:13333933413d | 89 | |
| 200923317 | 6:6e9a415436e0 | 90 | settings(); |
| 200923317 | 8:e47c48551124 | 91 | state = MENU;//change next state to Menu state |
| 200923317 | 3:b4de529de482 | 92 | |
| 200923317 | 4:713ac9379ac6 | 93 | } else if (state == MENU) { // main menu |
| 200923317 | 7:13333933413d | 94 | |
| 200923317 | 2:a2bb794f830c | 95 | wait(1.0); |
| 200923317 | 2:a2bb794f830c | 96 | int box = drawMenu(); |
| 200923317 | 2:a2bb794f830c | 97 | if (box == 0) { |
| 200923317 | 8:e47c48551124 | 98 | state = FIGHT;//change next state to Fight state |
| 200923317 | 2:a2bb794f830c | 99 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 100 | } else if (box ==1) { |
| 200923317 | 8:e47c48551124 | 101 | state = POKEMON;//change next state to Pokemon state |
| 200923317 | 2:a2bb794f830c | 102 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 103 | } else if (box ==2) { |
| 200923317 | 8:e47c48551124 | 104 | state = SETTINGS;//change next state to Settings state |
| 200923317 | 2:a2bb794f830c | 105 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 106 | } |
| 200923317 | 2:a2bb794f830c | 107 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 108 | wait(2.0); |
| 200923317 | 3:b4de529de482 | 109 | |
| 200923317 | 4:713ac9379ac6 | 110 | } else if (state == POKEMON) { //check partner pokemon stats |
| 200923317 | 7:13333933413d | 111 | |
| 200923317 | 8:e47c48551124 | 112 | state = MENU;//change next state to Menu state |
| 200923317 | 2:a2bb794f830c | 113 | drawPoke(); |
| 200923317 | 2:a2bb794f830c | 114 | } |
| 200923317 | 2:a2bb794f830c | 115 | } |
| 200923317 | 2:a2bb794f830c | 116 | } |
| 200923317 | 2:a2bb794f830c | 117 | |
| 200923317 | 7:13333933413d | 118 | /** initialise screen and pad |
| 200923317 | 7:13333933413d | 119 | *powers up the display and configures the gamepad |
| 200923317 | 7:13333933413d | 120 | *sets the screen brightness to 100% |
| 200923317 | 7:13333933413d | 121 | */ |
| 200923317 | 1:af881f58c4f9 | 122 | void init() |
| 200923317 | 1:af881f58c4f9 | 123 | { |
| 200923317 | 8:e47c48551124 | 124 | //need to initialise gamepad and LCD screen |
| 200923317 | 1:af881f58c4f9 | 125 | pad.init(); |
| 200923317 | 1:af881f58c4f9 | 126 | lcd.init(); |
| 200923317 | 8:e47c48551124 | 127 | //set screen brightness to full by default |
| 200923317 | 1:af881f58c4f9 | 128 | lcd.setBrightness(1.0); |
| 200923317 | 2:a2bb794f830c | 129 | } |
| 200923317 | 2:a2bb794f830c | 130 | |
| 200923317 | 8:e47c48551124 | 131 | /** LED Cycle |
| 200923317 | 8:e47c48551124 | 132 | *turns the leds of the game pad on and off in sequence |
| 200923317 | 8:e47c48551124 | 133 | *using 1 to turn on LED and 0 to turn off |
| 200923317 | 8:e47c48551124 | 134 | */ |
| 200923317 | 7:13333933413d | 135 | void ledCycle() |
| 200923317 | 7:13333933413d | 136 | { |
| 200923317 | 7:13333933413d | 137 | pad.led(1,1); //cycle through LED's on gamepad |
| 200923317 | 7:13333933413d | 138 | pad.led(6,1); |
| 200923317 | 7:13333933413d | 139 | wait(0.5); |
| 200923317 | 7:13333933413d | 140 | pad.led(2,1); |
| 200923317 | 7:13333933413d | 141 | pad.led(5,1); |
| 200923317 | 7:13333933413d | 142 | wait(0.5); |
| 200923317 | 8:e47c48551124 | 143 | //all LEDS on |
| 200923317 | 7:13333933413d | 144 | pad.leds_on(); |
| 200923317 | 7:13333933413d | 145 | wait(0.5); |
| 200923317 | 7:13333933413d | 146 | pad.led(1,0); |
| 200923317 | 7:13333933413d | 147 | pad.led(6,0); |
| 200923317 | 7:13333933413d | 148 | wait(0.5); |
| 200923317 | 7:13333933413d | 149 | pad.led(2,0); |
| 200923317 | 7:13333933413d | 150 | pad.led(5,0); |
| 200923317 | 7:13333933413d | 151 | wait(0.5); |
| 200923317 | 7:13333933413d | 152 | pad.leds_off(); |
| 200923317 | 8:e47c48551124 | 153 | //all LEDs off |
| 200923317 | 7:13333933413d | 154 | wait(0.5); |
| 200923317 | 7:13333933413d | 155 | } |
| 200923317 | 7:13333933413d | 156 | |
| 200923317 | 8:e47c48551124 | 157 | /** Draw the Start state |
| 200923317 | 8:e47c48551124 | 158 | *Draws the startup screen for the game |
| 200923317 | 8:e47c48551124 | 159 | *uses a check for the start button to start the game loop |
| 200923317 | 7:13333933413d | 160 | */ |
| 200923317 | 2:a2bb794f830c | 161 | void drawStart() |
| 200923317 | 2:a2bb794f830c | 162 | { |
| 200923317 | 8:e47c48551124 | 163 | //load script from Library |
| 200923317 | 7:13333933413d | 164 | sc.Start(lcd); |
| 200923317 | 8:e47c48551124 | 165 | //detect when Start button is pressed |
| 200923317 | 4:713ac9379ac6 | 166 | while( pad.check_event(Gamepad::START_PRESSED) == false) { // until start is pressed, this loop cycles |
| 200923317 | 7:13333933413d | 167 | ledCycle(); |
| 200923317 | 2:a2bb794f830c | 168 | } |
| 200923317 | 6:6e9a415436e0 | 169 | wait(0.5); |
| 200923317 | 8:e47c48551124 | 170 | //reset gamepad to counteract button bounce |
| 200923317 | 6:6e9a415436e0 | 171 | pad.init(); |
| 200923317 | 8:e47c48551124 | 172 | //clear screen |
| 200923317 | 6:6e9a415436e0 | 173 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 174 | } |
| 200923317 | 8:e47c48551124 | 175 | /** draw select Cursor |
| 200923317 | 8:e47c48551124 | 176 | *Draws the cursor used for selecting options |
| 200923317 | 8:e47c48551124 | 177 | *refreshes screen every time called to make sure cursors are shown |
| 200923317 | 8:e47c48551124 | 178 | * @param x - the column number (0-83) |
| 200923317 | 8:e47c48551124 | 179 | * @param y - the row number (0-42) |
| 200923317 | 8:e47c48551124 | 180 | */ |
| 200923317 | 6:6e9a415436e0 | 181 | void select(int x,int y) |
| 200923317 | 2:a2bb794f830c | 182 | { |
| 200923317 | 8:e47c48551124 | 183 | //Print 3 pixel cursor to screen |
| 200923317 | 6:6e9a415436e0 | 184 | lcd.setPixel(x,y); |
| 200923317 | 6:6e9a415436e0 | 185 | lcd.setPixel(x-1,y+1); |
| 200923317 | 6:6e9a415436e0 | 186 | lcd.setPixel(x-1,y-1); |
| 200923317 | 6:6e9a415436e0 | 187 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 188 | } |
| 200923317 | 8:e47c48551124 | 189 | /** removes select Cursor |
| 200923317 | 8:e47c48551124 | 190 | *clears the cursor used for selecting options |
| 200923317 | 8:e47c48551124 | 191 | *refreshes screen every time called to make sure cursors are removed from the screen |
| 200923317 | 8:e47c48551124 | 192 | * @param x - the column number (0-83) |
| 200923317 | 8:e47c48551124 | 193 | * @param y - the row number (0-42) |
| 200923317 | 8:e47c48551124 | 194 | */ |
| 200923317 | 6:6e9a415436e0 | 195 | void deselect(int _x,int _y) |
| 200923317 | 2:a2bb794f830c | 196 | { |
| 200923317 | 8:e47c48551124 | 197 | //remove 3 pixel cursor from screen buffer |
| 200923317 | 6:6e9a415436e0 | 198 | lcd.setPixel(_x,_y,false); |
| 200923317 | 6:6e9a415436e0 | 199 | lcd.setPixel(_x-1,_y+1,false); |
| 200923317 | 6:6e9a415436e0 | 200 | lcd.setPixel(_x-1,_y-1,false); |
| 200923317 | 6:6e9a415436e0 | 201 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 202 | } |
| 200923317 | 8:e47c48551124 | 203 | /** Print the Ball sprites |
| 200923317 | 8:e47c48551124 | 204 | *Calls the bitmap from the Images class and prints the Pokeball sprite to the buffer in the required loactions |
| 200923317 | 8:e47c48551124 | 205 | *refreshes screeen when called to make sure Pokeballs are displayed |
| 200923317 | 8:e47c48551124 | 206 | */ |
| 200923317 | 4:713ac9379ac6 | 207 | void balls() //show the pokeballs on the screen for player to choose from |
| 200923317 | 2:a2bb794f830c | 208 | { |
| 200923317 | 8:e47c48551124 | 209 | //call sprite library to print balls |
| 200923317 | 6:6e9a415436e0 | 210 | sp.ball(lcd, pad); |
| 200923317 | 2:a2bb794f830c | 211 | lcd.refresh(); |
| 200923317 | 7:13333933413d | 212 | wait(1.0); |
| 200923317 | 2:a2bb794f830c | 213 | } |
| 200923317 | 5:add0351183be | 214 | |
| 200923317 | 8:e47c48551124 | 215 | /** Draw Partner state |
| 200923317 | 8:e47c48551124 | 216 | *Allows player to select a pokeball which would contain the Pokemon used as their starter |
| 200923317 | 8:e47c48551124 | 217 | *uses the joystick to move a cursor and the A button to select which ball |
| 200923317 | 8:e47c48551124 | 218 | *Returns the value of the chosen ball |
| 200923317 | 8:e47c48551124 | 219 | */ |
| 200923317 | 4:713ac9379ac6 | 220 | float drawPartner() //choice of pokeball |
| 200923317 | 2:a2bb794f830c | 221 | { |
| 200923317 | 6:6e9a415436e0 | 222 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 223 | //set variable for Pokemon choice |
| 200923317 | 6:6e9a415436e0 | 224 | int offset = 1; |
| 200923317 | 8:e47c48551124 | 225 | //detect for A button press |
| 200923317 | 2:a2bb794f830c | 226 | while(pad.check_event(Gamepad::A_PRESSED) == false) { |
| 200923317 | 6:6e9a415436e0 | 227 | int _d = pad.get_direction(); |
| 200923317 | 8:e47c48551124 | 228 | //detect for Joystick pushed right |
| 200923317 | 6:6e9a415436e0 | 229 | if(_d == E && offset == 1||_d == E && offset == 0) { |
| 200923317 | 6:6e9a415436e0 | 230 | offset = offset + 1; |
| 200923317 | 8:e47c48551124 | 231 | //detect fo Joystick pushed left |
| 200923317 | 6:6e9a415436e0 | 232 | } else if(_d == W && offset == 1 || _d == W && offset == 2 ) { |
| 200923317 | 6:6e9a415436e0 | 233 | offset = offset - 1; |
| 200923317 | 2:a2bb794f830c | 234 | } |
| 200923317 | 8:e47c48551124 | 235 | //Print and remove cursors |
| 200923317 | 2:a2bb794f830c | 236 | if(offset == 0) { |
| 200923317 | 6:6e9a415436e0 | 237 | deselect(33,25); |
| 200923317 | 6:6e9a415436e0 | 238 | deselect(57,25); |
| 200923317 | 6:6e9a415436e0 | 239 | select(9,25); |
| 200923317 | 2:a2bb794f830c | 240 | balls(); |
| 200923317 | 2:a2bb794f830c | 241 | } else if(offset == 1) { |
| 200923317 | 6:6e9a415436e0 | 242 | deselect(9,25); |
| 200923317 | 6:6e9a415436e0 | 243 | deselect(57,25); |
| 200923317 | 6:6e9a415436e0 | 244 | select(33,25); |
| 200923317 | 2:a2bb794f830c | 245 | balls(); |
| 200923317 | 2:a2bb794f830c | 246 | } else if(offset == 2) { |
| 200923317 | 6:6e9a415436e0 | 247 | deselect(9,25); |
| 200923317 | 6:6e9a415436e0 | 248 | deselect(33,25); |
| 200923317 | 6:6e9a415436e0 | 249 | select(57,25); |
| 200923317 | 2:a2bb794f830c | 250 | balls(); |
| 200923317 | 2:a2bb794f830c | 251 | } |
| 200923317 | 2:a2bb794f830c | 252 | } |
| 200923317 | 6:6e9a415436e0 | 253 | wait(0.5); |
| 200923317 | 8:e47c48551124 | 254 | //reset gamepad to counteract button bounce |
| 200923317 | 6:6e9a415436e0 | 255 | pad.init(); |
| 200923317 | 2:a2bb794f830c | 256 | return offset; |
| 200923317 | 7:13333933413d | 257 | } |
| 200923317 | 6:6e9a415436e0 | 258 | |
| 200923317 | 8:e47c48551124 | 259 | /** Yes/No Choice |
| 200923317 | 8:e47c48551124 | 260 | *For use in the partner state to allow players to confirm choice of starters |
| 200923317 | 8:e47c48551124 | 261 | *Uses the use of the A and B buttons |
| 200923317 | 8:e47c48551124 | 262 | */ |
| 200923317 | 7:13333933413d | 263 | float YesNo() |
| 200923317 | 7:13333933413d | 264 | { |
| 200923317 | 8:e47c48551124 | 265 | //Call text from Script library |
| 200923317 | 8:e47c48551124 | 266 | sc.YN(lcd); |
| 200923317 | 7:13333933413d | 267 | int end = 0; |
| 200923317 | 7:13333933413d | 268 | wait(1.0); |
| 200923317 | 8:e47c48551124 | 269 | //loop until a choice is made |
| 200923317 | 7:13333933413d | 270 | while(end == 0) { |
| 200923317 | 7:13333933413d | 271 | lcd.refresh(); |
| 200923317 | 7:13333933413d | 272 | if(pad.check_event(Gamepad::A_PRESSED)== true) { //choose yes |
| 200923317 | 7:13333933413d | 273 | end = 1; |
| 200923317 | 7:13333933413d | 274 | } else if(pad.check_event(Gamepad::B_PRESSED)== true) {//choose no |
| 200923317 | 7:13333933413d | 275 | end = 2; |
| 200923317 | 7:13333933413d | 276 | } |
| 200923317 | 7:13333933413d | 277 | } |
| 200923317 | 8:e47c48551124 | 278 | //use return to load next scren in choice screen |
| 200923317 | 7:13333933413d | 279 | return end; |
| 200923317 | 2:a2bb794f830c | 280 | } |
| 200923317 | 2:a2bb794f830c | 281 | |
| 200923317 | 8:e47c48551124 | 282 | /** Load partner choice |
| 200923317 | 8:e47c48551124 | 283 | *Displays the Starter pokemon from the Chosen pokemon |
| 200923317 | 8:e47c48551124 | 284 | *Then passes on the choice of wether the player wants to choose that starter |
| 200923317 | 8:e47c48551124 | 285 | */ |
| 200923317 | 4:713ac9379ac6 | 286 | float partnerChoice(int choice) //shows pokemon inside ball, gives choice to choose that mon |
| 200923317 | 2:a2bb794f830c | 287 | { |
| 200923317 | 2:a2bb794f830c | 288 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 289 | //using choice value to determine pokeball chosen |
| 200923317 | 2:a2bb794f830c | 290 | if(choice == 0) { |
| 200923317 | 6:6e9a415436e0 | 291 | sp.bulbasaur(lcd, pad); //grass starter |
| 200923317 | 2:a2bb794f830c | 292 | } else if(choice == 1) { |
| 200923317 | 6:6e9a415436e0 | 293 | sp.charmander(lcd, pad);//fire starter |
| 200923317 | 2:a2bb794f830c | 294 | } else if(choice == 2) { |
| 200923317 | 6:6e9a415436e0 | 295 | sp.squirtle(lcd, pad);//water starter |
| 200923317 | 2:a2bb794f830c | 296 | } |
| 200923317 | 6:6e9a415436e0 | 297 | lcd.refresh(); |
| 200923317 | 6:6e9a415436e0 | 298 | wait(3.5); |
| 200923317 | 6:6e9a415436e0 | 299 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 300 | //load decision |
| 200923317 | 7:13333933413d | 301 | int end = YesNo(); |
| 200923317 | 6:6e9a415436e0 | 302 | wait(0.5); |
| 200923317 | 8:e47c48551124 | 303 | //reset gamepad to counteract button bounce |
| 200923317 | 6:6e9a415436e0 | 304 | pad.init(); |
| 200923317 | 6:6e9a415436e0 | 305 | return end; |
| 200923317 | 4:713ac9379ac6 | 306 | |
| 200923317 | 2:a2bb794f830c | 307 | } |
| 200923317 | 2:a2bb794f830c | 308 | |
| 200923317 | 8:e47c48551124 | 309 | /** Choice setting |
| 200923317 | 8:e47c48551124 | 310 | *Sets the type for your chosen pokemon, and display the name of your pokemon |
| 200923317 | 8:e47c48551124 | 311 | * @param p - used to select which pokemon was chosen by player |
| 200923317 | 8:e47c48551124 | 312 | */ |
| 200923317 | 4:713ac9379ac6 | 313 | void choice(int p) //sets up class for your chosen starter |
| 200923317 | 2:a2bb794f830c | 314 | { |
| 200923317 | 2:a2bb794f830c | 315 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 316 | //using input to set pokemon used as Starter |
| 200923317 | 2:a2bb794f830c | 317 | if (p == 0) { |
| 200923317 | 8:e47c48551124 | 318 | //Setting Pokemon Type as grass - Bulbasaur |
| 200923317 | 2:a2bb794f830c | 319 | pk.setType(Grass); |
| 200923317 | 2:a2bb794f830c | 320 | lcd.printString("You Chose",16,2); |
| 200923317 | 2:a2bb794f830c | 321 | lcd.printString("Bulbasaur",15,3); |
| 200923317 | 2:a2bb794f830c | 322 | } else if (p == 1) { |
| 200923317 | 8:e47c48551124 | 323 | //Setting Pokemon Type as Fire - Charmander |
| 200923317 | 2:a2bb794f830c | 324 | pk.setType(Fire); |
| 200923317 | 2:a2bb794f830c | 325 | lcd.printString("You Chose",16,2); |
| 200923317 | 2:a2bb794f830c | 326 | lcd.printString("Charmander",15,3); |
| 200923317 | 2:a2bb794f830c | 327 | } else if (p == 2) { |
| 200923317 | 8:e47c48551124 | 328 | //Setting Pokemon Type as Water - Bulbasaur |
| 200923317 | 2:a2bb794f830c | 329 | pk.setType(Water); |
| 200923317 | 2:a2bb794f830c | 330 | lcd.printString("You Chose ",16,2); |
| 200923317 | 2:a2bb794f830c | 331 | lcd.printString("Squirtle",15,3); |
| 200923317 | 2:a2bb794f830c | 332 | } |
| 200923317 | 2:a2bb794f830c | 333 | } |
| 200923317 | 8:e47c48551124 | 334 | /** Draw Menu State |
| 200923317 | 8:e47c48551124 | 335 | *Prints the menu to the screen |
| 200923317 | 8:e47c48551124 | 336 | *uses a cursor to allow user to choose wether they want to fight, check pokemon or go to settings |
| 200923317 | 8:e47c48551124 | 337 | *returns the choice of the player |
| 200923317 | 8:e47c48551124 | 338 | */ |
| 200923317 | 2:a2bb794f830c | 339 | |
| 200923317 | 3:b4de529de482 | 340 | float drawMenu() |
| 200923317 | 3:b4de529de482 | 341 | { |
| 200923317 | 8:e47c48551124 | 342 | //load menu script from script library |
| 200923317 | 8:e47c48551124 | 343 | sc.menu(lcd); |
| 200923317 | 8:e47c48551124 | 344 | //set initial values for X and Y for select |
| 200923317 | 6:6e9a415436e0 | 345 | int Xo = 6; |
| 200923317 | 5:add0351183be | 346 | int Yo = 11; |
| 200923317 | 6:6e9a415436e0 | 347 | select(Xo, Yo); |
| 200923317 | 5:add0351183be | 348 | lcd.refresh(); |
| 200923317 | 5:add0351183be | 349 | wait(1.0); |
| 200923317 | 5:add0351183be | 350 | int box = 0; |
| 200923317 | 8:e47c48551124 | 351 | //Check for A button pressed, loop until it is pressed |
| 200923317 | 5:add0351183be | 352 | while( pad.check_event(Gamepad::A_PRESSED) == false) { |
| 200923317 | 8:e47c48551124 | 353 | //get direction from joystick |
| 200923317 | 5:add0351183be | 354 | int d = pad.get_direction(); |
| 200923317 | 8:e47c48551124 | 355 | //check for stick moved down |
| 200923317 | 6:6e9a415436e0 | 356 | if (d == S && box == 0 ||d == S && box == 1) { |
| 200923317 | 8:e47c48551124 | 357 | //move cursor |
| 200923317 | 6:6e9a415436e0 | 358 | deselect(Xo,Yo); |
| 200923317 | 5:add0351183be | 359 | Yo = Yo + 16; |
| 200923317 | 6:6e9a415436e0 | 360 | select(Xo,Yo); |
| 200923317 | 5:add0351183be | 361 | lcd.refresh(); |
| 200923317 | 5:add0351183be | 362 | box = box + 1; |
| 200923317 | 5:add0351183be | 363 | wait(1.0); |
| 200923317 | 8:e47c48551124 | 364 | //check for stick moved up |
| 200923317 | 6:6e9a415436e0 | 365 | } else if (d == N && box == 1||d == N && box == 2) { |
| 200923317 | 8:e47c48551124 | 366 | //move cursor |
| 200923317 | 6:6e9a415436e0 | 367 | deselect(Xo,Yo); |
| 200923317 | 5:add0351183be | 368 | Yo = Yo - 16; |
| 200923317 | 6:6e9a415436e0 | 369 | select(Xo,Yo); |
| 200923317 | 5:add0351183be | 370 | lcd.refresh(); |
| 200923317 | 5:add0351183be | 371 | box = box - 1; |
| 200923317 | 5:add0351183be | 372 | wait(1.0); |
| 200923317 | 5:add0351183be | 373 | } |
| 200923317 | 5:add0351183be | 374 | } |
| 200923317 | 8:e47c48551124 | 375 | //reset gamepad to counteract button bounce |
| 200923317 | 8:e47c48551124 | 376 | pad.init(); |
| 200923317 | 5:add0351183be | 377 | return box; |
| 200923317 | 3:b4de529de482 | 378 | } |
| 200923317 | 5:add0351183be | 379 | |
| 200923317 | 8:e47c48551124 | 380 | /** Randomiser float |
| 200923317 | 8:e47c48551124 | 381 | *used to select and return a random number |
| 200923317 | 8:e47c48551124 | 382 | *keeps the Pokemon type in correspondence to the pokemon the player comes up against |
| 200923317 | 8:e47c48551124 | 383 | * @param RA - random number which chose the wild pokemon you come up against, used to chose pokemon type |
| 200923317 | 8:e47c48551124 | 384 | */ |
| 200923317 | 7:13333933413d | 385 | float rndm(int RA) |
| 200923317 | 7:13333933413d | 386 | { |
| 200923317 | 7:13333933413d | 387 | int R = rand() %2; |
| 200923317 | 7:13333933413d | 388 | |
| 200923317 | 7:13333933413d | 389 | if (R ==1) { |
| 200923317 | 7:13333933413d | 390 | } else { |
| 200923317 | 7:13333933413d | 391 | if ( RA <= 6 ) { |
| 200923317 | 7:13333933413d | 392 | R = R; |
| 200923317 | 7:13333933413d | 393 | } else if ( RA >= 13 ) { |
| 200923317 | 7:13333933413d | 394 | R = R + 4; |
| 200923317 | 7:13333933413d | 395 | } else { |
| 200923317 | 7:13333933413d | 396 | R = R + 2; |
| 200923317 | 7:13333933413d | 397 | } |
| 200923317 | 7:13333933413d | 398 | } |
| 200923317 | 7:13333933413d | 399 | return R; |
| 200923317 | 7:13333933413d | 400 | } |
| 200923317 | 8:e47c48551124 | 401 | |
| 200923317 | 8:e47c48551124 | 402 | /**Battle stats |
| 200923317 | 8:e47c48551124 | 403 | * Displays the Name of the pokemon you come against |
| 200923317 | 8:e47c48551124 | 404 | * Also displays name and level of your pokemon |
| 200923317 | 8:e47c48551124 | 405 | */ |
| 200923317 | 7:13333933413d | 406 | void battleStats() |
| 200923317 | 7:13333933413d | 407 | { |
| 200923317 | 8:e47c48551124 | 408 | //taking information from Pokemon class, adding them to strings |
| 200923317 | 7:13333933413d | 409 | std::string pkn = pk.Name(); |
| 200923317 | 7:13333933413d | 410 | std::string pkl = pk.Level(); |
| 200923317 | 7:13333933413d | 411 | char aa[50]; |
| 200923317 | 7:13333933413d | 412 | char bb[50]; |
| 200923317 | 8:e47c48551124 | 413 | //copying strings to buffers, so they can be added to buffer |
| 200923317 | 7:13333933413d | 414 | strncpy(aa, pkn.c_str(), sizeof(aa)); |
| 200923317 | 7:13333933413d | 415 | strncpy(bb, pkl.c_str(), sizeof(bb)); |
| 200923317 | 7:13333933413d | 416 | lcd.printString(aa,2,4); |
| 200923317 | 7:13333933413d | 417 | lcd.printString(bb,2,5); |
| 200923317 | 7:13333933413d | 418 | lcd.refresh(); |
| 200923317 | 7:13333933413d | 419 | wait(2.0); |
| 200923317 | 7:13333933413d | 420 | lcd.clear(); |
| 200923317 | 7:13333933413d | 421 | lcd.printString(aa,2,4); |
| 200923317 | 7:13333933413d | 422 | } |
| 200923317 | 8:e47c48551124 | 423 | /** Draw fight class |
| 200923317 | 8:e47c48551124 | 424 | * randomise 2 integers to get a pokemon name and stats for said pokemon for yours to figth |
| 200923317 | 8:e47c48551124 | 425 | * shows HP as battle progresses |
| 200923317 | 8:e47c48551124 | 426 | * pulls battle functions and typings from the pokemon class |
| 200923317 | 8:e47c48551124 | 427 | */ |
| 200923317 | 3:b4de529de482 | 428 | void drawFight() |
| 200923317 | 3:b4de529de482 | 429 | { |
| 200923317 | 6:6e9a415436e0 | 430 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 431 | //setting randomiser |
| 200923317 | 6:6e9a415436e0 | 432 | srand(time(NULL)); |
| 200923317 | 8:e47c48551124 | 433 | //Pokemon name list, ordered Fire, Grass then water |
| 200923317 | 6:6e9a415436e0 | 434 | std::string PokemonNames[18] = {"Charmander","Cyndaquil","Torchic","Chimchar","Tepig","Fennekin","Bulbasaur","Chikorita","Treeko","Turtwig","Snivy","Chespin","Squirtle","Totodile","Mudkip","Piplup","Oshawott","Froakie"}; |
| 200923317 | 8:e47c48551124 | 435 | //Pokemon level and type list |
| 200923317 | 6:6e9a415436e0 | 436 | 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)}; |
| 200923317 | 8:e47c48551124 | 437 | //setting a value for the name randomiser and type randomiser |
| 200923317 | 6:6e9a415436e0 | 438 | int ene = rand() % 18; |
| 200923317 | 7:13333933413d | 439 | int ene2 = rndm(ene); |
| 200923317 | 8:e47c48551124 | 440 | //Copying pokemon name to buffer to be printed onto the screen |
| 200923317 | 6:6e9a415436e0 | 441 | std::string Pokename = PokemonNames[ene]; |
| 200923317 | 6:6e9a415436e0 | 442 | Pokemon opponent = enemy[ene2]; |
| 200923317 | 6:6e9a415436e0 | 443 | char buffer[64]; |
| 200923317 | 6:6e9a415436e0 | 444 | std::string nameStr = Pokename; |
| 200923317 | 6:6e9a415436e0 | 445 | strncpy(buffer, nameStr.c_str(), sizeof(buffer)); |
| 200923317 | 6:6e9a415436e0 | 446 | lcd.printString("A wild",25,2); |
| 200923317 | 6:6e9a415436e0 | 447 | lcd.printString(buffer,18,3); |
| 200923317 | 6:6e9a415436e0 | 448 | lcd.printString("appeared",20,4); |
| 200923317 | 6:6e9a415436e0 | 449 | lcd.refresh(); |
| 200923317 | 6:6e9a415436e0 | 450 | wait(2.0); |
| 200923317 | 6:6e9a415436e0 | 451 | lcd.clear(); |
| 200923317 | 6:6e9a415436e0 | 452 | lcd.printString(buffer,35,0); |
| 200923317 | 7:13333933413d | 453 | lcd.printString("LVL:??",35,1); |
| 200923317 | 7:13333933413d | 454 | battleStats(); |
| 200923317 | 6:6e9a415436e0 | 455 | lcd.printString(buffer,35,0); |
| 200923317 | 8:e47c48551124 | 456 | //Actual battle taking place |
| 200923317 | 6:6e9a415436e0 | 457 | char HP1[10]; |
| 200923317 | 6:6e9a415436e0 | 458 | char HP2[10]; |
| 200923317 | 8:e47c48551124 | 459 | //Setting HP values |
| 200923317 | 6:6e9a415436e0 | 460 | int HPOT; |
| 200923317 | 6:6e9a415436e0 | 461 | int HPAT; |
| 200923317 | 6:6e9a415436e0 | 462 | int HPO = HPOT = pk.HPO(opponent); |
| 200923317 | 6:6e9a415436e0 | 463 | int HPA = HPAT = pk.HPA(); |
| 200923317 | 6:6e9a415436e0 | 464 | int win = 0; //1 = win, 2 = loss, 3 = draw |
| 200923317 | 6:6e9a415436e0 | 465 | // After HP cycles past 0, The HP counter goes to above 429,000,000 ; |
| 200923317 | 8:e47c48551124 | 466 | //whle loop until one pokemons HP hits 0 |
| 200923317 | 6:6e9a415436e0 | 467 | while ( win == 0 ) { |
| 200923317 | 8:e47c48551124 | 468 | //update HP values |
| 200923317 | 6:6e9a415436e0 | 469 | HPAT = HPAT - pk.OpponentTurn(opponent); |
| 200923317 | 6:6e9a415436e0 | 470 | HPOT = HPOT - pk.YourTurn(opponent); |
| 200923317 | 8:e47c48551124 | 471 | wait(1.8); |
| 200923317 | 8:e47c48551124 | 472 | //player wins |
| 200923317 | 6:6e9a415436e0 | 473 | if( HPOT >> HPO) { |
| 200923317 | 6:6e9a415436e0 | 474 | win = 1; |
| 200923317 | 6:6e9a415436e0 | 475 | lcd.printString("HP:0",2,5); |
| 200923317 | 7:13333933413d | 476 | pk.win(lcd); |
| 200923317 | 8:e47c48551124 | 477 | //wild pokemon wins |
| 200923317 | 6:6e9a415436e0 | 478 | } else if( HPAT >> HPA) { |
| 200923317 | 6:6e9a415436e0 | 479 | win = 2; |
| 200923317 | 6:6e9a415436e0 | 480 | lcd.printString("HP:0",35,1); |
| 200923317 | 8:e47c48551124 | 481 | //both pokemon faint |
| 200923317 | 6:6e9a415436e0 | 482 | } else if( HPOT >> HPO && HPAT >> HPA) { |
| 200923317 | 6:6e9a415436e0 | 483 | win = 3; |
| 200923317 | 6:6e9a415436e0 | 484 | lcd.printString("HP:0",2,5); |
| 200923317 | 6:6e9a415436e0 | 485 | lcd.printString("HP:0",35,1); |
| 200923317 | 8:e47c48551124 | 486 | //game still looping |
| 200923317 | 6:6e9a415436e0 | 487 | } else { |
| 200923317 | 7:13333933413d | 488 | lcd.drawRect(2,40,50,8,FILL_WHITE); |
| 200923317 | 7:13333933413d | 489 | lcd.drawRect(35,8,50,8,FILL_WHITE); |
| 200923317 | 8:e47c48551124 | 490 | //update screen with new HP |
| 200923317 | 6:6e9a415436e0 | 491 | sprintf(HP1, "HP:%u",HPAT); |
| 200923317 | 6:6e9a415436e0 | 492 | sprintf(HP2, "HP:%u",HPOT); |
| 200923317 | 6:6e9a415436e0 | 493 | lcd.printString(HP1,2,5); |
| 200923317 | 6:6e9a415436e0 | 494 | lcd.printString(HP2,35,1); |
| 200923317 | 6:6e9a415436e0 | 495 | lcd.refresh(); |
| 200923317 | 6:6e9a415436e0 | 496 | } |
| 200923317 | 6:6e9a415436e0 | 497 | } |
| 200923317 | 8:e47c48551124 | 498 | //print correct script for win condition from script library |
| 200923317 | 7:13333933413d | 499 | sc.Battle(lcd, win); |
| 200923317 | 6:6e9a415436e0 | 500 | lcd.refresh(); |
| 200923317 | 6:6e9a415436e0 | 501 | wait(2.0); |
| 200923317 | 8:e47c48551124 | 502 | //reset gamepad to counteract button bounce |
| 200923317 | 6:6e9a415436e0 | 503 | pad.init(); |
| 200923317 | 3:b4de529de482 | 504 | } |
| 200923317 | 6:6e9a415436e0 | 505 | |
| 200923317 | 8:e47c48551124 | 506 | /** Draw the Pokemon class |
| 200923317 | 8:e47c48551124 | 507 | * Prints all your pokemons stats to the screen |
| 200923317 | 8:e47c48551124 | 508 | * uses B button to go back to Menu state |
| 200923317 | 8:e47c48551124 | 509 | */ |
| 200923317 | 3:b4de529de482 | 510 | void drawPoke() |
| 200923317 | 3:b4de529de482 | 511 | { |
| 200923317 | 5:add0351183be | 512 | lcd.clear(); |
| 200923317 | 8:e47c48551124 | 513 | //check for B button pressed, loop until. |
| 200923317 | 5:add0351183be | 514 | while(pad.check_event(Gamepad::B_PRESSED) == false) { |
| 200923317 | 8:e47c48551124 | 515 | //get Pokemon stats, copy to buffers to be printed to screen |
| 200923317 | 5:add0351183be | 516 | std::string pkn = pk.Name(); |
| 200923317 | 5:add0351183be | 517 | std::string pkl = pk.Level(); |
| 200923317 | 5:add0351183be | 518 | std::string pkh = pk.HP(); |
| 200923317 | 5:add0351183be | 519 | std::string pkt = pk.Type(); |
| 200923317 | 5:add0351183be | 520 | char a[50]; |
| 200923317 | 5:add0351183be | 521 | char b[50]; |
| 200923317 | 5:add0351183be | 522 | char c[50]; |
| 200923317 | 5:add0351183be | 523 | char d[50]; |
| 200923317 | 5:add0351183be | 524 | strncpy(a, pkn.c_str(), sizeof(a)); |
| 200923317 | 5:add0351183be | 525 | strncpy(b, pkl.c_str(), sizeof(b)); |
| 200923317 | 5:add0351183be | 526 | strncpy(c, pkt.c_str(), sizeof(c)); |
| 200923317 | 5:add0351183be | 527 | strncpy(d, pkh.c_str(), sizeof(d)); |
| 200923317 | 5:add0351183be | 528 | lcd.printString("Pokemon:",2,0); |
| 200923317 | 5:add0351183be | 529 | lcd.printString(a,2,1); |
| 200923317 | 5:add0351183be | 530 | lcd.printString(b,2,2); |
| 200923317 | 5:add0351183be | 531 | lcd.printString(c,2,3); |
| 200923317 | 5:add0351183be | 532 | lcd.printString(d,2,4); |
| 200923317 | 5:add0351183be | 533 | lcd.printString(" B = Back ",24,5); |
| 200923317 | 5:add0351183be | 534 | lcd.refresh(); |
| 200923317 | 5:add0351183be | 535 | } |
| 200923317 | 3:b4de529de482 | 536 | } |
| 200923317 | 5:add0351183be | 537 | |
| 200923317 | 8:e47c48551124 | 538 | /** Draw the settings class |
| 200923317 | 8:e47c48551124 | 539 | * Used to set screen brightness |
| 200923317 | 8:e47c48551124 | 540 | * uses B button to go back to menu |
| 200923317 | 8:e47c48551124 | 541 | */ |
| 200923317 | 3:b4de529de482 | 542 | void settings() |
| 200923317 | 3:b4de529de482 | 543 | { |
| 200923317 | 5:add0351183be | 544 | lcd.clear(); |
| 200923317 | 5:add0351183be | 545 | int bright = 1; |
| 200923317 | 6:6e9a415436e0 | 546 | lcd.printString("SET BRIGHTNESS", 2, 20); |
| 200923317 | 6:6e9a415436e0 | 547 | lcd.printString("B = BACK", 4, 20); |
| 200923317 | 6:6e9a415436e0 | 548 | lcd.refresh(); |
| 200923317 | 8:e47c48551124 | 549 | wait(0.1); |
| 200923317 | 8:e47c48551124 | 550 | //check for B button pressed, loop until. |
| 200923317 | 5:add0351183be | 551 | while(pad.check_event(Gamepad::B_PRESSED) == false) { |
| 200923317 | 8:e47c48551124 | 552 | //get value of potentiometer from gamepad library |
| 200923317 | 5:add0351183be | 553 | bright = pad.read_pot(); |
| 200923317 | 8:e47c48551124 | 554 | //update brightness with float value |
| 200923317 | 5:add0351183be | 555 | float brightness = 0.5*bright; |
| 200923317 | 5:add0351183be | 556 | lcd.setBrightness(brightness); |
| 200923317 | 6:6e9a415436e0 | 557 | lcd.refresh(); |
| 200923317 | 5:add0351183be | 558 | } |
| 200923317 | 3:b4de529de482 | 559 | } |
| 200923317 | 2:a2bb794f830c | 560 |
