James Heavey
/
EL17JH
FINAL VERSION
main.cpp@107:1b21b028d764, 2019-05-08 (annotated)
- Committer:
- jamesheavey
- Date:
- Wed May 08 01:02:28 2019 +0000
- Revision:
- 107:1b21b028d764
- Parent:
- 106:a2957bab0c16
- Child:
- 108:91da12db6f26
cp
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 | 106:a2957bab0c16 | 86 | breakout.set_mult_one(); |
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 | 83:645cdbd79c21 | 283 | if (pad.check_event(Gamepad::START_PRESSED) == true) { // if BACK pressed, toggle pause |
jamesheavey | 58:a159cd976aca | 284 | pause = !pause; |
jamesheavey | 58:a159cd976aca | 285 | } |
jamesheavey | 91:c01a736fb0d9 | 286 | |
jamesheavey | 59:fdc05d5778a6 | 287 | while (pause == true) { // if pause is true, display pause screen |
jamesheavey | 58:a159cd976aca | 288 | lcd.clear(); |
jamesheavey | 83:645cdbd79c21 | 289 | lcd.printString(" PAUSED ",0,1); |
jamesheavey | 83:645cdbd79c21 | 290 | lcd.printString(" START = PLAY",0,3); |
jamesheavey | 83:645cdbd79c21 | 291 | lcd.printString(" BACK = MENU",0,4); |
jamesheavey | 58:a159cd976aca | 292 | lcd.refresh(); |
jamesheavey | 99:d8f1570faa05 | 293 | if (pad.check_event(Gamepad::START_PRESSED) == true) { // if START pressed, toggle pause, leaving pause screen |
jamesheavey | 58:a159cd976aca | 294 | pause = !pause; |
jamesheavey | 58:a159cd976aca | 295 | } |
jamesheavey | 99:d8f1570faa05 | 296 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { // if BACK pressed, return to the title screen |
jamesheavey | 99:d8f1570faa05 | 297 | breakout.set_mult_one(); // reset multiplier |
jamesheavey | 99:d8f1570faa05 | 298 | breakout.set_prev_score(0); |
jamesheavey | 99:d8f1570faa05 | 299 | breakout.reset_game(); //reset game to initial positions |
jamesheavey | 83:645cdbd79c21 | 300 | title_screen(); |
jamesheavey | 83:645cdbd79c21 | 301 | } |
jamesheavey | 58:a159cd976aca | 302 | } |
jamesheavey | 91:c01a736fb0d9 | 303 | |
jamesheavey | 79:70510b0102af | 304 | if (breakout.get_lives() == 0) { // when all lives lost, enter loss screen |
jamesheavey | 59:fdc05d5778a6 | 305 | 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 | 306 | breakout.set_mult_one(); // reset multiplier |
jamesheavey | 99:d8f1570faa05 | 307 | breakout.reset_game(); // return game to initial positions |
jamesheavey | 99:d8f1570faa05 | 308 | loss_screen(); |
jamesheavey | 58:a159cd976aca | 309 | } |
jamesheavey | 91:c01a736fb0d9 | 310 | |
jamesheavey | 59:fdc05d5778a6 | 311 | if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen |
jamesheavey | 99:d8f1570faa05 | 312 | breakout.reset_game(); // return game to initial positions |
jamesheavey | 99:d8f1570faa05 | 313 | breakout.inc_mult(); // increment multiplier |
jamesheavey | 58:a159cd976aca | 314 | victory_screen(); |
jamesheavey | 58:a159cd976aca | 315 | } |
jamesheavey | 99:d8f1570faa05 | 316 | number_of_frames ++; // track the number of frames passed to add to the score (inversely proportional) |
jamesheavey | 58:a159cd976aca | 317 | wait(1.0f/fps); |
jamesheavey | 58:a159cd976aca | 318 | } |
jamesheavey | 50:3da51f34e253 | 319 | } |
jamesheavey | 16:1761dfe801af | 320 | |
jamesheavey | 99:d8f1570faa05 | 321 | void loss_screen() // loss screen when lives of paddle == 0 |
jamesheavey | 64:c3426b417ad9 | 322 | { |
jamesheavey | 107:1b21b028d764 | 323 | if (compare_to_hi_score(breakout.get_score()) == true) { // little hi-score sequence if current score is higher than prev |
jamesheavey | 106:a2957bab0c16 | 324 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 325 | lcd.printString(" NEW ",0,2); |
jamesheavey | 106:a2957bab0c16 | 326 | lcd.printString(" HI-SCORE! ",0,3); |
jamesheavey | 106:a2957bab0c16 | 327 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 328 | |
jamesheavey | 106:a2957bab0c16 | 329 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 330 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 331 | |
jamesheavey | 106:a2957bab0c16 | 332 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 333 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 334 | |
jamesheavey | 106:a2957bab0c16 | 335 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 336 | print_hi_score(30,3); |
jamesheavey | 106:a2957bab0c16 | 337 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 338 | |
jamesheavey | 106:a2957bab0c16 | 339 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 340 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 341 | |
jamesheavey | 106:a2957bab0c16 | 342 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 343 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 344 | |
jamesheavey | 106:a2957bab0c16 | 345 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 346 | lcd.printString(" NEW ",0,2); |
jamesheavey | 106:a2957bab0c16 | 347 | lcd.printString(" HI-SCORE! ",0,3); |
jamesheavey | 106:a2957bab0c16 | 348 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 349 | |
jamesheavey | 106:a2957bab0c16 | 350 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 351 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 352 | |
jamesheavey | 106:a2957bab0c16 | 353 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 354 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 355 | |
jamesheavey | 106:a2957bab0c16 | 356 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 357 | print_hi_score(30,3); |
jamesheavey | 106:a2957bab0c16 | 358 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 359 | |
jamesheavey | 106:a2957bab0c16 | 360 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 361 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 362 | |
jamesheavey | 106:a2957bab0c16 | 363 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 364 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 365 | } |
jamesheavey | 106:a2957bab0c16 | 366 | |
jamesheavey | 15:b867a6620f96 | 367 | lcd.clear(); |
jamesheavey | 64:c3426b417ad9 | 368 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 369 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 370 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 64:c3426b417ad9 | 371 | lcd.printString(" RESTART? ",2,1); |
jamesheavey | 72:7254d2a8a1cd | 372 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 91:c01a736fb0d9 | 373 | lcd.refresh(); |
jamesheavey | 15:b867a6620f96 | 374 | pad.tone(750.0,0.3); |
jamesheavey | 15:b867a6620f96 | 375 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 376 | |
jamesheavey | 15:b867a6620f96 | 377 | pad.tone(300.0,0.3); |
jamesheavey | 15:b867a6620f96 | 378 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 379 | |
jamesheavey | 107:1b21b028d764 | 380 | while (pad.check_event(Gamepad::START_PRESSED) == false) { // flashes the score, waits for START to return to title screen |
jamesheavey | 10:9f445faa892c | 381 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 382 | |
jamesheavey | 107:1b21b028d764 | 383 | lcd.printString(" RESTART? ",2,1); |
jamesheavey | 64:c3426b417ad9 | 384 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 10:9f445faa892c | 385 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 386 | |
jamesheavey | 64:c3426b417ad9 | 387 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 388 | |
jamesheavey | 64:c3426b417ad9 | 389 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 390 | |
jamesheavey | 64:c3426b417ad9 | 391 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 392 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 393 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 64:c3426b417ad9 | 394 | lcd.printString(" RESTART? ",2,1); //print score here |
jamesheavey | 64:c3426b417ad9 | 395 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 64:c3426b417ad9 | 396 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 397 | |
jamesheavey | 64:c3426b417ad9 | 398 | wait(0.4); |
jamesheavey | 10:9f445faa892c | 399 | } |
jamesheavey | 107:1b21b028d764 | 400 | breakout.set_prev_score(0); // resets prev score to 0 |
jamesheavey | 49:31eb7807dbd3 | 401 | title_screen(); |
jamesheavey | 10:9f445faa892c | 402 | } |
jamesheavey | 10:9f445faa892c | 403 | |
jamesheavey | 91:c01a736fb0d9 | 404 | void victory_screen() |
jamesheavey | 64:c3426b417ad9 | 405 | { |
jamesheavey | 99:d8f1570faa05 | 406 | int bonus = NULL; |
jamesheavey | 99:d8f1570faa05 | 407 | |
jamesheavey | 99:d8f1570faa05 | 408 | if (breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720 > 0) { // beat within 90 seconds and you get a bonus |
jamesheavey | 99:d8f1570faa05 | 409 | bonus = breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720; |
jamesheavey | 99:d8f1570faa05 | 410 | } |
jamesheavey | 99:d8f1570faa05 | 411 | else { |
jamesheavey | 99:d8f1570faa05 | 412 | bonus = 0; |
jamesheavey | 99:d8f1570faa05 | 413 | } |
jamesheavey | 99:d8f1570faa05 | 414 | |
jamesheavey | 102:4946a6b47c78 | 415 | if (compare_to_hi_score(bonus+breakout.get_score()) == true) { |
jamesheavey | 106:a2957bab0c16 | 416 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 417 | lcd.printString(" NEW ",0,2); |
jamesheavey | 106:a2957bab0c16 | 418 | lcd.printString(" HI-SCORE! ",0,3); |
jamesheavey | 106:a2957bab0c16 | 419 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 420 | |
jamesheavey | 106:a2957bab0c16 | 421 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 422 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 423 | |
jamesheavey | 106:a2957bab0c16 | 424 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 425 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 426 | |
jamesheavey | 106:a2957bab0c16 | 427 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 428 | print_hi_score(30,3); |
jamesheavey | 106:a2957bab0c16 | 429 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 430 | |
jamesheavey | 106:a2957bab0c16 | 431 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 432 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 433 | |
jamesheavey | 106:a2957bab0c16 | 434 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 435 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 436 | |
jamesheavey | 106:a2957bab0c16 | 437 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 438 | lcd.printString(" NEW ",0,2); |
jamesheavey | 106:a2957bab0c16 | 439 | lcd.printString(" HI-SCORE! ",0,3); |
jamesheavey | 106:a2957bab0c16 | 440 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 441 | |
jamesheavey | 106:a2957bab0c16 | 442 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 443 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 444 | |
jamesheavey | 106:a2957bab0c16 | 445 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 446 | wait(1); |
jamesheavey | 106:a2957bab0c16 | 447 | |
jamesheavey | 106:a2957bab0c16 | 448 | lcd.clear(); |
jamesheavey | 106:a2957bab0c16 | 449 | print_hi_score(30,3); |
jamesheavey | 106:a2957bab0c16 | 450 | lcd.refresh(); |
jamesheavey | 106:a2957bab0c16 | 451 | |
jamesheavey | 106:a2957bab0c16 | 452 | pad.tone(2500.0,0.2); |
jamesheavey | 106:a2957bab0c16 | 453 | wait(0.2); |
jamesheavey | 106:a2957bab0c16 | 454 | |
jamesheavey | 106:a2957bab0c16 | 455 | pad.tone(4000.0,0.4); |
jamesheavey | 106:a2957bab0c16 | 456 | wait(1); |
jamesheavey | 102:4946a6b47c78 | 457 | } |
jamesheavey | 102:4946a6b47c78 | 458 | |
jamesheavey | 15:b867a6620f96 | 459 | lcd.clear(); |
jamesheavey | 99:d8f1570faa05 | 460 | |
jamesheavey | 64:c3426b417ad9 | 461 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 462 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 463 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 99:d8f1570faa05 | 464 | |
jamesheavey | 99:d8f1570faa05 | 465 | char buffer2[14]; |
jamesheavey | 99:d8f1570faa05 | 466 | sprintf(buffer2,"%2d",bonus); |
jamesheavey | 100:9842813b7131 | 467 | lcd.printString(buffer2,WIDTH/2 + 10,3); |
jamesheavey | 103:f9f69944a850 | 468 | lcd.printString(" BONUS: ",2,3); |
jamesheavey | 99:d8f1570faa05 | 469 | |
jamesheavey | 103:f9f69944a850 | 470 | lcd.printString(" VICTORY! ",2,0); |
jamesheavey | 47:1d1a827be81b | 471 | lcd.printString(" CONT = START ",0,4); // print score here |
jamesheavey | 100:9842813b7131 | 472 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 15:b867a6620f96 | 473 | lcd.refresh(); |
jamesheavey | 22:fa2e0b58043a | 474 | pad.tone(2500.0,0.1); |
jamesheavey | 22:fa2e0b58043a | 475 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 476 | |
jamesheavey | 22:fa2e0b58043a | 477 | pad.tone(2500.0,0.1); |
jamesheavey | 22:fa2e0b58043a | 478 | wait(0.2); |
jamesheavey | 91:c01a736fb0d9 | 479 | |
jamesheavey | 15:b867a6620f96 | 480 | pad.tone(4000.0,0.6); |
jamesheavey | 15:b867a6620f96 | 481 | wait(0.6); |
jamesheavey | 99:d8f1570faa05 | 482 | |
jamesheavey | 91:c01a736fb0d9 | 483 | while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) { |
jamesheavey | 13:418a71b24b37 | 484 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 485 | |
jamesheavey | 103:f9f69944a850 | 486 | lcd.printString(" VICTORY! ",2,0); |
jamesheavey | 47:1d1a827be81b | 487 | lcd.printString(" CONT = START ",0,4); |
jamesheavey | 100:9842813b7131 | 488 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 43:81d22f68dfae | 489 | |
jamesheavey | 13:418a71b24b37 | 490 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 491 | |
jamesheavey | 64:c3426b417ad9 | 492 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 493 | |
jamesheavey | 64:c3426b417ad9 | 494 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 495 | |
jamesheavey | 99:d8f1570faa05 | 496 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 497 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 498 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 99:d8f1570faa05 | 499 | |
jamesheavey | 99:d8f1570faa05 | 500 | char buffer2[14]; |
jamesheavey | 99:d8f1570faa05 | 501 | sprintf(buffer2,"%2d",bonus); |
jamesheavey | 103:f9f69944a850 | 502 | lcd.printString(buffer2,WIDTH/2 + 10,3); |
jamesheavey | 103:f9f69944a850 | 503 | lcd.printString(" BONUS:",2,3); |
jamesheavey | 99:d8f1570faa05 | 504 | |
jamesheavey | 103:f9f69944a850 | 505 | lcd.printString(" VICTORY! ",2,0); |
jamesheavey | 107:1b21b028d764 | 506 | lcd.printString(" CONT = START ",0,4); |
jamesheavey | 100:9842813b7131 | 507 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 64:c3426b417ad9 | 508 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 509 | |
jamesheavey | 64:c3426b417ad9 | 510 | pad.tone(2500.0,0.1); |
jamesheavey | 64:c3426b417ad9 | 511 | |
jamesheavey | 64:c3426b417ad9 | 512 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 513 | |
jamesheavey | 64:c3426b417ad9 | 514 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 515 | |
jamesheavey | 43:81d22f68dfae | 516 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jamesheavey | 99:d8f1570faa05 | 517 | breakout.set_prev_score(breakout.get_score() + bonus); |
jamesheavey | 99:d8f1570faa05 | 518 | number_of_frames = 0; |
jamesheavey | 106:a2957bab0c16 | 519 | main_game(); |
jamesheavey | 91:c01a736fb0d9 | 520 | } else if (pad.check_event(Gamepad::BACK_PRESSED)) { |
jamesheavey | 64:c3426b417ad9 | 521 | breakout.set_prev_score(0); |
jamesheavey | 99:d8f1570faa05 | 522 | number_of_frames = 0; |
jamesheavey | 49:31eb7807dbd3 | 523 | title_screen(); |
jamesheavey | 43:81d22f68dfae | 524 | } |
jamesheavey | 13:418a71b24b37 | 525 | } |
jamesheavey | 13:418a71b24b37 | 526 | } |
jamesheavey | 13:418a71b24b37 | 527 | |
jamesheavey | 91:c01a736fb0d9 | 528 | void flash_screen(N5110 &lcd) // move to engine |
jamesheavey | 91:c01a736fb0d9 | 529 | { |
jamesheavey | 58:a159cd976aca | 530 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 531 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 532 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 533 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 534 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 535 | wait(0.1); |
jamesheavey | 47:1d1a827be81b | 536 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 537 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 538 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 539 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 540 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 541 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 542 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 543 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 544 | lcd.setBrightness(1); |
jamesheavey | 59:fdc05d5778a6 | 545 | } |
jamesheavey | 59:fdc05d5778a6 | 546 | |
jamesheavey | 91:c01a736fb0d9 | 547 | void countdown() |
jamesheavey | 91:c01a736fb0d9 | 548 | { |
jamesheavey | 104:c2d49c4f3e06 | 549 | lcd.setBrightness(1); // stops the game from dimming (bug) |
jamesheavey | 104:c2d49c4f3e06 | 550 | |
jamesheavey | 59:fdc05d5778a6 | 551 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 552 | three.render(lcd, 0, 0); // render the 3 |
jamesheavey | 59:fdc05d5778a6 | 553 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 554 | pad.tone(500.0,0.5); |
jamesheavey | 59:fdc05d5778a6 | 555 | wait(1); // wait 1 second |
jamesheavey | 106:a2957bab0c16 | 556 | |
jamesheavey | 106:a2957bab0c16 | 557 | lcd.setBrightness(1); |
jamesheavey | 106:a2957bab0c16 | 558 | |
jamesheavey | 59:fdc05d5778a6 | 559 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 560 | two.render(lcd, 0, 0); // render 2 |
jamesheavey | 59:fdc05d5778a6 | 561 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 562 | pad.tone(500.0,0.5); |
jamesheavey | 59:fdc05d5778a6 | 563 | wait(1); // wait 1 second |
jamesheavey | 106:a2957bab0c16 | 564 | |
jamesheavey | 106:a2957bab0c16 | 565 | lcd.setBrightness(1); |
jamesheavey | 106:a2957bab0c16 | 566 | |
jamesheavey | 59:fdc05d5778a6 | 567 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 568 | one.render(lcd, 0, 0); // render 1 |
jamesheavey | 59:fdc05d5778a6 | 569 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 570 | pad.tone(1000.0,1); |
jamesheavey | 59:fdc05d5778a6 | 571 | wait(1); // wait 1 second |
jamesheavey | 98:2ce2c666266b | 572 | } |
jamesheavey | 101:0df767523dbd | 573 | |
jamesheavey | 98:2ce2c666266b | 574 | void save_hi_score(int hi_score) |
jamesheavey | 98:2ce2c666266b | 575 | { |
jamesheavey | 105:4e7585d8e5e2 | 576 | serial.baud(115200); // max speed |
jamesheavey | 105:4e7585d8e5e2 | 577 | FILE *fp; // file pointer |
jamesheavey | 107:1b21b028d764 | 578 | fp = fopen("/sd/highscore.txt", "w"); |
jamesheavey | 98:2ce2c666266b | 579 | if (fp == NULL) { // if it can't open the file then print error message |
jamesheavey | 98:2ce2c666266b | 580 | serial.printf("Error! Unable to open file!\n"); |
jamesheavey | 98:2ce2c666266b | 581 | } else { // opened file so can write |
jamesheavey | 98:2ce2c666266b | 582 | serial.printf("Writing to file...."); |
jamesheavey | 105:4e7585d8e5e2 | 583 | fprintf(fp, "%d",hi_score); |
jamesheavey | 98:2ce2c666266b | 584 | serial.printf("Done.\n"); |
jamesheavey | 107:1b21b028d764 | 585 | fclose(fp); // close the file after writing |
jamesheavey | 98:2ce2c666266b | 586 | } |
jamesheavey | 98:2ce2c666266b | 587 | } |
jamesheavey | 98:2ce2c666266b | 588 | |
jamesheavey | 98:2ce2c666266b | 589 | int get_hi_score() |
jamesheavey | 98:2ce2c666266b | 590 | { |
jamesheavey | 105:4e7585d8e5e2 | 591 | serial.baud(115200); // max speed |
jamesheavey | 101:0df767523dbd | 592 | FILE *fp; |
jamesheavey | 107:1b21b028d764 | 593 | fp = fopen("/sd/highscore.txt", "r"); |
jamesheavey | 98:2ce2c666266b | 594 | int stored_hi_score = NULL; |
jamesheavey | 98:2ce2c666266b | 595 | if (fp == NULL) { // if it can't open the file then print error message |
jamesheavey | 98:2ce2c666266b | 596 | serial.printf("Error! Unable to open file!\n"); |
jamesheavey | 98:2ce2c666266b | 597 | } else { // opened file so can write |
jamesheavey | 105:4e7585d8e5e2 | 598 | fscanf(fp, "%d",&stored_hi_score); |
jamesheavey | 101:0df767523dbd | 599 | serial.printf("Read %d from file.\n",stored_hi_score); |
jamesheavey | 107:1b21b028d764 | 600 | fclose(fp); // close the file after reading |
jamesheavey | 98:2ce2c666266b | 601 | } |
jamesheavey | 107:1b21b028d764 | 602 | return stored_hi_score; // returns the stored hi score |
jamesheavey | 99:d8f1570faa05 | 603 | } |
jamesheavey | 101:0df767523dbd | 604 | |
jamesheavey | 107:1b21b028d764 | 605 | bool compare_to_hi_score(int score) // returns true if new score higher than current stored hi score |
jamesheavey | 101:0df767523dbd | 606 | { |
jamesheavey | 101:0df767523dbd | 607 | if (score >= get_hi_score()) { |
jamesheavey | 107:1b21b028d764 | 608 | //printf("hi score! \n"); |
jamesheavey | 107:1b21b028d764 | 609 | save_hi_score(score); // writes the new score to the SD card |
jamesheavey | 101:0df767523dbd | 610 | return true; |
jamesheavey | 101:0df767523dbd | 611 | } |
jamesheavey | 101:0df767523dbd | 612 | else { |
jamesheavey | 107:1b21b028d764 | 613 | return false; |
jamesheavey | 101:0df767523dbd | 614 | } |
jamesheavey | 101:0df767523dbd | 615 | } |
jamesheavey | 103:f9f69944a850 | 616 | |
jamesheavey | 103:f9f69944a850 | 617 | void print_hi_score(int col,int row) |
jamesheavey | 103:f9f69944a850 | 618 | { |
jamesheavey | 107:1b21b028d764 | 619 | char buffer[14]; // creates buffer |
jamesheavey | 107:1b21b028d764 | 620 | sprintf(buffer,"%2d",get_hi_score()); // puts hi-score in buffer |
jamesheavey | 107:1b21b028d764 | 621 | lcd.printString(buffer,col,row); // prints buffer to the screen |
jamesheavey | 103:f9f69944a850 | 622 | |
jamesheavey | 103:f9f69944a850 | 623 | } |