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