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