FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue May 07 18:26:57 2019 +0000
Revision:
104:c2d49c4f3e06
Parent:
103:f9f69944a850
Child:
105:4e7585d8e5e2
add prinf's, fix mult bug, comment, doxygen, modularise the menus

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