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@2:a2bb794f830c, 2017-04-20 (annotated)
- Committer:
- 200923317
- Date:
- Thu Apr 20 17:27:48 2017 +0000
- Revision:
- 2:a2bb794f830c
- Parent:
- 1:af881f58c4f9
- Child:
- 3:b4de529de482
partner selection done, sprite bitmaps need drawing;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 200923317 | 0:cd3f75767e71 | 1 | #include "mbed.h" |
| 200923317 | 0:cd3f75767e71 | 2 | #include "Gamepad.h" |
| 200923317 | 0:cd3f75767e71 | 3 | #include "N5110.h" |
| 200923317 | 0:cd3f75767e71 | 4 | #include "Pokemon.h" |
| 200923317 | 1:af881f58c4f9 | 5 | #include "Sprites.h" |
| 200923317 | 0:cd3f75767e71 | 6 | |
| 200923317 | 0:cd3f75767e71 | 7 | //rewritten code to implement new/better ideas. part of the code is same as before but lots of changes were needed. |
| 200923317 | 0:cd3f75767e71 | 8 | |
| 200923317 | 0:cd3f75767e71 | 9 | //-------------------------------- objects ------------------------------------- |
| 200923317 | 0:cd3f75767e71 | 10 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| 200923317 | 0:cd3f75767e71 | 11 | Gamepad pad; |
| 200923317 | 0:cd3f75767e71 | 12 | Pokemon pk; |
| 200923317 | 2:a2bb794f830c | 13 | Sprites sp; |
| 200923317 | 0:cd3f75767e71 | 14 | //--------------------------- Structs/Typedefs --------------------------------- |
| 200923317 | 0:cd3f75767e71 | 15 | struct joyInput { |
| 200923317 | 0:cd3f75767e71 | 16 | Direction d; //direction of joystick to navigate menu's |
| 200923317 | 0:cd3f75767e71 | 17 | }; |
| 200923317 | 0:cd3f75767e71 | 18 | |
| 200923317 | 0:cd3f75767e71 | 19 | typedef enum State {START, PARTNER, MENU, FIGHT, POKEMON, SETTINGS} Gamepage; |
| 200923317 | 0:cd3f75767e71 | 20 | //assigning names to different states |
| 200923317 | 0:cd3f75767e71 | 21 | |
| 200923317 | 0:cd3f75767e71 | 22 | void init(); |
| 200923317 | 0:cd3f75767e71 | 23 | void drawStart(); |
| 200923317 | 2:a2bb794f830c | 24 | void select(int x, int y, int L); |
| 200923317 | 2:a2bb794f830c | 25 | void deselect(int _x, int _y, int _L); |
| 200923317 | 2:a2bb794f830c | 26 | void balls(); |
| 200923317 | 2:a2bb794f830c | 27 | float drawPartner(); |
| 200923317 | 2:a2bb794f830c | 28 | float partnerChoice(int choice); |
| 200923317 | 2:a2bb794f830c | 29 | void choice(int p); |
| 200923317 | 0:cd3f75767e71 | 30 | float drawMenu(); |
| 200923317 | 0:cd3f75767e71 | 31 | void drawFight(); |
| 200923317 | 0:cd3f75767e71 | 32 | void drawPoke(); |
| 200923317 | 0:cd3f75767e71 | 33 | void menu(); |
| 200923317 | 1:af881f58c4f9 | 34 | void settings(); |
| 200923317 | 1:af881f58c4f9 | 35 | |
| 200923317 | 1:af881f58c4f9 | 36 | //------------------------------------------------------------------------------ |
| 200923317 | 1:af881f58c4f9 | 37 | |
| 200923317 | 1:af881f58c4f9 | 38 | int main() |
| 200923317 | 1:af881f58c4f9 | 39 | { |
| 200923317 | 1:af881f58c4f9 | 40 | init(); |
| 200923317 | 1:af881f58c4f9 | 41 | drawStart(); |
| 200923317 | 1:af881f58c4f9 | 42 | Gamepage state = START; |
| 200923317 | 2:a2bb794f830c | 43 | //-----------------------GAME LOOP START------------------------------- |
| 200923317 | 2:a2bb794f830c | 44 | while(1) { |
| 200923317 | 2:a2bb794f830c | 45 | if (state == START) { |
| 200923317 | 2:a2bb794f830c | 46 | state = PARTNER; |
| 200923317 | 2:a2bb794f830c | 47 | |
| 200923317 | 2:a2bb794f830c | 48 | } else if (state == PARTNER) { |
| 200923317 | 2:a2bb794f830c | 49 | int partner = drawPartner(); |
| 200923317 | 2:a2bb794f830c | 50 | int correct = partnerChoice(partner); |
| 200923317 | 2:a2bb794f830c | 51 | if( correct == 1) { |
| 200923317 | 2:a2bb794f830c | 52 | state = PARTNER; |
| 200923317 | 2:a2bb794f830c | 53 | } else if(correct == 0) { |
| 200923317 | 2:a2bb794f830c | 54 | choice(partner); |
| 200923317 | 2:a2bb794f830c | 55 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 56 | state = MENU; |
| 200923317 | 2:a2bb794f830c | 57 | } |
| 200923317 | 2:a2bb794f830c | 58 | |
| 200923317 | 2:a2bb794f830c | 59 | } else if (state == FIGHT) { |
| 200923317 | 2:a2bb794f830c | 60 | state = MENU; |
| 200923317 | 2:a2bb794f830c | 61 | |
| 200923317 | 2:a2bb794f830c | 62 | } else if (state == SETTINGS) { |
| 200923317 | 2:a2bb794f830c | 63 | state = MENU; |
| 200923317 | 2:a2bb794f830c | 64 | |
| 200923317 | 2:a2bb794f830c | 65 | } else if (state == MENU) { |
| 200923317 | 2:a2bb794f830c | 66 | wait(1.0); |
| 200923317 | 2:a2bb794f830c | 67 | int box = drawMenu(); |
| 200923317 | 2:a2bb794f830c | 68 | if (box == 0) { |
| 200923317 | 2:a2bb794f830c | 69 | state = FIGHT; |
| 200923317 | 2:a2bb794f830c | 70 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 71 | lcd.printString("FIGHT",8,2); |
| 200923317 | 2:a2bb794f830c | 72 | } else if (box ==1) { |
| 200923317 | 2:a2bb794f830c | 73 | state = POKEMON; |
| 200923317 | 2:a2bb794f830c | 74 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 75 | lcd.printString("POKEMON",20,4); |
| 200923317 | 2:a2bb794f830c | 76 | } else if (box ==2) { |
| 200923317 | 2:a2bb794f830c | 77 | state = SETTINGS; |
| 200923317 | 2:a2bb794f830c | 78 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 79 | lcd.printString("SETTINGS",50,2); |
| 200923317 | 2:a2bb794f830c | 80 | } |
| 200923317 | 2:a2bb794f830c | 81 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 82 | wait(2.0); |
| 200923317 | 2:a2bb794f830c | 83 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 84 | |
| 200923317 | 2:a2bb794f830c | 85 | } else if (state == POKEMON) { |
| 200923317 | 2:a2bb794f830c | 86 | state = MENU; |
| 200923317 | 2:a2bb794f830c | 87 | drawPoke(); |
| 200923317 | 2:a2bb794f830c | 88 | } |
| 200923317 | 2:a2bb794f830c | 89 | } |
| 200923317 | 2:a2bb794f830c | 90 | } |
| 200923317 | 2:a2bb794f830c | 91 | |
| 200923317 | 2:a2bb794f830c | 92 | |
| 200923317 | 1:af881f58c4f9 | 93 | |
| 200923317 | 1:af881f58c4f9 | 94 | void init() |
| 200923317 | 1:af881f58c4f9 | 95 | { |
| 200923317 | 1:af881f58c4f9 | 96 | pad.init(); |
| 200923317 | 1:af881f58c4f9 | 97 | lcd.init(); |
| 200923317 | 1:af881f58c4f9 | 98 | lcd.setBrightness(1.0); |
| 200923317 | 2:a2bb794f830c | 99 | } |
| 200923317 | 2:a2bb794f830c | 100 | |
| 200923317 | 2:a2bb794f830c | 101 | void drawStart() |
| 200923317 | 2:a2bb794f830c | 102 | { |
| 200923317 | 2:a2bb794f830c | 103 | |
| 200923317 | 2:a2bb794f830c | 104 | lcd.printString(" Welcome to ",0,0); |
| 200923317 | 2:a2bb794f830c | 105 | lcd.printString(" the World of ",0,1); |
| 200923317 | 2:a2bb794f830c | 106 | lcd.printString(" Pokemon ",0,2); |
| 200923317 | 2:a2bb794f830c | 107 | lcd.printString(" Press Start ",0,4); |
| 200923317 | 2:a2bb794f830c | 108 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 109 | |
| 200923317 | 2:a2bb794f830c | 110 | while( pad.check_event(Gamepad::START_PRESSED) == false) { |
| 200923317 | 2:a2bb794f830c | 111 | pad.led(1,1); |
| 200923317 | 2:a2bb794f830c | 112 | pad.led(6,1); |
| 200923317 | 2:a2bb794f830c | 113 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 114 | pad.led(2,1); |
| 200923317 | 2:a2bb794f830c | 115 | pad.led(5,1); |
| 200923317 | 2:a2bb794f830c | 116 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 117 | pad.leds_on(); |
| 200923317 | 2:a2bb794f830c | 118 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 119 | pad.led(1,0); |
| 200923317 | 2:a2bb794f830c | 120 | pad.led(6,0); |
| 200923317 | 2:a2bb794f830c | 121 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 122 | pad.led(2,0); |
| 200923317 | 2:a2bb794f830c | 123 | pad.led(5,0); |
| 200923317 | 2:a2bb794f830c | 124 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 125 | pad.leds_off(); |
| 200923317 | 2:a2bb794f830c | 126 | wait(0.5); |
| 200923317 | 2:a2bb794f830c | 127 | } |
| 200923317 | 2:a2bb794f830c | 128 | wait(1.0); |
| 200923317 | 2:a2bb794f830c | 129 | } |
| 200923317 | 2:a2bb794f830c | 130 | |
| 200923317 | 2:a2bb794f830c | 131 | void select(int x,int y, int L) |
| 200923317 | 2:a2bb794f830c | 132 | { |
| 200923317 | 2:a2bb794f830c | 133 | lcd.drawRect(x,y,L,L,FILL_TRANSPARENT); |
| 200923317 | 2:a2bb794f830c | 134 | } |
| 200923317 | 2:a2bb794f830c | 135 | |
| 200923317 | 2:a2bb794f830c | 136 | void select(int _x,int _y, int _L) |
| 200923317 | 2:a2bb794f830c | 137 | { |
| 200923317 | 2:a2bb794f830c | 138 | lcd.drawRect(_x,_y,_L,_L,2); |
| 200923317 | 2:a2bb794f830c | 139 | } |
| 200923317 | 2:a2bb794f830c | 140 | |
| 200923317 | 2:a2bb794f830c | 141 | void balls() |
| 200923317 | 2:a2bb794f830c | 142 | { |
| 200923317 | 2:a2bb794f830c | 143 | sp.ball(11,18); |
| 200923317 | 2:a2bb794f830c | 144 | sp.ball(35,18); |
| 200923317 | 2:a2bb794f830c | 145 | sp.ball(59,18); |
| 200923317 | 2:a2bb794f830c | 146 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 147 | } |
| 200923317 | 2:a2bb794f830c | 148 | |
| 200923317 | 2:a2bb794f830c | 149 | float drawPartner() |
| 200923317 | 2:a2bb794f830c | 150 | { |
| 200923317 | 2:a2bb794f830c | 151 | select(9,16,15); |
| 200923317 | 2:a2bb794f830c | 152 | int offset = 0; |
| 200923317 | 2:a2bb794f830c | 153 | int _d = pad.get_direction(); |
| 200923317 | 2:a2bb794f830c | 154 | while(pad.check_event(Gamepad::A_PRESSED) == false) { |
| 200923317 | 2:a2bb794f830c | 155 | if(_d == E && offset <= 1) { |
| 200923317 | 2:a2bb794f830c | 156 | offset ++; |
| 200923317 | 2:a2bb794f830c | 157 | } else if(_d == W && offset >=1) { |
| 200923317 | 2:a2bb794f830c | 158 | offset --; |
| 200923317 | 2:a2bb794f830c | 159 | } |
| 200923317 | 2:a2bb794f830c | 160 | if(offset == 0) { |
| 200923317 | 2:a2bb794f830c | 161 | deselect(57,16,15); |
| 200923317 | 2:a2bb794f830c | 162 | deselect(33,16,15); |
| 200923317 | 2:a2bb794f830c | 163 | select(9,16,15); |
| 200923317 | 2:a2bb794f830c | 164 | balls(); |
| 200923317 | 2:a2bb794f830c | 165 | } else if(offset == 1) { |
| 200923317 | 2:a2bb794f830c | 166 | deselect(57,16,15); |
| 200923317 | 2:a2bb794f830c | 167 | deselect(9,16,15); |
| 200923317 | 2:a2bb794f830c | 168 | select(33,16,15); |
| 200923317 | 2:a2bb794f830c | 169 | balls(); |
| 200923317 | 2:a2bb794f830c | 170 | } else if(offset == 2) { |
| 200923317 | 2:a2bb794f830c | 171 | deselect(9,16,15); |
| 200923317 | 2:a2bb794f830c | 172 | deselect(33,16,15); |
| 200923317 | 2:a2bb794f830c | 173 | select(57,16,15); |
| 200923317 | 2:a2bb794f830c | 174 | balls(); |
| 200923317 | 2:a2bb794f830c | 175 | } |
| 200923317 | 2:a2bb794f830c | 176 | } |
| 200923317 | 2:a2bb794f830c | 177 | return offset; |
| 200923317 | 2:a2bb794f830c | 178 | } |
| 200923317 | 2:a2bb794f830c | 179 | |
| 200923317 | 2:a2bb794f830c | 180 | float partnerChoice(int choice) |
| 200923317 | 2:a2bb794f830c | 181 | { |
| 200923317 | 2:a2bb794f830c | 182 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 183 | int _d = pad.get_direction(); |
| 200923317 | 2:a2bb794f830c | 184 | if(choice == 0) { |
| 200923317 | 2:a2bb794f830c | 185 | sp.bulbasaur(16,0); |
| 200923317 | 2:a2bb794f830c | 186 | } else if(choice == 1) { |
| 200923317 | 2:a2bb794f830c | 187 | sp.charmander(16,0); |
| 200923317 | 2:a2bb794f830c | 188 | } else if(choice == 2) { |
| 200923317 | 2:a2bb794f830c | 189 | sp.squirtle(16,0); |
| 200923317 | 2:a2bb794f830c | 190 | } |
| 200923317 | 2:a2bb794f830c | 191 | lcd.printString("Are you",0,2); |
| 200923317 | 2:a2bb794f830c | 192 | lcd.printString(" sure??",0,3); |
| 200923317 | 2:a2bb794f830c | 193 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 194 | while(pad.check_event(Gamepad::A_PRESSED) == false { |
| 200923317 | 2:a2bb794f830c | 195 | lcd.printString("Yes",65,1); |
| 200923317 | 2:a2bb794f830c | 196 | lcd.printString("No",65,5); |
| 200923317 | 2:a2bb794f830c | 197 | select(62,6,8); |
| 200923317 | 2:a2bb794f830c | 198 | lcd.refresh(); |
| 200923317 | 2:a2bb794f830c | 199 | int offset1 = 0; |
| 200923317 | 2:a2bb794f830c | 200 | if(offset1 == 0 && _d == S) { |
| 200923317 | 2:a2bb794f830c | 201 | deselect(62,6,8); |
| 200923317 | 2:a2bb794f830c | 202 | select(62,6,8); |
| 200923317 | 2:a2bb794f830c | 203 | } else if(offset1 == 1 && _d ==N) { |
| 200923317 | 2:a2bb794f830c | 204 | deselect(62,38,8); |
| 200923317 | 2:a2bb794f830c | 205 | select(62,38,8); |
| 200923317 | 2:a2bb794f830c | 206 | } |
| 200923317 | 2:a2bb794f830c | 207 | } |
| 200923317 | 2:a2bb794f830c | 208 | return offset1; |
| 200923317 | 2:a2bb794f830c | 209 | } |
| 200923317 | 2:a2bb794f830c | 210 | |
| 200923317 | 2:a2bb794f830c | 211 | void choice(int p) |
| 200923317 | 2:a2bb794f830c | 212 | { |
| 200923317 | 2:a2bb794f830c | 213 | lcd.clear(); |
| 200923317 | 2:a2bb794f830c | 214 | if (p == 0) { |
| 200923317 | 2:a2bb794f830c | 215 | pk.setType(Grass); |
| 200923317 | 2:a2bb794f830c | 216 | lcd.printString("You Chose",16,2); |
| 200923317 | 2:a2bb794f830c | 217 | lcd.printString("Bulbasaur",15,3); |
| 200923317 | 2:a2bb794f830c | 218 | } else if (p == 1) { |
| 200923317 | 2:a2bb794f830c | 219 | pk.setType(Fire); |
| 200923317 | 2:a2bb794f830c | 220 | lcd.printString("You Chose",16,2); |
| 200923317 | 2:a2bb794f830c | 221 | lcd.printString("Charmander",15,3); |
| 200923317 | 2:a2bb794f830c | 222 | } else if (p == 2) { |
| 200923317 | 2:a2bb794f830c | 223 | pk.setType(Water); |
| 200923317 | 2:a2bb794f830c | 224 | lcd.printString("You Chose ",16,2); |
| 200923317 | 2:a2bb794f830c | 225 | lcd.printString("Squirtle",15,3); |
| 200923317 | 2:a2bb794f830c | 226 | } |
| 200923317 | 2:a2bb794f830c | 227 | } |
| 200923317 | 2:a2bb794f830c | 228 | |
| 200923317 | 2:a2bb794f830c | 229 | |
| 200923317 | 2:a2bb794f830c | 230 | |
| 200923317 | 2:a2bb794f830c | 231 |
