FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 15:04:27 2019 +0000
Revision:
120:33dabf094a6d
Parent:
118:a44365bf061a
Child:
121:12acab75db8f

        

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