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