James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 15:59:38 2019 +0000
Revision:
123:39740c246fc2
Parent:
122:a78a1b98b9b1
Child:
124:d635e3154bf3
final final i promise

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