FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue May 07 18:17:37 2019 +0000
Revision:
103:f9f69944a850
Parent:
102:4946a6b47c78
Child:
104:c2d49c4f3e06
only error left is the multiplier isnt correct when replaying after returning to title screen at any point starts at +1

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 91:c01a736fb0d9 263
jamesheavey 58:a159cd976aca 264 lcd.setBrightness(1);
jamesheavey 91:c01a736fb0d9 265
jamesheavey 59:fdc05d5778a6 266 countdown(); // run the countdown
jamesheavey 91:c01a736fb0d9 267
jamesheavey 91:c01a736fb0d9 268 render(); // first draw the initial frame
jamesheavey 58:a159cd976aca 269 wait(1.0f/fps); // and wait for one frame period
jamesheavey 58:a159cd976aca 270
jamesheavey 58:a159cd976aca 271
jamesheavey 58:a159cd976aca 272 // game loop - read input, update the game state and render the display
jamesheavey 58:a159cd976aca 273 while (1) {
jamesheavey 72:7254d2a8a1cd 274 breakout.read_input(pad,tilt,sens); // read input from pad
jamesheavey 59:fdc05d5778a6 275 breakout.update(pad); // update game
jamesheavey 59:fdc05d5778a6 276 render(); // draw new frame
jamesheavey 91:c01a736fb0d9 277
jamesheavey 81:735e5ee2c92a 278 if (breakout.check_loss(pad) == true) { // if life lost flash screen
jamesheavey 58:a159cd976aca 279 flash_screen(lcd);
jamesheavey 58:a159cd976aca 280 }
jamesheavey 83:645cdbd79c21 281 if (pad.check_event(Gamepad::START_PRESSED) == true) { // if BACK pressed, toggle pause
jamesheavey 58:a159cd976aca 282 pause = !pause;
jamesheavey 58:a159cd976aca 283 }
jamesheavey 91:c01a736fb0d9 284
jamesheavey 59:fdc05d5778a6 285 while (pause == true) { // if pause is true, display pause screen
jamesheavey 58:a159cd976aca 286 lcd.clear();
jamesheavey 83:645cdbd79c21 287 lcd.printString(" PAUSED ",0,1);
jamesheavey 83:645cdbd79c21 288 lcd.printString(" START = PLAY",0,3);
jamesheavey 83:645cdbd79c21 289 lcd.printString(" BACK = MENU",0,4);
jamesheavey 58:a159cd976aca 290 lcd.refresh();
jamesheavey 99:d8f1570faa05 291 if (pad.check_event(Gamepad::START_PRESSED) == true) { // if START pressed, toggle pause, leaving pause screen
jamesheavey 58:a159cd976aca 292 pause = !pause;
jamesheavey 58:a159cd976aca 293 }
jamesheavey 99:d8f1570faa05 294 if (pad.check_event(Gamepad::BACK_PRESSED) == true) { // if BACK pressed, return to the title screen
jamesheavey 99:d8f1570faa05 295 breakout.set_mult_one(); // reset multiplier
jamesheavey 99:d8f1570faa05 296 breakout.set_prev_score(0);
jamesheavey 99:d8f1570faa05 297 breakout.reset_game(); //reset game to initial positions
jamesheavey 83:645cdbd79c21 298 title_screen();
jamesheavey 83:645cdbd79c21 299 }
jamesheavey 58:a159cd976aca 300 }
jamesheavey 91:c01a736fb0d9 301
jamesheavey 79:70510b0102af 302 if (breakout.get_lives() == 0) { // when all lives lost, enter loss screen
jamesheavey 59:fdc05d5778a6 303 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 304 breakout.set_mult_one(); // reset multiplier
jamesheavey 99:d8f1570faa05 305 breakout.reset_game(); // return game to initial positions
jamesheavey 99:d8f1570faa05 306 loss_screen();
jamesheavey 58:a159cd976aca 307 }
jamesheavey 91:c01a736fb0d9 308
jamesheavey 59:fdc05d5778a6 309 if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen
jamesheavey 99:d8f1570faa05 310 breakout.reset_game(); // return game to initial positions
jamesheavey 99:d8f1570faa05 311 breakout.inc_mult(); // increment multiplier
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 15:b867a6620f96 321 lcd.clear();
jamesheavey 64:c3426b417ad9 322 char buffer1[14];
jamesheavey 64:c3426b417ad9 323 sprintf(buffer1,"%2d",breakout.get_score());
jamesheavey 67:c362df66fac9 324 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 64:c3426b417ad9 325 lcd.printString(" RESTART? ",2,1);
jamesheavey 72:7254d2a8a1cd 326 lcd.printString(" PRESS START ",1,4);
jamesheavey 91:c01a736fb0d9 327 lcd.refresh();
jamesheavey 15:b867a6620f96 328 pad.tone(750.0,0.3);
jamesheavey 15:b867a6620f96 329 wait(0.4);
jamesheavey 91:c01a736fb0d9 330
jamesheavey 15:b867a6620f96 331 pad.tone(300.0,0.3);
jamesheavey 15:b867a6620f96 332 wait(0.4);
jamesheavey 91:c01a736fb0d9 333
jamesheavey 91:c01a736fb0d9 334 while (pad.check_event(Gamepad::START_PRESSED) == false) {
jamesheavey 10:9f445faa892c 335 lcd.clear();
jamesheavey 91:c01a736fb0d9 336
jamesheavey 64:c3426b417ad9 337 lcd.printString(" RESTART? ",2,1); //print score here
jamesheavey 64:c3426b417ad9 338 lcd.printString(" PRESS START ",1,4);
jamesheavey 10:9f445faa892c 339 lcd.refresh();
jamesheavey 91:c01a736fb0d9 340
jamesheavey 64:c3426b417ad9 341 wait(0.4);
jamesheavey 91:c01a736fb0d9 342
jamesheavey 64:c3426b417ad9 343 lcd.clear();
jamesheavey 91:c01a736fb0d9 344
jamesheavey 64:c3426b417ad9 345 char buffer1[14];
jamesheavey 64:c3426b417ad9 346 sprintf(buffer1,"%2d",breakout.get_score());
jamesheavey 67:c362df66fac9 347 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 64:c3426b417ad9 348 lcd.printString(" RESTART? ",2,1); //print score here
jamesheavey 64:c3426b417ad9 349 lcd.printString(" PRESS START ",1,4);
jamesheavey 64:c3426b417ad9 350 lcd.refresh();
jamesheavey 91:c01a736fb0d9 351
jamesheavey 64:c3426b417ad9 352 wait(0.4);
jamesheavey 10:9f445faa892c 353 }
jamesheavey 64:c3426b417ad9 354 breakout.set_prev_score(0);
jamesheavey 49:31eb7807dbd3 355 title_screen();
jamesheavey 10:9f445faa892c 356 }
jamesheavey 10:9f445faa892c 357
jamesheavey 91:c01a736fb0d9 358 void victory_screen()
jamesheavey 64:c3426b417ad9 359 {
jamesheavey 99:d8f1570faa05 360 int bonus = NULL;
jamesheavey 99:d8f1570faa05 361
jamesheavey 99:d8f1570faa05 362 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 363 bonus = breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720;
jamesheavey 99:d8f1570faa05 364 }
jamesheavey 99:d8f1570faa05 365 else {
jamesheavey 99:d8f1570faa05 366 bonus = 0;
jamesheavey 99:d8f1570faa05 367 }
jamesheavey 99:d8f1570faa05 368
jamesheavey 102:4946a6b47c78 369 if (compare_to_hi_score(bonus+breakout.get_score()) == true) {
jamesheavey 103:f9f69944a850 370 void flash_hi_score();
jamesheavey 102:4946a6b47c78 371 }
jamesheavey 102:4946a6b47c78 372
jamesheavey 15:b867a6620f96 373 lcd.clear();
jamesheavey 99:d8f1570faa05 374
jamesheavey 64:c3426b417ad9 375 char buffer1[14];
jamesheavey 64:c3426b417ad9 376 sprintf(buffer1,"%2d",breakout.get_score());
jamesheavey 67:c362df66fac9 377 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 99:d8f1570faa05 378
jamesheavey 99:d8f1570faa05 379 char buffer2[14];
jamesheavey 99:d8f1570faa05 380 sprintf(buffer2,"%2d",bonus);
jamesheavey 100:9842813b7131 381 lcd.printString(buffer2,WIDTH/2 + 10,3);
jamesheavey 103:f9f69944a850 382 lcd.printString(" BONUS: ",2,3);
jamesheavey 99:d8f1570faa05 383
jamesheavey 103:f9f69944a850 384 lcd.printString(" VICTORY! ",2,0);
jamesheavey 47:1d1a827be81b 385 lcd.printString(" CONT = START ",0,4); // print score here
jamesheavey 100:9842813b7131 386 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 15:b867a6620f96 387 lcd.refresh();
jamesheavey 22:fa2e0b58043a 388 pad.tone(2500.0,0.1);
jamesheavey 22:fa2e0b58043a 389 wait(0.4);
jamesheavey 91:c01a736fb0d9 390
jamesheavey 22:fa2e0b58043a 391 pad.tone(2500.0,0.1);
jamesheavey 22:fa2e0b58043a 392 wait(0.2);
jamesheavey 91:c01a736fb0d9 393
jamesheavey 15:b867a6620f96 394 pad.tone(4000.0,0.6);
jamesheavey 15:b867a6620f96 395 wait(0.6);
jamesheavey 99:d8f1570faa05 396
jamesheavey 91:c01a736fb0d9 397 while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) {
jamesheavey 13:418a71b24b37 398 lcd.clear();
jamesheavey 91:c01a736fb0d9 399
jamesheavey 103:f9f69944a850 400 lcd.printString(" VICTORY! ",2,0);
jamesheavey 47:1d1a827be81b 401 lcd.printString(" CONT = START ",0,4);
jamesheavey 100:9842813b7131 402 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 43:81d22f68dfae 403
jamesheavey 13:418a71b24b37 404 lcd.refresh();
jamesheavey 91:c01a736fb0d9 405
jamesheavey 64:c3426b417ad9 406 wait(0.4);
jamesheavey 91:c01a736fb0d9 407
jamesheavey 64:c3426b417ad9 408 lcd.clear();
jamesheavey 91:c01a736fb0d9 409
jamesheavey 99:d8f1570faa05 410 char buffer1[14];
jamesheavey 64:c3426b417ad9 411 sprintf(buffer1,"%2d",breakout.get_score());
jamesheavey 67:c362df66fac9 412 lcd.printString(buffer1,WIDTH/2 - 12,2);
jamesheavey 99:d8f1570faa05 413
jamesheavey 99:d8f1570faa05 414 char buffer2[14];
jamesheavey 99:d8f1570faa05 415 sprintf(buffer2,"%2d",bonus);
jamesheavey 103:f9f69944a850 416 lcd.printString(buffer2,WIDTH/2 + 10,3);
jamesheavey 103:f9f69944a850 417 lcd.printString(" BONUS:",2,3);
jamesheavey 99:d8f1570faa05 418
jamesheavey 103:f9f69944a850 419 lcd.printString(" VICTORY! ",2,0);
jamesheavey 64:c3426b417ad9 420 lcd.printString(" CONT = START ",0,4); // print score here
jamesheavey 100:9842813b7131 421 lcd.printString(" MENU = BACK ",0,5);
jamesheavey 64:c3426b417ad9 422 lcd.refresh();
jamesheavey 91:c01a736fb0d9 423
jamesheavey 64:c3426b417ad9 424 pad.tone(2500.0,0.1);
jamesheavey 64:c3426b417ad9 425
jamesheavey 64:c3426b417ad9 426 lcd.refresh();
jamesheavey 91:c01a736fb0d9 427
jamesheavey 64:c3426b417ad9 428 wait(0.4);
jamesheavey 91:c01a736fb0d9 429
jamesheavey 43:81d22f68dfae 430 if (pad.check_event(Gamepad::START_PRESSED)) {
jamesheavey 99:d8f1570faa05 431 breakout.set_prev_score(breakout.get_score() + bonus);
jamesheavey 99:d8f1570faa05 432 number_of_frames = 0;
jamesheavey 91:c01a736fb0d9 433 main_game(tilt,sens);
jamesheavey 91:c01a736fb0d9 434 } else if (pad.check_event(Gamepad::BACK_PRESSED)) {
jamesheavey 64:c3426b417ad9 435 breakout.set_prev_score(0);
jamesheavey 99:d8f1570faa05 436 number_of_frames = 0;
jamesheavey 49:31eb7807dbd3 437 title_screen();
jamesheavey 43:81d22f68dfae 438 }
jamesheavey 13:418a71b24b37 439 }
jamesheavey 13:418a71b24b37 440 }
jamesheavey 13:418a71b24b37 441
jamesheavey 91:c01a736fb0d9 442 void flash_screen(N5110 &lcd) // move to engine
jamesheavey 91:c01a736fb0d9 443 {
jamesheavey 58:a159cd976aca 444 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 445 wait(0.1);
jamesheavey 58:a159cd976aca 446 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 447 wait(0.1);
jamesheavey 58:a159cd976aca 448 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 449 wait(0.1);
jamesheavey 47:1d1a827be81b 450 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 451 wait(0.1);
jamesheavey 58:a159cd976aca 452 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 453 wait(0.1);
jamesheavey 58:a159cd976aca 454 lcd.setBrightness(1);
jamesheavey 58:a159cd976aca 455 wait(0.1);
jamesheavey 58:a159cd976aca 456 lcd.setBrightness(0);
jamesheavey 58:a159cd976aca 457 wait(0.1);
jamesheavey 58:a159cd976aca 458 lcd.setBrightness(1);
jamesheavey 59:fdc05d5778a6 459 }
jamesheavey 59:fdc05d5778a6 460
jamesheavey 91:c01a736fb0d9 461 void countdown()
jamesheavey 91:c01a736fb0d9 462 {
jamesheavey 59:fdc05d5778a6 463 lcd.clear();
jamesheavey 59:fdc05d5778a6 464 three.render(lcd, 0, 0); // render the 3
jamesheavey 59:fdc05d5778a6 465 lcd.refresh();
jamesheavey 59:fdc05d5778a6 466 pad.tone(500.0,0.5);
jamesheavey 59:fdc05d5778a6 467 wait(1); // wait 1 second
jamesheavey 91:c01a736fb0d9 468
jamesheavey 59:fdc05d5778a6 469 lcd.clear();
jamesheavey 59:fdc05d5778a6 470 two.render(lcd, 0, 0); // render 2
jamesheavey 59:fdc05d5778a6 471 lcd.refresh();
jamesheavey 59:fdc05d5778a6 472 pad.tone(500.0,0.5);
jamesheavey 59:fdc05d5778a6 473 wait(1); // wait 1 second
jamesheavey 91:c01a736fb0d9 474
jamesheavey 59:fdc05d5778a6 475 lcd.clear();
jamesheavey 59:fdc05d5778a6 476 one.render(lcd, 0, 0); // render 1
jamesheavey 59:fdc05d5778a6 477 lcd.refresh();
jamesheavey 59:fdc05d5778a6 478 pad.tone(1000.0,1);
jamesheavey 59:fdc05d5778a6 479 wait(1); // wait 1 second
jamesheavey 98:2ce2c666266b 480 }
jamesheavey 101:0df767523dbd 481
jamesheavey 98:2ce2c666266b 482 void save_hi_score(int hi_score)
jamesheavey 98:2ce2c666266b 483 {
jamesheavey 98:2ce2c666266b 484 serial.baud(115200); // full-speed!
jamesheavey 98:2ce2c666266b 485 FILE *fp; // this is our file pointer
jamesheavey 98:2ce2c666266b 486 fp = fopen("/sd/hiscore.txt", "w");
jamesheavey 98:2ce2c666266b 487 if (fp == NULL) { // if it can't open the file then print error message
jamesheavey 98:2ce2c666266b 488 serial.printf("Error! Unable to open file!\n");
jamesheavey 98:2ce2c666266b 489 } else { // opened file so can write
jamesheavey 98:2ce2c666266b 490 serial.printf("Writing to file....");
jamesheavey 98:2ce2c666266b 491 fprintf(fp, "%d",hi_score); // ensure data type matches
jamesheavey 98:2ce2c666266b 492 serial.printf("Done.\n");
jamesheavey 98:2ce2c666266b 493 fclose(fp); // ensure you close the file after writing
jamesheavey 98:2ce2c666266b 494 }
jamesheavey 98:2ce2c666266b 495 }
jamesheavey 98:2ce2c666266b 496
jamesheavey 98:2ce2c666266b 497 int get_hi_score()
jamesheavey 98:2ce2c666266b 498 {
jamesheavey 101:0df767523dbd 499 serial.baud(115200);
jamesheavey 101:0df767523dbd 500 FILE *fp;
jamesheavey 98:2ce2c666266b 501 fp = fopen("/sd/hiscore.txt", "r");
jamesheavey 98:2ce2c666266b 502 int stored_hi_score = NULL;
jamesheavey 98:2ce2c666266b 503 if (fp == NULL) { // if it can't open the file then print error message
jamesheavey 98:2ce2c666266b 504 serial.printf("Error! Unable to open file!\n");
jamesheavey 98:2ce2c666266b 505 } else { // opened file so can write
jamesheavey 98:2ce2c666266b 506 fscanf(fp, "%d",&stored_hi_score); // ensure data type matches - note address operator (&)
jamesheavey 101:0df767523dbd 507 serial.printf("Read %d from file.\n",stored_hi_score);
jamesheavey 98:2ce2c666266b 508 fclose(fp); // ensure you close the file after reading
jamesheavey 98:2ce2c666266b 509 }
jamesheavey 98:2ce2c666266b 510 return stored_hi_score;
jamesheavey 99:d8f1570faa05 511 }
jamesheavey 101:0df767523dbd 512
jamesheavey 101:0df767523dbd 513 bool compare_to_hi_score(int score)
jamesheavey 101:0df767523dbd 514 {
jamesheavey 101:0df767523dbd 515 if (score >= get_hi_score()) {
jamesheavey 101:0df767523dbd 516 printf("hi score! \n");
jamesheavey 101:0df767523dbd 517 save_hi_score(score);
jamesheavey 101:0df767523dbd 518 return true;
jamesheavey 101:0df767523dbd 519 }
jamesheavey 101:0df767523dbd 520 else {
jamesheavey 101:0df767523dbd 521 return false;
jamesheavey 101:0df767523dbd 522 }
jamesheavey 101:0df767523dbd 523 }
jamesheavey 103:f9f69944a850 524
jamesheavey 103:f9f69944a850 525 void print_hi_score(int col,int row)
jamesheavey 103:f9f69944a850 526 {
jamesheavey 103:f9f69944a850 527 char buffer[14];
jamesheavey 103:f9f69944a850 528 sprintf(buffer,"%2d",get_hi_score());
jamesheavey 103:f9f69944a850 529 lcd.printString(buffer,col,row);
jamesheavey 103:f9f69944a850 530
jamesheavey 103:f9f69944a850 531 }
jamesheavey 103:f9f69944a850 532
jamesheavey 103:f9f69944a850 533 void flash_hi_score()
jamesheavey 103:f9f69944a850 534 {
jamesheavey 103:f9f69944a850 535 lcd.clear();
jamesheavey 103:f9f69944a850 536 lcd.printString(" NEW ",0,2);
jamesheavey 103:f9f69944a850 537 lcd.printString(" HI-SCORE! ",0,3);
jamesheavey 103:f9f69944a850 538 lcd.refresh();
jamesheavey 103:f9f69944a850 539
jamesheavey 103:f9f69944a850 540 pad.tone(2500.0,0.2);
jamesheavey 103:f9f69944a850 541 wait(0.2);
jamesheavey 103:f9f69944a850 542
jamesheavey 103:f9f69944a850 543 pad.tone(4000.0,0.4);
jamesheavey 103:f9f69944a850 544 wait(1);
jamesheavey 103:f9f69944a850 545
jamesheavey 103:f9f69944a850 546 lcd.clear();
jamesheavey 103:f9f69944a850 547 print_hi_score(30,3);
jamesheavey 103:f9f69944a850 548 lcd.refresh();
jamesheavey 103:f9f69944a850 549
jamesheavey 103:f9f69944a850 550 pad.tone(2500.0,0.2);
jamesheavey 103:f9f69944a850 551 wait(0.2);
jamesheavey 103:f9f69944a850 552
jamesheavey 103:f9f69944a850 553 pad.tone(4000.0,0.4);
jamesheavey 103:f9f69944a850 554 wait(1);
jamesheavey 103:f9f69944a850 555
jamesheavey 103:f9f69944a850 556 lcd.clear();
jamesheavey 103:f9f69944a850 557 lcd.printString(" NEW ",0,2);
jamesheavey 103:f9f69944a850 558 lcd.printString(" HI-SCORE! ",0,3);
jamesheavey 103:f9f69944a850 559 lcd.refresh();
jamesheavey 103:f9f69944a850 560
jamesheavey 103:f9f69944a850 561 pad.tone(2500.0,0.2);
jamesheavey 103:f9f69944a850 562 wait(0.2);
jamesheavey 103:f9f69944a850 563
jamesheavey 103:f9f69944a850 564 pad.tone(4000.0,0.4);
jamesheavey 103:f9f69944a850 565 wait(1);
jamesheavey 103:f9f69944a850 566
jamesheavey 103:f9f69944a850 567 lcd.clear();
jamesheavey 103:f9f69944a850 568 print_hi_score(30,3);
jamesheavey 103:f9f69944a850 569 lcd.refresh();
jamesheavey 103:f9f69944a850 570
jamesheavey 103:f9f69944a850 571 pad.tone(2500.0,0.2);
jamesheavey 103:f9f69944a850 572 wait(0.2);
jamesheavey 103:f9f69944a850 573
jamesheavey 103:f9f69944a850 574 pad.tone(4000.0,0.4);
jamesheavey 103:f9f69944a850 575 wait(1);
jamesheavey 103:f9f69944a850 576 }