James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 08:45:03 2019 +0000
Revision:
132:a98cfea2d292
Parent:
128:b57930585692
Child:
133:05bb8de3c630
try adding a reset button flag func to game pad to deal with button bounce

Who changed what in which revision?

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