James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 09:10:24 2019 +0000
Revision:
134:48c4b408868c
Parent:
133:05bb8de3c630
Child:
135:888ae932cd70
inputs no longer carry through menus causing incorrect sequences

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