FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed May 08 04:00:00 2019 +0000
Revision:
113:d180342ac017
Parent:
112:26961473eae8
Child:
116:2793d31ca691
also, make a function to call all the resets in the menu

Who changed what in which revision?

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