James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 21:18:07 2019 +0000
Revision:
127:5994f1a186e0
Parent:
124:d635e3154bf3
Child:
128:b57930585692
joystick menu;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 ///////// pre-processor directives ////////
jamesheavey 117:4eedd15f2c3d 2
jamesheavey 0:7d4d2023ed9c 3 #include "mbed.h"
jamesheavey 0:7d4d2023ed9c 4 #include "Gamepad.h"
jamesheavey 0:7d4d2023ed9c 5 #include "N5110.h"
jamesheavey 27:1b5038b0a7a2 6 #include "BreakoutEngine.h"
jamesheavey 3:5719d0fe94ed 7 #include "Bitmap.h"
jamesheavey 8:1ab6d90c4d60 8 #include "Sprites.h"
jamesheavey 101:0df767523dbd 9 #include "SDFileSystem.h"
jamesheavey 117:4eedd15f2c3d 10
jamesheavey 124:d635e3154bf3 11 #ifdef WITH_TESTING
jamesheavey 124:d635e3154bf3 12 # include "tests.h"
jamesheavey 124:d635e3154bf3 13 #endif
jamesheavey 124:d635e3154bf3 14
jamesheavey 0:7d4d2023ed9c 15 /////////////// structs /////////////////
jamesheavey 79:70510b0102af 16
jamesheavey 0:7d4d2023ed9c 17 struct UserInput {
jamesheavey 0:7d4d2023ed9c 18 Direction d;
jamesheavey 0:7d4d2023ed9c 19 float mag;
jamesheavey 0:7d4d2023ed9c 20 };
jamesheavey 96:c30e110c0a93 21
jamesheavey 0:7d4d2023ed9c 22 /////////////// objects ///////////////
jamesheavey 79:70510b0102af 23
jamesheavey 0:7d4d2023ed9c 24 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
jamesheavey 0:7d4d2023ed9c 25 Gamepad pad;
jamesheavey 27:1b5038b0a7a2 26 BreakoutEngine breakout;
jamesheavey 101:0df767523dbd 27 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
jamesheavey 101:0df767523dbd 28 Serial serial(USBTX, USBRX); // for PC debug
jamesheavey 0:7d4d2023ed9c 29
jamesheavey 0:7d4d2023ed9c 30 ///////////// prototypes ///////////////
jamesheavey 79:70510b0102af 31
jamesheavey 0:7d4d2023ed9c 32 void init();
jamesheavey 0:7d4d2023ed9c 33 void update_game(UserInput input);
jamesheavey 0:7d4d2023ed9c 34 void render();
jamesheavey 53:8e3da0b58fe9 35 void main_menu();
jamesheavey 51:9d5c10a88b22 36 void settings();
jamesheavey 50:3da51f34e253 37 void how_to_play();
jamesheavey 79:70510b0102af 38 void loss_screen();
jamesheavey 58:a159cd976aca 39 void victory_screen();
jamesheavey 49:31eb7807dbd3 40 void title_screen();
jamesheavey 106:a2957bab0c16 41 void main_game();
jamesheavey 58:a159cd976aca 42 void flash_screen(N5110 &lcd);
jamesheavey 59:fdc05d5778a6 43 void countdown();
jamesheavey 99:d8f1570faa05 44 void save_hi_score(int hi_score);
jamesheavey 98:2ce2c666266b 45 int get_hi_score();
jamesheavey 102:4946a6b47c78 46 bool compare_to_hi_score(int score);
jamesheavey 103:f9f69944a850 47 void print_hi_score(int col,int row);
jamesheavey 120:33dabf094a6d 48 void flash_hi_score_screen();
jamesheavey 117:4eedd15f2c3d 49 void reset_victory(int score, int bonus);
jamesheavey 117:4eedd15f2c3d 50 void reset_loss();
jamesheavey 47:1d1a827be81b 51
jamesheavey 95:09550aeca8b9 52 Bitmap breakwhite(breakwhite_data, 48, 84); // assign the title screen sprites
jamesheavey 95:09550aeca8b9 53 Bitmap breakblack(breakblack_data, 48, 84);
jamesheavey 95:09550aeca8b9 54 Bitmap arrowup(arrowup_data, 5, 7); // assign the arrow up sprite data
jamesheavey 95:09550aeca8b9 55 Bitmap arrowdown(arrowdown_data, 5, 7); // assign the arrow down sprite data
jamesheavey 97:6c76526b5298 56 Bitmap three(three_data, 48, 84); // assign the 3 sprite data
jamesheavey 97:6c76526b5298 57 Bitmap two(two_data, 48, 84); // assign the 2 sprite data
jamesheavey 97:6c76526b5298 58 Bitmap one(one_data, 48, 84); // assign the 1 sprite data
jamesheavey 95:09550aeca8b9 59
jamesheavey 123:39740c246fc2 60 bool tilt = false;
jamesheavey 72:7254d2a8a1cd 61 float sens = 0.5; // default sens
jamesheavey 106:a2957bab0c16 62 int number_of_frames = 0; // tracks the number of frames passed to use for bonus score
jamesheavey 0:7d4d2023ed9c 63
jamesheavey 0:7d4d2023ed9c 64 ///////////// functions ////////////////
jamesheavey 79:70510b0102af 65
jamesheavey 0:7d4d2023ed9c 66 int main()
jamesheavey 91:c01a736fb0d9 67 {
jamesheavey 124:d635e3154bf3 68
jamesheavey 124:d635e3154bf3 69 #ifdef WITH_TESTING
jamesheavey 124:d635e3154bf3 70 int number_of_failures = run_all_tests();
jamesheavey 124:d635e3154bf3 71
jamesheavey 124:d635e3154bf3 72 if(number_of_failures > 0) return number_of_failures;
jamesheavey 124:d635e3154bf3 73 #endif
jamesheavey 124:d635e3154bf3 74
jamesheavey 58:a159cd976aca 75 init(); // initialise the gamepad
jamesheavey 58:a159cd976aca 76 title_screen(); // run the title screen
jamesheavey 0:7d4d2023ed9c 77 }
jamesheavey 0:7d4d2023ed9c 78
jamesheavey 121:12acab75db8f 79 void init() // initialies all classes and libraries
jamesheavey 0:7d4d2023ed9c 80 {
jamesheavey 91:c01a736fb0d9 81 // need to initialise LCD and Gamepad
jamesheavey 0:7d4d2023ed9c 82 lcd.init();
jamesheavey 0:7d4d2023ed9c 83 pad.init();
jamesheavey 72:7254d2a8a1cd 84 breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); // init the objects
jamesheavey 0:7d4d2023ed9c 85 }
jamesheavey 0:7d4d2023ed9c 86
jamesheavey 121:12acab75db8f 87 void render() // draws each frame on the LCD
jamesheavey 0:7d4d2023ed9c 88 {
jamesheavey 0:7d4d2023ed9c 89 // clear screen, re-draw and refresh
jamesheavey 91:c01a736fb0d9 90 lcd.clear();
jamesheavey 26:25eea19c5fbe 91 breakout.draw(lcd);
jamesheavey 0:7d4d2023ed9c 92 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 93 }
jamesheavey 0:7d4d2023ed9c 94
jamesheavey 123:39740c246fc2 95 void title_screen()
jamesheavey 91:c01a736fb0d9 96 {
jamesheavey 58:a159cd976aca 97 tilt = false; // reset the tilt variable so that on start, joystick is default
jamesheavey 109:5ba766522df0 98 breakout.set_mult_zero();
jamesheavey 58:a159cd976aca 99 lcd.clear();
jamesheavey 123:39740c246fc2 100
jamesheavey 106:a2957bab0c16 101 lcd.setBrightness(1); // stops the game from dimming (hardware bug?)
jamesheavey 91:c01a736fb0d9 102
jamesheavey 58:a159cd976aca 103 breakblack.render(lcd, 0, 0); // render the first frame
jamesheavey 58:a159cd976aca 104 lcd.refresh();
jamesheavey 58:a159cd976aca 105 pad.leds_off();
jamesheavey 58:a159cd976aca 106 wait(0.5);
jamesheavey 91:c01a736fb0d9 107
jamesheavey 58:a159cd976aca 108 while (pad.check_event(Gamepad::START_PRESSED) == false) { // while start is not pressed alternate sprites
jamesheavey 91:c01a736fb0d9 109
jamesheavey 58:a159cd976aca 110 lcd.clear();
jamesheavey 91:c01a736fb0d9 111
jamesheavey 58:a159cd976aca 112 breakwhite.render(lcd, 0, 0);
jamesheavey 58:a159cd976aca 113 lcd.refresh();
jamesheavey 58:a159cd976aca 114 pad.leds_on();
jamesheavey 58:a159cd976aca 115 wait(0.5);
jamesheavey 91:c01a736fb0d9 116
jamesheavey 58:a159cd976aca 117 lcd.clear();
jamesheavey 91:c01a736fb0d9 118
jamesheavey 58:a159cd976aca 119 breakblack.render(lcd, 0, 0);
jamesheavey 58:a159cd976aca 120 lcd.refresh();
jamesheavey 58:a159cd976aca 121 pad.leds_off();
jamesheavey 58:a159cd976aca 122 wait(0.5);
jamesheavey 58:a159cd976aca 123 }
jamesheavey 123:39740c246fc2 124
jamesheavey 58:a159cd976aca 125 pad.tone(750.0,0.3);
jamesheavey 58:a159cd976aca 126 wait(0.2);
jamesheavey 58:a159cd976aca 127 main_menu(); // load main menu
jamesheavey 58:a159cd976aca 128 }
jamesheavey 58:a159cd976aca 129
jamesheavey 58:a159cd976aca 130
jamesheavey 91:c01a736fb0d9 131 void main_menu()
jamesheavey 123:39740c246fc2 132 {
jamesheavey 121:12acab75db8f 133 bool menu_toggle = false; // added to delay the while loop condition toggle to prevent button bounce from causing the game to start immediately
jamesheavey 123:39740c246fc2 134
jamesheavey 50:3da51f34e253 135 lcd.clear();
jamesheavey 99:d8f1570faa05 136 lcd.printString(" START ",0,1); // start game with default as joystick
jamesheavey 73:54ab819609bc 137 lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt
jamesheavey 107:1b21b028d764 138 lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to interact with the game
jamesheavey 123:39740c246fc2 139 lcd.printString("HI-SCORE: ",0,5);
jamesheavey 106:a2957bab0c16 140 print_hi_score(55,5);
jamesheavey 91:c01a736fb0d9 141 lcd.refresh();
jamesheavey 107:1b21b028d764 142 wait(0.4); // load initial frame (delay helps button bounce)
jamesheavey 91:c01a736fb0d9 143
jamesheavey 73:54ab819609bc 144 int pointer = 1;
jamesheavey 91:c01a736fb0d9 145
jamesheavey 121:12acab75db8f 146 while (menu_toggle == false) {
jamesheavey 53:8e3da0b58fe9 147 lcd.clear();
jamesheavey 99:d8f1570faa05 148 lcd.printString(" START ",0,1); // start game with default as joystick
jamesheavey 73:54ab819609bc 149 lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt
jamesheavey 107:1b21b028d764 150 lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to interact with the game
jamesheavey 123:39740c246fc2 151 lcd.printString("HI-SCORE: ",0,5);
jamesheavey 106:a2957bab0c16 152 print_hi_score(55,5);
jamesheavey 56:f9e586348e0b 153 lcd.printString(" >",0,pointer);
jamesheavey 91:c01a736fb0d9 154 lcd.refresh();
jamesheavey 53:8e3da0b58fe9 155 wait(0.1);
jamesheavey 127:5994f1a186e0 156 if (pad.get_direction() == N && pointer > 1) { // if L is pressed and pointer isnt already on START, move it up one line
jamesheavey 50:3da51f34e253 157 pointer -= 1;
jamesheavey 53:8e3da0b58fe9 158 pad.tone(750.0,0.3);
jamesheavey 123:39740c246fc2 159 wait(0.4); // large wait time to reduce button bounce errors
jamesheavey 127:5994f1a186e0 160 } else if (pad.get_direction() == S && pointer < 3) { // if R is pressed and pointer isnt already on HOW TO PLAY, move it down one line
jamesheavey 50:3da51f34e253 161 pointer += 1;
jamesheavey 53:8e3da0b58fe9 162 pad.tone(750.0,0.3);
jamesheavey 123:39740c246fc2 163 wait(0.4); // large wait time to reduce button bounce errors
jamesheavey 50:3da51f34e253 164 }
jamesheavey 118:a44365bf061a 165 if (pad.check_event(Gamepad::X_PRESSED) & pad.check_event(Gamepad::Y_PRESSED)) {
jamesheavey 118:a44365bf061a 166 save_hi_score(0); // resets hi score
jamesheavey 118:a44365bf061a 167 }
jamesheavey 121:12acab75db8f 168 if (pad.check_event(Gamepad::A_PRESSED)) {
jamesheavey 121:12acab75db8f 169 menu_toggle = !menu_toggle;
jamesheavey 121:12acab75db8f 170 }
jamesheavey 99:d8f1570faa05 171 //printf("Pointer 1 = %d",pointer);
jamesheavey 91:c01a736fb0d9 172
jamesheavey 50:3da51f34e253 173 }
jamesheavey 73:54ab819609bc 174 if (pointer == 1) { // if START was selected on exit of the while loop, run main game with the appropriate tilt/joystick setting
jamesheavey 56:f9e586348e0b 175 pad.tone(750.0,0.3);
jamesheavey 99:d8f1570faa05 176 number_of_frames = 0;
jamesheavey 106:a2957bab0c16 177 main_game();
jamesheavey 91:c01a736fb0d9 178 } else if (pointer == 2) { // if SETTINGS was selected, enter the settings menu
jamesheavey 56:f9e586348e0b 179 pad.tone(750.0,0.3);
jamesheavey 51:9d5c10a88b22 180 settings();
jamesheavey 99:d8f1570faa05 181 } else if (pointer == 3) { // if HOW TO PLAY was selected, display instructions on how to play
jamesheavey 56:f9e586348e0b 182 pad.tone(750.0,0.3);
jamesheavey 51:9d5c10a88b22 183 how_to_play();
jamesheavey 51:9d5c10a88b22 184 }
jamesheavey 50:3da51f34e253 185 }
jamesheavey 50:3da51f34e253 186
jamesheavey 91:c01a736fb0d9 187 void settings()
jamesheavey 91:c01a736fb0d9 188 {
jamesheavey 50:3da51f34e253 189 lcd.clear();
jamesheavey 72:7254d2a8a1cd 190 lcd.printString(" JOYSTICK ",0,1); // choose joystick
jamesheavey 72:7254d2a8a1cd 191 lcd.printString(" TILT ",0,2); // choose tilt
jamesheavey 72:7254d2a8a1cd 192 lcd.printString("SENS :",0,4);
jamesheavey 72:7254d2a8a1cd 193 lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT);
jamesheavey 99:d8f1570faa05 194 lcd.drawRect(42,30,40 * pad.read_pot() + 1,10,FILL_BLACK); // represents the sensitivity which is changed by turning the pot
jamesheavey 91:c01a736fb0d9 195 lcd.refresh();
jamesheavey 107:1b21b028d764 196 wait(0.4); // load initial frame (delay helps button bounce)
jamesheavey 91:c01a736fb0d9 197
jamesheavey 107:1b21b028d764 198 int pointer = 1; // init the pointer
jamesheavey 91:c01a736fb0d9 199
jamesheavey 58:a159cd976aca 200 while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu
jamesheavey 53:8e3da0b58fe9 201 lcd.clear();
jamesheavey 72:7254d2a8a1cd 202 lcd.printString(" JOYSTICK ",0,1); // choose joystick
jamesheavey 72:7254d2a8a1cd 203 lcd.printString(" TILT ",0,2); // choose tilt
jamesheavey 72:7254d2a8a1cd 204 lcd.printString("SENS :",0,4);
jamesheavey 56:f9e586348e0b 205 lcd.printString(" >",0,pointer);
jamesheavey 72:7254d2a8a1cd 206 lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT);
jamesheavey 72:7254d2a8a1cd 207 lcd.drawRect(42,30,40 * pad.read_pot() + 1,10,FILL_BLACK); // have it so it fills half the transparent one (default position)
jamesheavey 91:c01a736fb0d9 208 lcd.refresh();
jamesheavey 53:8e3da0b58fe9 209 wait(0.1);
jamesheavey 127:5994f1a186e0 210 if (pad.get_direction() == N && pointer > 1) { // if L is pressed and pointer isnt already on JOYSTICK, move it up one line
jamesheavey 50:3da51f34e253 211 pointer -= 1;
jamesheavey 53:8e3da0b58fe9 212 pad.tone(750.0,0.3);
jamesheavey 123:39740c246fc2 213 wait(0.4); // large wait time to reduce button bounce errors
jamesheavey 127:5994f1a186e0 214 } else if (pad.get_direction() == S && pointer < 2) { // if R is pressed and pointer isnt already on TILT, move it down one line
jamesheavey 50:3da51f34e253 215 pointer += 1;
jamesheavey 53:8e3da0b58fe9 216 pad.tone(750.0,0.3);
jamesheavey 123:39740c246fc2 217 wait(0.4); // large wait time to reduce button bounce errors
jamesheavey 50:3da51f34e253 218 }
jamesheavey 91:c01a736fb0d9 219
jamesheavey 58:a159cd976aca 220 if (pad.check_event(Gamepad::A_PRESSED)) { // if A is pressed, switch the tilt option accordingly
jamesheavey 56:f9e586348e0b 221 pad.tone(750.0,0.3);
jamesheavey 56:f9e586348e0b 222 wait(0.1);
jamesheavey 74:dfb1d693d8e3 223 if (pointer == 1) { // if A is pressed on JOYSTICK, tilt = false
jamesheavey 57:d498dd835cfc 224 tilt = false;
jamesheavey 91:c01a736fb0d9 225 } else if (pointer == 2) { // if A is pressed on TILT, tilt == true
jamesheavey 57:d498dd835cfc 226 tilt = true;
jamesheavey 57:d498dd835cfc 227 }
jamesheavey 58:a159cd976aca 228 }
jamesheavey 91:c01a736fb0d9 229
jamesheavey 58:a159cd976aca 230 if (pad.check_event(Gamepad::B_PRESSED)) { // when B is pressed return to main menu
jamesheavey 56:f9e586348e0b 231 pad.tone(750.0,0.3);
jamesheavey 123:39740c246fc2 232 sens = pad.read_pot() + 0.5f; // sens is set as the pot value at the instant the settings menu is exited +the minimum value of 0.5
jamesheavey 105:4e7585d8e5e2 233 breakout.set_paddle_motion(tilt,sens); // sets the paddles motion options
jamesheavey 99:d8f1570faa05 234 //printf("Sensitivity = %d",sens);
jamesheavey 91:c01a736fb0d9 235 wait(0.3);
jamesheavey 50:3da51f34e253 236 main_menu();
jamesheavey 16:1761dfe801af 237 }
jamesheavey 99:d8f1570faa05 238 //printf("Pointer 2 = %d",pointer);
jamesheavey 0:7d4d2023ed9c 239 }
jamesheavey 10:9f445faa892c 240 }
jamesheavey 10:9f445faa892c 241
jamesheavey 107:1b21b028d764 242 void how_to_play() // explains how to interact with the game
jamesheavey 91:c01a736fb0d9 243 {
jamesheavey 84:6483503a72fc 244 lcd.clear();
jamesheavey 84:6483503a72fc 245 lcd.printString(" B = LASER ",0,0);
jamesheavey 84:6483503a72fc 246 lcd.printString(" START = PAUSE ",0,1);
jamesheavey 84:6483503a72fc 247 lcd.printString(" DESTROY ALL ",0,2);
jamesheavey 102:4946a6b47c78 248 lcd.printString(" BRICKS. ",0,3);
jamesheavey 91:c01a736fb0d9 249 arrowdown.render(lcd, 38, 43);
jamesheavey 84:6483503a72fc 250 lcd.refresh();
jamesheavey 107:1b21b028d764 251 wait(0.4); // load initial frame (delay helps button bounce)
jamesheavey 123:39740c246fc2 252
jamesheavey 107:1b21b028d764 253 while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu, display instruction on how to interact with the game
jamesheavey 91:c01a736fb0d9 254
jamesheavey 127:5994f1a186e0 255 if (pad.get_direction() == S) {
jamesheavey 84:6483503a72fc 256 lcd.clear();
jamesheavey 84:6483503a72fc 257 lcd.printString(" CONTINUE TO ",0,2);
jamesheavey 84:6483503a72fc 258 lcd.printString("INCREASE SCORE. ",0,3);
jamesheavey 102:4946a6b47c78 259 lcd.printString(" BONUS SCORE ",0,4);
jamesheavey 102:4946a6b47c78 260 lcd.printString("BASED ON TIME. ",0,5);
jamesheavey 91:c01a736fb0d9 261 arrowup.render(lcd, 38, 0);
jamesheavey 91:c01a736fb0d9 262 lcd.refresh();
jamesheavey 84:6483503a72fc 263 wait(0.1);
jamesheavey 84:6483503a72fc 264 }
jamesheavey 127:5994f1a186e0 265 if (pad.get_direction() == N) {
jamesheavey 84:6483503a72fc 266 lcd.clear();
jamesheavey 84:6483503a72fc 267 lcd.printString(" B = LASER ",0,0);
jamesheavey 84:6483503a72fc 268 lcd.printString(" START = PAUSE ",0,1);
jamesheavey 84:6483503a72fc 269 lcd.printString(" DESTROY ALL ",0,2);
jamesheavey 84:6483503a72fc 270 lcd.printString(" BRICKS ",0,3);
jamesheavey 91:c01a736fb0d9 271 arrowdown.render(lcd, 38, 43);
jamesheavey 84:6483503a72fc 272 lcd.refresh();
jamesheavey 84:6483503a72fc 273 wait(0.1);
jamesheavey 84:6483503a72fc 274 }
jamesheavey 50:3da51f34e253 275 }
jamesheavey 84:6483503a72fc 276 wait(0.3);
jamesheavey 58:a159cd976aca 277 main_menu(); // when B is pressed, the loop is exited and main menu is entered once again
jamesheavey 58:a159cd976aca 278 }
jamesheavey 58:a159cd976aca 279
jamesheavey 58:a159cd976aca 280
jamesheavey 106:a2957bab0c16 281 void main_game()
jamesheavey 91:c01a736fb0d9 282 {
jamesheavey 58:a159cd976aca 283 int fps = 8; // frames per second
jamesheavey 58:a159cd976aca 284 bool pause = false; // set pause screen to false
jamesheavey 99:d8f1570faa05 285 //printf("Pause = %d",pointer);
jamesheavey 123:39740c246fc2 286
jamesheavey 59:fdc05d5778a6 287 countdown(); // run the countdown
jamesheavey 91:c01a736fb0d9 288
jamesheavey 91:c01a736fb0d9 289 render(); // first draw the initial frame
jamesheavey 58:a159cd976aca 290 wait(1.0f/fps); // and wait for one frame period
jamesheavey 58:a159cd976aca 291
jamesheavey 58:a159cd976aca 292
jamesheavey 58:a159cd976aca 293 // game loop - read input, update the game state and render the display
jamesheavey 58:a159cd976aca 294 while (1) {
jamesheavey 105:4e7585d8e5e2 295 breakout.read_input(pad); // read input from pad
jamesheavey 59:fdc05d5778a6 296 breakout.update(pad); // update game
jamesheavey 104:c2d49c4f3e06 297 lcd.setBrightness(1); // stops the game from dimming (bug)
jamesheavey 59:fdc05d5778a6 298 render(); // draw new frame
jamesheavey 91:c01a736fb0d9 299
jamesheavey 81:735e5ee2c92a 300 if (breakout.check_loss(pad) == true) { // if life lost flash screen
jamesheavey 58:a159cd976aca 301 flash_screen(lcd);
jamesheavey 58:a159cd976aca 302 }
jamesheavey 111:4848c399fae0 303 if (pad.check_event(Gamepad::START_PRESSED)) { // if BACK pressed, toggle pause
jamesheavey 58:a159cd976aca 304 pause = !pause;
jamesheavey 110:780fdecd9171 305 wait(0.4); // delay to help with button bounce
jamesheavey 58:a159cd976aca 306 }
jamesheavey 91:c01a736fb0d9 307
jamesheavey 59:fdc05d5778a6 308 while (pause == true) { // if pause is true, display pause screen
jamesheavey 58:a159cd976aca 309 lcd.clear();
jamesheavey 83:645cdbd79c21 310 lcd.printString(" PAUSED ",0,1);
jamesheavey 83:645cdbd79c21 311 lcd.printString(" START = PLAY",0,3);
jamesheavey 83:645cdbd79c21 312 lcd.printString(" BACK = MENU",0,4);
jamesheavey 58:a159cd976aca 313 lcd.refresh();
jamesheavey 111:4848c399fae0 314 if (pad.check_event(Gamepad::START_PRESSED)) { // if START pressed, toggle pause, leaving pause screen
jamesheavey 58:a159cd976aca 315 pause = !pause;
jamesheavey 110:780fdecd9171 316 wait(0.4); // delay to help with button bounce
jamesheavey 58:a159cd976aca 317 }
jamesheavey 111:4848c399fae0 318 if (pad.check_event(Gamepad::BACK_PRESSED)) { // if BACK pressed, return to the title screen
jamesheavey 117:4eedd15f2c3d 319 reset_loss();
jamesheavey 83:645cdbd79c21 320 title_screen();
jamesheavey 83:645cdbd79c21 321 }
jamesheavey 58:a159cd976aca 322 }
jamesheavey 91:c01a736fb0d9 323
jamesheavey 79:70510b0102af 324 if (breakout.get_lives() == 0) { // when all lives lost, enter loss screen
jamesheavey 59:fdc05d5778a6 325 pad.leds_off(); //turns last led off (doesnt run through and update board so last led doent turn off via lives leds
jamesheavey 123:39740c246fc2 326 loss_screen();
jamesheavey 58:a159cd976aca 327 }
jamesheavey 91:c01a736fb0d9 328
jamesheavey 59:fdc05d5778a6 329 if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen
jamesheavey 58:a159cd976aca 330 victory_screen();
jamesheavey 58:a159cd976aca 331 }
jamesheavey 99:d8f1570faa05 332 number_of_frames ++; // track the number of frames passed to add to the score (inversely proportional)
jamesheavey 58:a159cd976aca 333 wait(1.0f/fps);
jamesheavey 58:a159cd976aca 334 }
jamesheavey 50:3da51f34e253 335 }
jamesheavey 16:1761dfe801af 336
jamesheavey 99:d8f1570faa05 337 void loss_screen() // loss screen when lives of paddle == 0
jamesheavey 64:c3426b417ad9 338 {
jamesheavey 107:1b21b028d764 339 if (compare_to_hi_score(breakout.get_score()) == true) { // little hi-score sequence if current score is higher than prev
jamesheavey 120:33dabf094a6d 340 flash_hi_score_screen();
jamesheavey 106:a2957bab0c16 341 }
jamesheavey 123:39740c246fc2 342
jamesheavey 15:b867a6620f96 343 lcd.clear();
jamesheavey 111:4848c399fae0 344 char buffer[14]; // init the buffer
jamesheavey 111:4848c399fae0 345 sprintf(buffer,"%2d",breakout.get_score()); // print the score to the buffer
jamesheavey 111:4848c399fae0 346 lcd.printString(buffer,WIDTH/2 - 12,2); // print buffer on lcd
jamesheavey 64:c3426b417ad9 347 lcd.printString(" RESTART? ",2,1);
jamesheavey 72:7254d2a8a1cd 348 lcd.printString(" PRESS START ",1,4);
jamesheavey 91:c01a736fb0d9 349 lcd.refresh();
jamesheavey 15:b867a6620f96 350 pad.tone(750.0,0.3);
jamesheavey 15:b867a6620f96 351 wait(0.4);
jamesheavey 91:c01a736fb0d9 352
jamesheavey 15:b867a6620f96 353 pad.tone(300.0,0.3);
jamesheavey 15:b867a6620f96 354 wait(0.4);
jamesheavey 91:c01a736fb0d9 355
jamesheavey 107:1b21b028d764 356 while (pad.check_event(Gamepad::START_PRESSED) == false) { // flashes the score, waits for START to return to title screen
jamesheavey 10:9f445faa892c 357 lcd.clear();
jamesheavey 91:c01a736fb0d9 358
jamesheavey 123:39740c246fc2 359 lcd.printString(" RESTART? ",2,1);
jamesheavey 64:c3426b417ad9 360 lcd.printString(" PRESS START ",1,4);
jamesheavey 10:9f445faa892c 361 lcd.refresh();
jamesheavey 91:c01a736fb0d9 362
jamesheavey 64:c3426b417ad9 363 wait(0.4);
jamesheavey 91:c01a736fb0d9 364
jamesheavey 64:c3426b417ad9 365 lcd.clear();
jamesheavey 123:39740c246fc2 366
jamesheavey 123:39740c246fc2 367 lcd.printString(buffer,WIDTH/2 - 12,2);
jamesheavey 123:39740c246fc2 368 lcd.printString(" RESTART? ",2,1);
jamesheavey 64:c3426b417ad9 369 lcd.printString(" PRESS START ",1,4);
jamesheavey 64:c3426b417ad9 370 lcd.refresh();
jamesheavey 91:c01a736fb0d9 371
jamesheavey 64:c3426b417ad9 372 wait(0.4);
jamesheavey 10:9f445faa892c 373 }
jamesheavey 117:4eedd15f2c3d 374 reset_loss();
jamesheavey 49:31eb7807dbd3 375 title_screen();
jamesheavey 10:9f445faa892c 376 }
jamesheavey 10:9f445faa892c 377
jamesheavey 112:26961473eae8 378 void victory_screen() // victory screen when all bricks are destroyed
jamesheavey 64:c3426b417ad9 379 {
jamesheavey 99:d8f1570faa05 380 int bonus = NULL;
jamesheavey 123:39740c246fc2 381
jamesheavey 111:4848c399fae0 382 if (breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720 > 0) { // beat within 90 seconds (8 fps) and you get a bonus
jamesheavey 99:d8f1570faa05 383 bonus = breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720;
jamesheavey 123:39740c246fc2 384 } else {
jamesheavey 112:26961473eae8 385 bonus = 0; // else you get no bonus
jamesheavey 99:d8f1570faa05 386 }
jamesheavey 123:39740c246fc2 387
jamesheavey 111:4848c399fae0 388 if (compare_to_hi_score(bonus+breakout.get_score()) == true) { // little hi-score sequence if current score is higher than prev
jamesheavey 120:33dabf094a6d 389 flash_hi_score_screen();
jamesheavey 102:4946a6b47c78 390 }
jamesheavey 123:39740c246fc2 391
jamesheavey 15:b867a6620f96 392 lcd.clear();
jamesheavey 123:39740c246fc2 393
jamesheavey 123:39740c246fc2 394 char buffer1[14];
jamesheavey 64:c3426b417ad9 395 sprintf(buffer1,"%2d",breakout.get_score());
jamesheavey 67:c362df66fac9 396 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 123:39740c246fc2 397
jamesheavey 99:d8f1570faa05 398 char buffer2[14];
jamesheavey 99:d8f1570faa05 399 sprintf(buffer2,"%2d",bonus);
jamesheavey 124:d635e3154bf3 400 lcd.printString(buffer2,WIDTH/2 + 12,3);
jamesheavey 124:d635e3154bf3 401 lcd.printString(" BONUS: ",2,3);
jamesheavey 123:39740c246fc2 402
jamesheavey 103:f9f69944a850 403 lcd.printString(" VICTORY! ",2,0);
jamesheavey 123:39740c246fc2 404 lcd.printString(" CONT = START ",0,4);
jamesheavey 100:9842813b7131 405 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 15:b867a6620f96 406 lcd.refresh();
jamesheavey 22:fa2e0b58043a 407 pad.tone(2500.0,0.1);
jamesheavey 22:fa2e0b58043a 408 wait(0.4);
jamesheavey 91:c01a736fb0d9 409
jamesheavey 22:fa2e0b58043a 410 pad.tone(2500.0,0.1);
jamesheavey 22:fa2e0b58043a 411 wait(0.2);
jamesheavey 91:c01a736fb0d9 412
jamesheavey 15:b867a6620f96 413 pad.tone(4000.0,0.6);
jamesheavey 15:b867a6620f96 414 wait(0.6);
jamesheavey 123:39740c246fc2 415
jamesheavey 123:39740c246fc2 416 while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) { // while neither START or BACK is pressed, flash the score and display options
jamesheavey 13:418a71b24b37 417 lcd.clear();
jamesheavey 91:c01a736fb0d9 418
jamesheavey 123:39740c246fc2 419 lcd.printString(" VICTORY! ",2,0);
jamesheavey 47:1d1a827be81b 420 lcd.printString(" CONT = START ",0,4);
jamesheavey 100:9842813b7131 421 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 43:81d22f68dfae 422
jamesheavey 13:418a71b24b37 423 lcd.refresh();
jamesheavey 91:c01a736fb0d9 424
jamesheavey 64:c3426b417ad9 425 wait(0.4);
jamesheavey 91:c01a736fb0d9 426
jamesheavey 64:c3426b417ad9 427 lcd.clear();
jamesheavey 91:c01a736fb0d9 428
jamesheavey 67:c362df66fac9 429 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 124:d635e3154bf3 430 lcd.printString(buffer2,WIDTH/2 + 12,3);
jamesheavey 124:d635e3154bf3 431 lcd.printString(" BONUS:",2,3);
jamesheavey 123:39740c246fc2 432
jamesheavey 103:f9f69944a850 433 lcd.printString(" VICTORY! ",2,0);
jamesheavey 123:39740c246fc2 434 lcd.printString(" CONT = START ",0,4);
jamesheavey 100:9842813b7131 435 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 64:c3426b417ad9 436 lcd.refresh();
jamesheavey 91:c01a736fb0d9 437
jamesheavey 64:c3426b417ad9 438 pad.tone(2500.0,0.1);
jamesheavey 64:c3426b417ad9 439
jamesheavey 64:c3426b417ad9 440 lcd.refresh();
jamesheavey 91:c01a736fb0d9 441
jamesheavey 64:c3426b417ad9 442 wait(0.4);
jamesheavey 91:c01a736fb0d9 443
jamesheavey 43:81d22f68dfae 444 if (pad.check_event(Gamepad::START_PRESSED)) {
jamesheavey 123:39740c246fc2 445 reset_victory(breakout.get_score(),bonus); // reset game
jamesheavey 123:39740c246fc2 446 main_game(); // reload game
jamesheavey 91:c01a736fb0d9 447 } else if (pad.check_event(Gamepad::BACK_PRESSED)) {
jamesheavey 123:39740c246fc2 448 reset_loss(); // reset game
jamesheavey 123:39740c246fc2 449 title_screen(); // load title screen
jamesheavey 123:39740c246fc2 450 }
jamesheavey 13:418a71b24b37 451 }
jamesheavey 13:418a71b24b37 452 }
jamesheavey 13:418a71b24b37 453
jamesheavey 112:26961473eae8 454 void flash_screen(N5110 &lcd) // flash the screen when a life is lost
jamesheavey 91:c01a736fb0d9 455 {
jamesheavey 58:a159cd976aca 456 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 457 wait(0.1);
jamesheavey 58:a159cd976aca 458 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 459 wait(0.1);
jamesheavey 58:a159cd976aca 460 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 461 wait(0.1);
jamesheavey 47:1d1a827be81b 462 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 463 wait(0.1);
jamesheavey 58:a159cd976aca 464 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 465 wait(0.1);
jamesheavey 58:a159cd976aca 466 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 467 wait(0.1);
jamesheavey 58:a159cd976aca 468 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 469 wait(0.1);
jamesheavey 58:a159cd976aca 470 lcd.setBrightness(1);
jamesheavey 59:fdc05d5778a6 471 }
jamesheavey 59:fdc05d5778a6 472
jamesheavey 112:26961473eae8 473 void countdown() // draw the countdown
jamesheavey 91:c01a736fb0d9 474 {
jamesheavey 104:c2d49c4f3e06 475 lcd.setBrightness(1); // stops the game from dimming (bug)
jamesheavey 123:39740c246fc2 476
jamesheavey 59:fdc05d5778a6 477 lcd.clear();
jamesheavey 59:fdc05d5778a6 478 three.render(lcd, 0, 0); // render the 3
jamesheavey 59:fdc05d5778a6 479 lcd.refresh();
jamesheavey 59:fdc05d5778a6 480 pad.tone(500.0,0.5);
jamesheavey 59:fdc05d5778a6 481 wait(1); // wait 1 second
jamesheavey 123:39740c246fc2 482
jamesheavey 106:a2957bab0c16 483 lcd.setBrightness(1);
jamesheavey 123:39740c246fc2 484
jamesheavey 59:fdc05d5778a6 485 lcd.clear();
jamesheavey 59:fdc05d5778a6 486 two.render(lcd, 0, 0); // render 2
jamesheavey 59:fdc05d5778a6 487 lcd.refresh();
jamesheavey 59:fdc05d5778a6 488 pad.tone(500.0,0.5);
jamesheavey 59:fdc05d5778a6 489 wait(1); // wait 1 second
jamesheavey 123:39740c246fc2 490
jamesheavey 106:a2957bab0c16 491 lcd.setBrightness(1);
jamesheavey 123:39740c246fc2 492
jamesheavey 59:fdc05d5778a6 493 lcd.clear();
jamesheavey 59:fdc05d5778a6 494 one.render(lcd, 0, 0); // render 1
jamesheavey 59:fdc05d5778a6 495 lcd.refresh();
jamesheavey 59:fdc05d5778a6 496 pad.tone(1000.0,1);
jamesheavey 59:fdc05d5778a6 497 wait(1); // wait 1 second
jamesheavey 98:2ce2c666266b 498 }
jamesheavey 101:0df767523dbd 499
jamesheavey 112:26961473eae8 500 void save_hi_score(int hi_score) // save score to SD card
jamesheavey 98:2ce2c666266b 501 {
jamesheavey 105:4e7585d8e5e2 502 serial.baud(115200); // max speed
jamesheavey 105:4e7585d8e5e2 503 FILE *fp; // file pointer
jamesheavey 113:d180342ac017 504 fp = fopen("/sd/hi_score.txt", "w");
jamesheavey 98:2ce2c666266b 505 if (fp == NULL) { // if it can't open the file then print error message
jamesheavey 98:2ce2c666266b 506 serial.printf("Error! Unable to open file!\n");
jamesheavey 98:2ce2c666266b 507 } else { // opened file so can write
jamesheavey 98:2ce2c666266b 508 serial.printf("Writing to file....");
jamesheavey 123:39740c246fc2 509 fprintf(fp, "%d",hi_score);
jamesheavey 98:2ce2c666266b 510 serial.printf("Done.\n");
jamesheavey 107:1b21b028d764 511 fclose(fp); // close the file after writing
jamesheavey 98:2ce2c666266b 512 }
jamesheavey 98:2ce2c666266b 513 }
jamesheavey 98:2ce2c666266b 514
jamesheavey 112:26961473eae8 515 int get_hi_score() // retrieve score from SD card
jamesheavey 98:2ce2c666266b 516 {
jamesheavey 105:4e7585d8e5e2 517 serial.baud(115200); // max speed
jamesheavey 101:0df767523dbd 518 FILE *fp;
jamesheavey 113:d180342ac017 519 fp = fopen("/sd/hi_score.txt", "r");
jamesheavey 98:2ce2c666266b 520 int stored_hi_score = NULL;
jamesheavey 98:2ce2c666266b 521 if (fp == NULL) { // if it can't open the file then print error message
jamesheavey 98:2ce2c666266b 522 serial.printf("Error! Unable to open file!\n");
jamesheavey 98:2ce2c666266b 523 } else { // opened file so can write
jamesheavey 123:39740c246fc2 524 fscanf(fp, "%d",&stored_hi_score);
jamesheavey 101:0df767523dbd 525 serial.printf("Read %d from file.\n",stored_hi_score);
jamesheavey 107:1b21b028d764 526 fclose(fp); // close the file after reading
jamesheavey 98:2ce2c666266b 527 }
jamesheavey 107:1b21b028d764 528 return stored_hi_score; // returns the stored hi score
jamesheavey 99:d8f1570faa05 529 }
jamesheavey 101:0df767523dbd 530
jamesheavey 107:1b21b028d764 531 bool compare_to_hi_score(int score) // returns true if new score higher than current stored hi score
jamesheavey 101:0df767523dbd 532 {
jamesheavey 101:0df767523dbd 533 if (score >= get_hi_score()) {
jamesheavey 107:1b21b028d764 534 //printf("hi score! \n");
jamesheavey 107:1b21b028d764 535 save_hi_score(score); // writes the new score to the SD card
jamesheavey 101:0df767523dbd 536 return true;
jamesheavey 123:39740c246fc2 537 } else {
jamesheavey 123:39740c246fc2 538 return false;
jamesheavey 101:0df767523dbd 539 }
jamesheavey 101:0df767523dbd 540 }
jamesheavey 103:f9f69944a850 541
jamesheavey 112:26961473eae8 542 void print_hi_score(int col,int row) // print hi score to lcd at specified location
jamesheavey 103:f9f69944a850 543 {
jamesheavey 107:1b21b028d764 544 char buffer[14]; // creates buffer
jamesheavey 107:1b21b028d764 545 sprintf(buffer,"%2d",get_hi_score()); // puts hi-score in buffer
jamesheavey 107:1b21b028d764 546 lcd.printString(buffer,col,row); // prints buffer to the screen
jamesheavey 123:39740c246fc2 547
jamesheavey 103:f9f69944a850 548 }
jamesheavey 117:4eedd15f2c3d 549
jamesheavey 117:4eedd15f2c3d 550 void reset_loss() // reset the game when returning to title screen from any point
jamesheavey 117:4eedd15f2c3d 551 {
jamesheavey 117:4eedd15f2c3d 552 breakout.reset_paddle_lives(); // resets lives back to 6
jamesheavey 117:4eedd15f2c3d 553 breakout.set_prev_score(0); // resets prev score to 0
jamesheavey 117:4eedd15f2c3d 554 number_of_frames = 0; // reset the number of frames
jamesheavey 117:4eedd15f2c3d 555 breakout.set_mult_zero(); // reset multiplier
jamesheavey 123:39740c246fc2 556 breakout.reset_game(); // return game to initial positions
jamesheavey 117:4eedd15f2c3d 557 }
jamesheavey 117:4eedd15f2c3d 558
jamesheavey 117:4eedd15f2c3d 559 void reset_victory(int score, int bonus) // reset the game after a victory
jamesheavey 123:39740c246fc2 560 {
jamesheavey 117:4eedd15f2c3d 561 breakout.set_prev_score(score + bonus); // saves score
jamesheavey 117:4eedd15f2c3d 562 number_of_frames = 0; // reset the number of frames
jamesheavey 117:4eedd15f2c3d 563 breakout.inc_mult(); // increment multiplier
jamesheavey 123:39740c246fc2 564 breakout.reset_game(); // return game to initial positions
jamesheavey 117:4eedd15f2c3d 565 }
jamesheavey 120:33dabf094a6d 566
jamesheavey 121:12acab75db8f 567 void flash_hi_score_screen() // flash the hi score, called when hi score acheieved
jamesheavey 120:33dabf094a6d 568 {
jamesheavey 120:33dabf094a6d 569 lcd.clear();
jamesheavey 120:33dabf094a6d 570 lcd.printString(" NEW ",0,2);
jamesheavey 120:33dabf094a6d 571 lcd.printString(" HI-SCORE! ",0,3);
jamesheavey 120:33dabf094a6d 572 lcd.refresh();
jamesheavey 123:39740c246fc2 573
jamesheavey 120:33dabf094a6d 574 pad.tone(2500.0,0.2);
jamesheavey 120:33dabf094a6d 575 wait(0.2);
jamesheavey 123:39740c246fc2 576
jamesheavey 120:33dabf094a6d 577 pad.tone(4000.0,0.4);
jamesheavey 120:33dabf094a6d 578 wait(1);
jamesheavey 123:39740c246fc2 579
jamesheavey 120:33dabf094a6d 580 lcd.clear();
jamesheavey 120:33dabf094a6d 581 print_hi_score(30,3);
jamesheavey 120:33dabf094a6d 582 lcd.refresh();
jamesheavey 123:39740c246fc2 583
jamesheavey 120:33dabf094a6d 584 pad.tone(2500.0,0.2);
jamesheavey 120:33dabf094a6d 585 wait(0.2);
jamesheavey 123:39740c246fc2 586
jamesheavey 120:33dabf094a6d 587 pad.tone(4000.0,0.4);
jamesheavey 120:33dabf094a6d 588 wait(1);
jamesheavey 123:39740c246fc2 589
jamesheavey 120:33dabf094a6d 590 lcd.clear();
jamesheavey 120:33dabf094a6d 591 lcd.printString(" NEW ",0,2);
jamesheavey 120:33dabf094a6d 592 lcd.printString(" HI-SCORE! ",0,3);
jamesheavey 120:33dabf094a6d 593 lcd.refresh();
jamesheavey 123:39740c246fc2 594
jamesheavey 120:33dabf094a6d 595 pad.tone(2500.0,0.2);
jamesheavey 120:33dabf094a6d 596 wait(0.2);
jamesheavey 123:39740c246fc2 597
jamesheavey 120:33dabf094a6d 598 pad.tone(4000.0,0.4);
jamesheavey 120:33dabf094a6d 599 wait(1);
jamesheavey 123:39740c246fc2 600
jamesheavey 120:33dabf094a6d 601 lcd.clear();
jamesheavey 120:33dabf094a6d 602 print_hi_score(30,3);
jamesheavey 120:33dabf094a6d 603 lcd.refresh();
jamesheavey 123:39740c246fc2 604
jamesheavey 120:33dabf094a6d 605 pad.tone(2500.0,0.2);
jamesheavey 120:33dabf094a6d 606 wait(0.2);
jamesheavey 123:39740c246fc2 607
jamesheavey 120:33dabf094a6d 608 pad.tone(4000.0,0.4);
jamesheavey 120:33dabf094a6d 609 wait(1.3);
jamesheavey 120:33dabf094a6d 610 }