![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Adam Baker 201166301
Dependencies: mbed Gamepad N5110
main.cpp@36:6f452777b9ce, 2019-05-05 (annotated)
- Committer:
- adambakerwa
- Date:
- Sun May 05 14:58:20 2019 +0000
- Revision:
- 36:6f452777b9ce
- Parent:
- 34:911d7d05a8d2
- Child:
- 37:23b6312c8315
more menu work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adambakerwa | 1:a3cce487dffe | 1 | #include "mbed.h" |
adambakerwa | 1:a3cce487dffe | 2 | #include "Gamepad.h" |
adambakerwa | 1:a3cce487dffe | 3 | #include "N5110.h" |
adambakerwa | 1:a3cce487dffe | 4 | #include "Runner.h" |
adambakerwa | 6:cef9c2853315 | 5 | #include "Levels.h" |
adambakerwa | 9:ddeafaadf984 | 6 | #include "Animation.h" |
adambakerwa | 25:230e4163cb6b | 7 | #include "BlockheadEngine.h" |
adambakerwa | 36:6f452777b9ce | 8 | #include "Menu.h" |
adambakerwa | 1:a3cce487dffe | 9 | |
adambakerwa | 1:a3cce487dffe | 10 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
adambakerwa | 1:a3cce487dffe | 11 | Gamepad pad; |
adambakerwa | 25:230e4163cb6b | 12 | BlockheadEngine blockhead; |
adambakerwa | 13:35a459a33bbd | 13 | Ticker ticker; |
adambakerwa | 36:6f452777b9ce | 14 | Menu menu; |
adambakerwa | 13:35a459a33bbd | 15 | |
adambakerwa | 13:35a459a33bbd | 16 | volatile int timer_flag = 0; |
adambakerwa | 13:35a459a33bbd | 17 | |
adambakerwa | 13:35a459a33bbd | 18 | void timer_isr(); |
adambakerwa | 31:bf765e3961f1 | 19 | void init(); |
adambakerwa | 36:6f452777b9ce | 20 | |
adambakerwa | 26:3319ddd5111d | 21 | void main_menu(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 22 | int select_menu_item(Gamepad &pad, N5110 &lcd); |
adambakerwa | 36:6f452777b9ce | 23 | void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 31:bf765e3961f1 | 24 | |
adambakerwa | 26:3319ddd5111d | 25 | void menu_playgame(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 26 | void menu_highscore(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 27 | void menu_settings(N5110 &lcd, Gamepad &pad); |
adambakerwa | 36:6f452777b9ce | 28 | void menu_quit(N5110 &lcd, Gamepad &pad); |
adambakerwa | 6:cef9c2853315 | 29 | |
adambakerwa | 31:bf765e3961f1 | 30 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 36:6f452777b9ce | 31 | void print_continue_menu(N5110 &lcd); // |
adambakerwa | 31:bf765e3961f1 | 32 | void menu_continue(N5110 &lcd, Gamepad &pad); |
adambakerwa | 31:bf765e3961f1 | 33 | |
adambakerwa | 31:bf765e3961f1 | 34 | |
adambakerwa | 13:35a459a33bbd | 35 | int main() |
adambakerwa | 13:35a459a33bbd | 36 | { |
adambakerwa | 13:35a459a33bbd | 37 | |
adambakerwa | 18:0a0976faedfb | 38 | int fps = 6; //6 |
adambakerwa | 31:bf765e3961f1 | 39 | init(); |
adambakerwa | 31:bf765e3961f1 | 40 | ticker.attach(&timer_isr,1.0f/fps); |
adambakerwa | 36:6f452777b9ce | 41 | |
adambakerwa | 17:ecbafe932c19 | 42 | lcd.setContrast(0.55); |
adambakerwa | 24:f5dd1f3db693 | 43 | |
adambakerwa | 26:3319ddd5111d | 44 | main_menu(lcd, pad); |
adambakerwa | 13:35a459a33bbd | 45 | |
adambakerwa | 13:35a459a33bbd | 46 | } |
adambakerwa | 13:35a459a33bbd | 47 | |
adambakerwa | 13:35a459a33bbd | 48 | void timer_isr() |
adambakerwa | 13:35a459a33bbd | 49 | { |
adambakerwa | 13:35a459a33bbd | 50 | |
adambakerwa | 36:6f452777b9ce | 51 | timer_flag = 1; // set flag in ISR |
adambakerwa | 1:a3cce487dffe | 52 | } |
adambakerwa | 1:a3cce487dffe | 53 | |
adambakerwa | 31:bf765e3961f1 | 54 | void init() |
adambakerwa | 31:bf765e3961f1 | 55 | { |
adambakerwa | 36:6f452777b9ce | 56 | pad.init(); //intialise Gamepad class variables |
adambakerwa | 36:6f452777b9ce | 57 | lcd.init(); |
adambakerwa | 36:6f452777b9ce | 58 | menu.init(); //intialise N5110 class variables |
adambakerwa | 31:bf765e3961f1 | 59 | } |
adambakerwa | 31:bf765e3961f1 | 60 | |
adambakerwa | 26:3319ddd5111d | 61 | void main_menu(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 62 | { |
adambakerwa | 36:6f452777b9ce | 63 | blockhead.init(); //intialise blockhead engine variables for new game |
adambakerwa | 36:6f452777b9ce | 64 | //intialse menu variables |
adambakerwa | 36:6f452777b9ce | 65 | int input = select_menu_item(pad, lcd); //reads input and prints the main menu |
adambakerwa | 36:6f452777b9ce | 66 | go_to_main_menu_item(lcd, pad, input); //peforms what ever input is chosen |
adambakerwa | 26:3319ddd5111d | 67 | } |
adambakerwa | 26:3319ddd5111d | 68 | |
adambakerwa | 26:3319ddd5111d | 69 | |
adambakerwa | 36:6f452777b9ce | 70 | |
adambakerwa | 26:3319ddd5111d | 71 | |
adambakerwa | 36:6f452777b9ce | 72 | int select_menu_item(Gamepad &pad, N5110 &lcd) |
adambakerwa | 36:6f452777b9ce | 73 | { |
adambakerwa | 36:6f452777b9ce | 74 | int input = 0; |
adambakerwa | 36:6f452777b9ce | 75 | do { //ADD TRICKR AND ANIMATION |
adambakerwa | 36:6f452777b9ce | 76 | input = menu.select_input(pad, lcd); //prints menu and toggles cursor depending on input, |
adambakerwa | 36:6f452777b9ce | 77 | } while (input == 0); //repeats untill an input is chosen |
adambakerwa | 26:3319ddd5111d | 78 | |
adambakerwa | 25:230e4163cb6b | 79 | return input; |
adambakerwa | 25:230e4163cb6b | 80 | } |
adambakerwa | 25:230e4163cb6b | 81 | |
adambakerwa | 36:6f452777b9ce | 82 | void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 26:3319ddd5111d | 83 | { |
adambakerwa | 36:6f452777b9ce | 84 | switch (input) { //depending on input, performes following menu items |
adambakerwa | 26:3319ddd5111d | 85 | case 1: |
adambakerwa | 26:3319ddd5111d | 86 | menu_playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 87 | break; |
adambakerwa | 26:3319ddd5111d | 88 | case 2: |
adambakerwa | 36:6f452777b9ce | 89 | menu_highscore(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 90 | break; |
adambakerwa | 36:6f452777b9ce | 91 | case 3: |
adambakerwa | 36:6f452777b9ce | 92 | menu_settings(lcd, pad); |
adambakerwa | 36:6f452777b9ce | 93 | case 4: |
adambakerwa | 36:6f452777b9ce | 94 | menu_quit(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 95 | default: |
adambakerwa | 26:3319ddd5111d | 96 | exit(1); |
adambakerwa | 26:3319ddd5111d | 97 | break; |
adambakerwa | 26:3319ddd5111d | 98 | } |
adambakerwa | 25:230e4163cb6b | 99 | } |
adambakerwa | 25:230e4163cb6b | 100 | |
adambakerwa | 26:3319ddd5111d | 101 | void menu_playgame(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 102 | { |
adambakerwa | 26:3319ddd5111d | 103 | int gameover = 0; |
adambakerwa | 31:bf765e3961f1 | 104 | |
adambakerwa | 26:3319ddd5111d | 105 | do { |
adambakerwa | 36:6f452777b9ce | 106 | if (timer_flag == true) { //only run when timer flag is true (6fps) |
adambakerwa | 26:3319ddd5111d | 107 | |
adambakerwa | 36:6f452777b9ce | 108 | timer_flag = 0; //if it has, clear the flag |
adambakerwa | 26:3319ddd5111d | 109 | |
adambakerwa | 36:6f452777b9ce | 110 | gameover = blockhead.playgame(lcd, pad); //run game untill gameover |
adambakerwa | 26:3319ddd5111d | 111 | |
adambakerwa | 26:3319ddd5111d | 112 | } else { |
adambakerwa | 26:3319ddd5111d | 113 | |
adambakerwa | 36:6f452777b9ce | 114 | sleep(); //sleep when timer_flag not true in order to conserve energy |
adambakerwa | 26:3319ddd5111d | 115 | |
adambakerwa | 26:3319ddd5111d | 116 | } |
adambakerwa | 26:3319ddd5111d | 117 | } while (gameover == 0); |
adambakerwa | 26:3319ddd5111d | 118 | |
adambakerwa | 36:6f452777b9ce | 119 | menu_continue(lcd, pad); //once gameover occurs, go to continue menu (LETS TRY GET LIVES INVOLVED MAYBE!!!))) |
adambakerwa | 26:3319ddd5111d | 120 | |
adambakerwa | 26:3319ddd5111d | 121 | } |
adambakerwa | 26:3319ddd5111d | 122 | |
adambakerwa | 36:6f452777b9ce | 123 | void menu_highscore(N5110 &lcd, Gamepad &pad) |
adambakerwa | 25:230e4163cb6b | 124 | { |
adambakerwa | 25:230e4163cb6b | 125 | |
adambakerwa | 26:3319ddd5111d | 126 | int goback = 0; |
adambakerwa | 36:6f452777b9ce | 127 | |
adambakerwa | 26:3319ddd5111d | 128 | do { |
adambakerwa | 25:230e4163cb6b | 129 | |
adambakerwa | 26:3319ddd5111d | 130 | if (timer_flag == true) { |
adambakerwa | 26:3319ddd5111d | 131 | |
adambakerwa | 26:3319ddd5111d | 132 | timer_flag = 0; |
adambakerwa | 26:3319ddd5111d | 133 | |
adambakerwa | 36:6f452777b9ce | 134 | int highscore = blockhead.highscore(); |
adambakerwa | 36:6f452777b9ce | 135 | |
adambakerwa | 26:3319ddd5111d | 136 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 137 | |
adambakerwa | 36:6f452777b9ce | 138 | lcd.printString(" Highscore.. ", 0,1); |
adambakerwa | 36:6f452777b9ce | 139 | |
adambakerwa | 36:6f452777b9ce | 140 | char buffer[14]; |
adambakerwa | 36:6f452777b9ce | 141 | sprintf(buffer,"%i", highscore); |
adambakerwa | 36:6f452777b9ce | 142 | lcd.printString(buffer,35,5); |
adambakerwa | 26:3319ddd5111d | 143 | |
adambakerwa | 26:3319ddd5111d | 144 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 145 | |
adambakerwa | 29:b2f63d8c1268 | 146 | if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 147 | goback = 1; |
adambakerwa | 26:3319ddd5111d | 148 | } |
adambakerwa | 26:3319ddd5111d | 149 | |
adambakerwa | 26:3319ddd5111d | 150 | } else { |
adambakerwa | 26:3319ddd5111d | 151 | |
adambakerwa | 26:3319ddd5111d | 152 | sleep(); |
adambakerwa | 26:3319ddd5111d | 153 | |
adambakerwa | 26:3319ddd5111d | 154 | } |
adambakerwa | 26:3319ddd5111d | 155 | } while (goback == 0); |
adambakerwa | 36:6f452777b9ce | 156 | |
adambakerwa | 36:6f452777b9ce | 157 | main_menu(lcd, pad); |
adambakerwa | 36:6f452777b9ce | 158 | } |
adambakerwa | 26:3319ddd5111d | 159 | |
adambakerwa | 36:6f452777b9ce | 160 | void menu_settings(N5110 &lcd, Gamepad &pad) |
adambakerwa | 36:6f452777b9ce | 161 | { |
adambakerwa | 36:6f452777b9ce | 162 | |
adambakerwa | 36:6f452777b9ce | 163 | int goback = 0; |
adambakerwa | 36:6f452777b9ce | 164 | |
adambakerwa | 36:6f452777b9ce | 165 | do { |
adambakerwa | 36:6f452777b9ce | 166 | |
adambakerwa | 36:6f452777b9ce | 167 | if (timer_flag == true) { |
adambakerwa | 36:6f452777b9ce | 168 | |
adambakerwa | 36:6f452777b9ce | 169 | timer_flag = 0; |
adambakerwa | 36:6f452777b9ce | 170 | |
adambakerwa | 36:6f452777b9ce | 171 | lcd.clear(); |
adambakerwa | 36:6f452777b9ce | 172 | |
adambakerwa | 36:6f452777b9ce | 173 | lcd.printString(" settings ", 0,1); |
adambakerwa | 36:6f452777b9ce | 174 | |
adambakerwa | 36:6f452777b9ce | 175 | lcd.refresh(); |
adambakerwa | 36:6f452777b9ce | 176 | |
adambakerwa | 36:6f452777b9ce | 177 | if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 36:6f452777b9ce | 178 | goback = 1; |
adambakerwa | 36:6f452777b9ce | 179 | } |
adambakerwa | 36:6f452777b9ce | 180 | |
adambakerwa | 36:6f452777b9ce | 181 | } else { |
adambakerwa | 36:6f452777b9ce | 182 | |
adambakerwa | 36:6f452777b9ce | 183 | sleep(); |
adambakerwa | 36:6f452777b9ce | 184 | |
adambakerwa | 36:6f452777b9ce | 185 | } |
adambakerwa | 36:6f452777b9ce | 186 | } while (goback == 0); |
adambakerwa | 26:3319ddd5111d | 187 | main_menu(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 188 | } |
adambakerwa | 25:230e4163cb6b | 189 | |
adambakerwa | 36:6f452777b9ce | 190 | void menu_quit(N5110 &lcd, Gamepad &pad) |
adambakerwa | 36:6f452777b9ce | 191 | { |
adambakerwa | 36:6f452777b9ce | 192 | lcd.turnOff(); |
adambakerwa | 36:6f452777b9ce | 193 | sleep(); |
adambakerwa | 36:6f452777b9ce | 194 | |
adambakerwa | 36:6f452777b9ce | 195 | } |
adambakerwa | 36:6f452777b9ce | 196 | |
adambakerwa | 25:230e4163cb6b | 197 | |
adambakerwa | 31:bf765e3961f1 | 198 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 31:bf765e3961f1 | 199 | { |
adambakerwa | 31:bf765e3961f1 | 200 | switch (input) { |
adambakerwa | 31:bf765e3961f1 | 201 | case 1: |
adambakerwa | 31:bf765e3961f1 | 202 | menu_playgame(lcd, pad); |
adambakerwa | 31:bf765e3961f1 | 203 | break; |
adambakerwa | 31:bf765e3961f1 | 204 | case 2: |
adambakerwa | 31:bf765e3961f1 | 205 | main_menu(lcd, pad); |
adambakerwa | 31:bf765e3961f1 | 206 | break; |
adambakerwa | 31:bf765e3961f1 | 207 | default: |
adambakerwa | 31:bf765e3961f1 | 208 | exit(1); |
adambakerwa | 31:bf765e3961f1 | 209 | break; |
adambakerwa | 31:bf765e3961f1 | 210 | } |
adambakerwa | 31:bf765e3961f1 | 211 | } |
adambakerwa | 31:bf765e3961f1 | 212 | |
adambakerwa | 31:bf765e3961f1 | 213 | void print_menu_continue(N5110 &lcd) |
adambakerwa | 31:bf765e3961f1 | 214 | { |
adambakerwa | 31:bf765e3961f1 | 215 | lcd.clear(); |
adambakerwa | 31:bf765e3961f1 | 216 | |
adambakerwa | 31:bf765e3961f1 | 217 | lcd.printString("Continue??",0,0); |
adambakerwa | 31:bf765e3961f1 | 218 | lcd.printString("A for hell yeh",0,1); |
adambakerwa | 31:bf765e3961f1 | 219 | lcd.printString("B for back",0,2); |
adambakerwa | 31:bf765e3961f1 | 220 | lcd.refresh(); |
adambakerwa | 31:bf765e3961f1 | 221 | |
adambakerwa | 31:bf765e3961f1 | 222 | } |
adambakerwa | 31:bf765e3961f1 | 223 | |
adambakerwa | 31:bf765e3961f1 | 224 | void menu_continue(N5110 &lcd, Gamepad &pad) |
adambakerwa | 31:bf765e3961f1 | 225 | { |
adambakerwa | 31:bf765e3961f1 | 226 | blockhead.continueInit(); |
adambakerwa | 36:6f452777b9ce | 227 | |
adambakerwa | 31:bf765e3961f1 | 228 | print_menu_continue(lcd); |
adambakerwa | 36:6f452777b9ce | 229 | |
adambakerwa | 36:6f452777b9ce | 230 | int input = 0; |
adambakerwa | 36:6f452777b9ce | 231 | do{ |
adambakerwa | 36:6f452777b9ce | 232 | |
adambakerwa | 36:6f452777b9ce | 233 | if (pad.check_event(Gamepad::A_PRESSED)) { |
adambakerwa | 36:6f452777b9ce | 234 | input = 1; |
adambakerwa | 36:6f452777b9ce | 235 | } |
adambakerwa | 36:6f452777b9ce | 236 | |
adambakerwa | 36:6f452777b9ce | 237 | if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 36:6f452777b9ce | 238 | input = 2; |
adambakerwa | 36:6f452777b9ce | 239 | } |
adambakerwa | 36:6f452777b9ce | 240 | |
adambakerwa | 36:6f452777b9ce | 241 | } while (input ==0); |
adambakerwa | 36:6f452777b9ce | 242 | select_continue_menu_item(lcd, pad, input); |
adambakerwa | 31:bf765e3961f1 | 243 | } |
adambakerwa | 31:bf765e3961f1 | 244 | |
adambakerwa | 1:a3cce487dffe | 245 | |
adambakerwa | 13:35a459a33bbd | 246 | //rtos |
adambakerwa | 24:f5dd1f3db693 | 247 | /** |
adambakerwa | 24:f5dd1f3db693 | 248 | char buffer[14]; |
adambakerwa | 24:f5dd1f3db693 | 249 | sprintf(buffer,"%i", gameover); |
adambakerwa | 24:f5dd1f3db693 | 250 | lcd.printString(buffer,0,0); |
adambakerwa | 24:f5dd1f3db693 | 251 | */ |
adambakerwa | 26:3319ddd5111d | 252 | //NEED TO RESET POS CORDS EACH NEW LEVEL TO PREVENT UNNECERSARRY GAME OVER (have fun x) |