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