James Heavey
/
EL17JH
FINAL VERSION
main.cpp@101:0df767523dbd, 2019-05-07 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue May 07 17:14:21 2019 +0000
- Revision:
- 101:0df767523dbd
- Parent:
- 100:9842813b7131
- Child:
- 102:4946a6b47c78
SD CARD WORKS?
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 | 72:7254d2a8a1cd | 37 | void main_game(bool tilt,float sens); |
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 | 47:1d1a827be81b | 42 | |
jamesheavey | 95:09550aeca8b9 | 43 | Bitmap breakwhite(breakwhite_data, 48, 84); // assign the title screen sprites |
jamesheavey | 95:09550aeca8b9 | 44 | Bitmap breakblack(breakblack_data, 48, 84); |
jamesheavey | 95:09550aeca8b9 | 45 | Bitmap arrowup(arrowup_data, 5, 7); // assign the arrow up sprite data |
jamesheavey | 95:09550aeca8b9 | 46 | Bitmap arrowdown(arrowdown_data, 5, 7); // assign the arrow down sprite data |
jamesheavey | 97:6c76526b5298 | 47 | Bitmap three(three_data, 48, 84); // assign the 3 sprite data |
jamesheavey | 97:6c76526b5298 | 48 | Bitmap two(two_data, 48, 84); // assign the 2 sprite data |
jamesheavey | 97:6c76526b5298 | 49 | Bitmap one(one_data, 48, 84); // assign the 1 sprite data |
jamesheavey | 95:09550aeca8b9 | 50 | |
jamesheavey | 47:1d1a827be81b | 51 | bool tilt = false; |
jamesheavey | 72:7254d2a8a1cd | 52 | float sens = 0.5; // default sens |
jamesheavey | 99:d8f1570faa05 | 53 | int number_of_frames = 0; |
jamesheavey | 0:7d4d2023ed9c | 54 | |
jamesheavey | 0:7d4d2023ed9c | 55 | ///////////// functions //////////////// |
jamesheavey | 79:70510b0102af | 56 | |
jamesheavey | 0:7d4d2023ed9c | 57 | int main() |
jamesheavey | 91:c01a736fb0d9 | 58 | { |
jamesheavey | 58:a159cd976aca | 59 | init(); // initialise the gamepad |
jamesheavey | 58:a159cd976aca | 60 | title_screen(); // run the title screen |
jamesheavey | 0:7d4d2023ed9c | 61 | } |
jamesheavey | 0:7d4d2023ed9c | 62 | |
jamesheavey | 0:7d4d2023ed9c | 63 | // initialies all classes and libraries |
jamesheavey | 56:f9e586348e0b | 64 | void init() |
jamesheavey | 0:7d4d2023ed9c | 65 | { |
jamesheavey | 91:c01a736fb0d9 | 66 | // need to initialise LCD and Gamepad |
jamesheavey | 0:7d4d2023ed9c | 67 | lcd.init(); |
jamesheavey | 0:7d4d2023ed9c | 68 | pad.init(); |
jamesheavey | 72:7254d2a8a1cd | 69 | breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); // init the objects |
jamesheavey | 0:7d4d2023ed9c | 70 | } |
jamesheavey | 0:7d4d2023ed9c | 71 | |
jamesheavey | 0:7d4d2023ed9c | 72 | // this function draws each frame on the LCD |
jamesheavey | 0:7d4d2023ed9c | 73 | void render() |
jamesheavey | 0:7d4d2023ed9c | 74 | { |
jamesheavey | 0:7d4d2023ed9c | 75 | // clear screen, re-draw and refresh |
jamesheavey | 91:c01a736fb0d9 | 76 | lcd.clear(); |
jamesheavey | 26:25eea19c5fbe | 77 | breakout.draw(lcd); |
jamesheavey | 0:7d4d2023ed9c | 78 | lcd.refresh(); |
jamesheavey | 0:7d4d2023ed9c | 79 | } |
jamesheavey | 0:7d4d2023ed9c | 80 | |
jamesheavey | 91:c01a736fb0d9 | 81 | void title_screen() |
jamesheavey | 91:c01a736fb0d9 | 82 | { |
jamesheavey | 58:a159cd976aca | 83 | tilt = false; // reset the tilt variable so that on start, joystick is default |
jamesheavey | 91:c01a736fb0d9 | 84 | |
jamesheavey | 58:a159cd976aca | 85 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 86 | |
jamesheavey | 58:a159cd976aca | 87 | breakblack.render(lcd, 0, 0); // render the first frame |
jamesheavey | 58:a159cd976aca | 88 | lcd.refresh(); |
jamesheavey | 58:a159cd976aca | 89 | pad.leds_off(); |
jamesheavey | 58:a159cd976aca | 90 | wait(0.5); |
jamesheavey | 91:c01a736fb0d9 | 91 | |
jamesheavey | 58:a159cd976aca | 92 | while (pad.check_event(Gamepad::START_PRESSED) == false) { // while start is not pressed alternate sprites |
jamesheavey | 91:c01a736fb0d9 | 93 | |
jamesheavey | 58:a159cd976aca | 94 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 95 | |
jamesheavey | 58:a159cd976aca | 96 | breakwhite.render(lcd, 0, 0); |
jamesheavey | 58:a159cd976aca | 97 | lcd.refresh(); |
jamesheavey | 58:a159cd976aca | 98 | pad.leds_on(); |
jamesheavey | 58:a159cd976aca | 99 | wait(0.5); |
jamesheavey | 91:c01a736fb0d9 | 100 | |
jamesheavey | 58:a159cd976aca | 101 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 102 | |
jamesheavey | 58:a159cd976aca | 103 | breakblack.render(lcd, 0, 0); |
jamesheavey | 58:a159cd976aca | 104 | lcd.refresh(); |
jamesheavey | 58:a159cd976aca | 105 | pad.leds_off(); |
jamesheavey | 58:a159cd976aca | 106 | wait(0.5); |
jamesheavey | 58:a159cd976aca | 107 | } |
jamesheavey | 91:c01a736fb0d9 | 108 | |
jamesheavey | 58:a159cd976aca | 109 | pad.tone(750.0,0.3); |
jamesheavey | 58:a159cd976aca | 110 | wait(0.2); |
jamesheavey | 58:a159cd976aca | 111 | main_menu(); // load main menu |
jamesheavey | 58:a159cd976aca | 112 | } |
jamesheavey | 58:a159cd976aca | 113 | |
jamesheavey | 58:a159cd976aca | 114 | |
jamesheavey | 91:c01a736fb0d9 | 115 | void main_menu() |
jamesheavey | 91:c01a736fb0d9 | 116 | { |
jamesheavey | 50:3da51f34e253 | 117 | lcd.clear(); |
jamesheavey | 99:d8f1570faa05 | 118 | lcd.printString(" START ",0,1); // start game with default as joystick |
jamesheavey | 73:54ab819609bc | 119 | lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt |
jamesheavey | 73:54ab819609bc | 120 | lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to do stuff |
jamesheavey | 91:c01a736fb0d9 | 121 | lcd.refresh(); |
jamesheavey | 56:f9e586348e0b | 122 | wait(0.3); |
jamesheavey | 91:c01a736fb0d9 | 123 | |
jamesheavey | 73:54ab819609bc | 124 | int pointer = 1; |
jamesheavey | 91:c01a736fb0d9 | 125 | |
jamesheavey | 91:c01a736fb0d9 | 126 | while (pad.check_event(Gamepad::A_PRESSED) == false) { |
jamesheavey | 53:8e3da0b58fe9 | 127 | lcd.clear(); |
jamesheavey | 99:d8f1570faa05 | 128 | lcd.printString(" START ",0,1); // start game with default as joystick |
jamesheavey | 73:54ab819609bc | 129 | lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt |
jamesheavey | 73:54ab819609bc | 130 | lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to do stuff |
jamesheavey | 56:f9e586348e0b | 131 | lcd.printString(" >",0,pointer); |
jamesheavey | 91:c01a736fb0d9 | 132 | lcd.refresh(); |
jamesheavey | 53:8e3da0b58fe9 | 133 | wait(0.1); |
jamesheavey | 73:54ab819609bc | 134 | 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 | 135 | pointer -= 1; |
jamesheavey | 53:8e3da0b58fe9 | 136 | pad.tone(750.0,0.3); |
jamesheavey | 53:8e3da0b58fe9 | 137 | wait(0.1); |
jamesheavey | 73:54ab819609bc | 138 | } 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 | 139 | pointer += 1; |
jamesheavey | 53:8e3da0b58fe9 | 140 | pad.tone(750.0,0.3); |
jamesheavey | 53:8e3da0b58fe9 | 141 | wait(0.1); |
jamesheavey | 50:3da51f34e253 | 142 | } |
jamesheavey | 99:d8f1570faa05 | 143 | //printf("Pointer 1 = %d",pointer); |
jamesheavey | 91:c01a736fb0d9 | 144 | |
jamesheavey | 50:3da51f34e253 | 145 | } |
jamesheavey | 73:54ab819609bc | 146 | 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 | 147 | pad.tone(750.0,0.3); |
jamesheavey | 99:d8f1570faa05 | 148 | number_of_frames = 0; |
jamesheavey | 72:7254d2a8a1cd | 149 | main_game(tilt,sens); |
jamesheavey | 91:c01a736fb0d9 | 150 | } else if (pointer == 2) { // if SETTINGS was selected, enter the settings menu |
jamesheavey | 56:f9e586348e0b | 151 | pad.tone(750.0,0.3); |
jamesheavey | 51:9d5c10a88b22 | 152 | settings(); |
jamesheavey | 99:d8f1570faa05 | 153 | } else if (pointer == 3) { // if HOW TO PLAY was selected, display instructions on how to play |
jamesheavey | 56:f9e586348e0b | 154 | pad.tone(750.0,0.3); |
jamesheavey | 51:9d5c10a88b22 | 155 | how_to_play(); |
jamesheavey | 51:9d5c10a88b22 | 156 | } |
jamesheavey | 50:3da51f34e253 | 157 | } |
jamesheavey | 50:3da51f34e253 | 158 | |
jamesheavey | 91:c01a736fb0d9 | 159 | void settings() |
jamesheavey | 91:c01a736fb0d9 | 160 | { |
jamesheavey | 50:3da51f34e253 | 161 | lcd.clear(); |
jamesheavey | 72:7254d2a8a1cd | 162 | lcd.printString(" JOYSTICK ",0,1); // choose joystick |
jamesheavey | 72:7254d2a8a1cd | 163 | lcd.printString(" TILT ",0,2); // choose tilt |
jamesheavey | 72:7254d2a8a1cd | 164 | lcd.printString("SENS :",0,4); |
jamesheavey | 72:7254d2a8a1cd | 165 | lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT); |
jamesheavey | 99:d8f1570faa05 | 166 | 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 | 167 | lcd.refresh(); |
jamesheavey | 75:d96b177585aa | 168 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 169 | |
jamesheavey | 72:7254d2a8a1cd | 170 | int pointer = 1; |
jamesheavey | 91:c01a736fb0d9 | 171 | |
jamesheavey | 58:a159cd976aca | 172 | while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu |
jamesheavey | 53:8e3da0b58fe9 | 173 | lcd.clear(); |
jamesheavey | 72:7254d2a8a1cd | 174 | lcd.printString(" JOYSTICK ",0,1); // choose joystick |
jamesheavey | 72:7254d2a8a1cd | 175 | lcd.printString(" TILT ",0,2); // choose tilt |
jamesheavey | 72:7254d2a8a1cd | 176 | lcd.printString("SENS :",0,4); |
jamesheavey | 56:f9e586348e0b | 177 | lcd.printString(" >",0,pointer); |
jamesheavey | 72:7254d2a8a1cd | 178 | lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT); |
jamesheavey | 72:7254d2a8a1cd | 179 | 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 | 180 | lcd.refresh(); |
jamesheavey | 53:8e3da0b58fe9 | 181 | wait(0.1); |
jamesheavey | 72:7254d2a8a1cd | 182 | 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 | 183 | pointer -= 1; |
jamesheavey | 53:8e3da0b58fe9 | 184 | pad.tone(750.0,0.3); |
jamesheavey | 53:8e3da0b58fe9 | 185 | wait(0.1); |
jamesheavey | 72:7254d2a8a1cd | 186 | } 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 | 187 | pointer += 1; |
jamesheavey | 53:8e3da0b58fe9 | 188 | pad.tone(750.0,0.3); |
jamesheavey | 53:8e3da0b58fe9 | 189 | wait(0.1); |
jamesheavey | 50:3da51f34e253 | 190 | } |
jamesheavey | 91:c01a736fb0d9 | 191 | |
jamesheavey | 58:a159cd976aca | 192 | if (pad.check_event(Gamepad::A_PRESSED)) { // if A is pressed, switch the tilt option accordingly |
jamesheavey | 56:f9e586348e0b | 193 | pad.tone(750.0,0.3); |
jamesheavey | 56:f9e586348e0b | 194 | wait(0.1); |
jamesheavey | 74:dfb1d693d8e3 | 195 | if (pointer == 1) { // if A is pressed on JOYSTICK, tilt = false |
jamesheavey | 57:d498dd835cfc | 196 | tilt = false; |
jamesheavey | 91:c01a736fb0d9 | 197 | } else if (pointer == 2) { // if A is pressed on TILT, tilt == true |
jamesheavey | 57:d498dd835cfc | 198 | tilt = true; |
jamesheavey | 57:d498dd835cfc | 199 | } |
jamesheavey | 58:a159cd976aca | 200 | } |
jamesheavey | 91:c01a736fb0d9 | 201 | |
jamesheavey | 58:a159cd976aca | 202 | if (pad.check_event(Gamepad::B_PRESSED)) { // when B is pressed return to main menu |
jamesheavey | 56:f9e586348e0b | 203 | pad.tone(750.0,0.3); |
jamesheavey | 72:7254d2a8a1cd | 204 | sens = pad.read_pot() + 0.5f; |
jamesheavey | 99:d8f1570faa05 | 205 | //printf("Sensitivity = %d",sens); |
jamesheavey | 91:c01a736fb0d9 | 206 | wait(0.3); |
jamesheavey | 50:3da51f34e253 | 207 | main_menu(); |
jamesheavey | 16:1761dfe801af | 208 | } |
jamesheavey | 99:d8f1570faa05 | 209 | //printf("Pointer 2 = %d",pointer); |
jamesheavey | 0:7d4d2023ed9c | 210 | } |
jamesheavey | 10:9f445faa892c | 211 | } |
jamesheavey | 10:9f445faa892c | 212 | |
jamesheavey | 91:c01a736fb0d9 | 213 | void how_to_play() |
jamesheavey | 91:c01a736fb0d9 | 214 | { |
jamesheavey | 84:6483503a72fc | 215 | lcd.clear(); |
jamesheavey | 84:6483503a72fc | 216 | lcd.printString(" B = LASER ",0,0); |
jamesheavey | 84:6483503a72fc | 217 | lcd.printString(" START = PAUSE ",0,1); |
jamesheavey | 84:6483503a72fc | 218 | lcd.printString(" DESTROY ALL ",0,2); |
jamesheavey | 84:6483503a72fc | 219 | lcd.printString(" BRICKS ",0,3); |
jamesheavey | 91:c01a736fb0d9 | 220 | arrowdown.render(lcd, 38, 43); |
jamesheavey | 84:6483503a72fc | 221 | lcd.refresh(); |
jamesheavey | 84:6483503a72fc | 222 | wait(0.4); |
jamesheavey | 58:a159cd976aca | 223 | 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 | 224 | |
jamesheavey | 91:c01a736fb0d9 | 225 | if (pad.check_event(Gamepad::R_PRESSED)) { |
jamesheavey | 84:6483503a72fc | 226 | lcd.clear(); |
jamesheavey | 84:6483503a72fc | 227 | lcd.printString(" CONTINUE TO ",0,2); |
jamesheavey | 84:6483503a72fc | 228 | lcd.printString("INCREASE SCORE. ",0,3); |
jamesheavey | 91:c01a736fb0d9 | 229 | arrowup.render(lcd, 38, 0); |
jamesheavey | 91:c01a736fb0d9 | 230 | lcd.refresh(); |
jamesheavey | 84:6483503a72fc | 231 | wait(0.1); |
jamesheavey | 84:6483503a72fc | 232 | } |
jamesheavey | 91:c01a736fb0d9 | 233 | if (pad.check_event(Gamepad::L_PRESSED)) { |
jamesheavey | 84:6483503a72fc | 234 | lcd.clear(); |
jamesheavey | 84:6483503a72fc | 235 | lcd.printString(" B = LASER ",0,0); |
jamesheavey | 84:6483503a72fc | 236 | lcd.printString(" START = PAUSE ",0,1); |
jamesheavey | 84:6483503a72fc | 237 | lcd.printString(" DESTROY ALL ",0,2); |
jamesheavey | 84:6483503a72fc | 238 | lcd.printString(" BRICKS ",0,3); |
jamesheavey | 91:c01a736fb0d9 | 239 | arrowdown.render(lcd, 38, 43); |
jamesheavey | 84:6483503a72fc | 240 | lcd.refresh(); |
jamesheavey | 84:6483503a72fc | 241 | wait(0.1); |
jamesheavey | 84:6483503a72fc | 242 | } |
jamesheavey | 50:3da51f34e253 | 243 | } |
jamesheavey | 84:6483503a72fc | 244 | wait(0.3); |
jamesheavey | 58:a159cd976aca | 245 | main_menu(); // when B is pressed, the loop is exited and main menu is entered once again |
jamesheavey | 58:a159cd976aca | 246 | } |
jamesheavey | 58:a159cd976aca | 247 | |
jamesheavey | 58:a159cd976aca | 248 | |
jamesheavey | 91:c01a736fb0d9 | 249 | void main_game(bool tilt, float sens) |
jamesheavey | 91:c01a736fb0d9 | 250 | { |
jamesheavey | 58:a159cd976aca | 251 | int fps = 8; // frames per second |
jamesheavey | 58:a159cd976aca | 252 | bool pause = false; // set pause screen to false |
jamesheavey | 99:d8f1570faa05 | 253 | //printf("Pause = %d",pointer); |
jamesheavey | 91:c01a736fb0d9 | 254 | |
jamesheavey | 58:a159cd976aca | 255 | lcd.setBrightness(1); |
jamesheavey | 91:c01a736fb0d9 | 256 | |
jamesheavey | 59:fdc05d5778a6 | 257 | countdown(); // run the countdown |
jamesheavey | 91:c01a736fb0d9 | 258 | |
jamesheavey | 91:c01a736fb0d9 | 259 | render(); // first draw the initial frame |
jamesheavey | 58:a159cd976aca | 260 | wait(1.0f/fps); // and wait for one frame period |
jamesheavey | 58:a159cd976aca | 261 | |
jamesheavey | 58:a159cd976aca | 262 | |
jamesheavey | 58:a159cd976aca | 263 | // game loop - read input, update the game state and render the display |
jamesheavey | 58:a159cd976aca | 264 | while (1) { |
jamesheavey | 72:7254d2a8a1cd | 265 | breakout.read_input(pad,tilt,sens); // read input from pad |
jamesheavey | 59:fdc05d5778a6 | 266 | breakout.update(pad); // update game |
jamesheavey | 59:fdc05d5778a6 | 267 | render(); // draw new frame |
jamesheavey | 91:c01a736fb0d9 | 268 | |
jamesheavey | 81:735e5ee2c92a | 269 | if (breakout.check_loss(pad) == true) { // if life lost flash screen |
jamesheavey | 58:a159cd976aca | 270 | flash_screen(lcd); |
jamesheavey | 58:a159cd976aca | 271 | } |
jamesheavey | 83:645cdbd79c21 | 272 | if (pad.check_event(Gamepad::START_PRESSED) == true) { // if BACK pressed, toggle pause |
jamesheavey | 58:a159cd976aca | 273 | pause = !pause; |
jamesheavey | 58:a159cd976aca | 274 | } |
jamesheavey | 91:c01a736fb0d9 | 275 | |
jamesheavey | 59:fdc05d5778a6 | 276 | while (pause == true) { // if pause is true, display pause screen |
jamesheavey | 58:a159cd976aca | 277 | lcd.clear(); |
jamesheavey | 83:645cdbd79c21 | 278 | lcd.printString(" PAUSED ",0,1); |
jamesheavey | 83:645cdbd79c21 | 279 | lcd.printString(" START = PLAY",0,3); |
jamesheavey | 83:645cdbd79c21 | 280 | lcd.printString(" BACK = MENU",0,4); |
jamesheavey | 58:a159cd976aca | 281 | lcd.refresh(); |
jamesheavey | 99:d8f1570faa05 | 282 | if (pad.check_event(Gamepad::START_PRESSED) == true) { // if START pressed, toggle pause, leaving pause screen |
jamesheavey | 58:a159cd976aca | 283 | pause = !pause; |
jamesheavey | 58:a159cd976aca | 284 | } |
jamesheavey | 99:d8f1570faa05 | 285 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { // if BACK pressed, return to the title screen |
jamesheavey | 99:d8f1570faa05 | 286 | breakout.set_mult_one(); // reset multiplier |
jamesheavey | 99:d8f1570faa05 | 287 | breakout.set_prev_score(0); |
jamesheavey | 99:d8f1570faa05 | 288 | breakout.reset_game(); //reset game to initial positions |
jamesheavey | 83:645cdbd79c21 | 289 | title_screen(); |
jamesheavey | 83:645cdbd79c21 | 290 | } |
jamesheavey | 58:a159cd976aca | 291 | } |
jamesheavey | 91:c01a736fb0d9 | 292 | |
jamesheavey | 79:70510b0102af | 293 | if (breakout.get_lives() == 0) { // when all lives lost, enter loss screen |
jamesheavey | 59:fdc05d5778a6 | 294 | 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 | 295 | breakout.set_mult_one(); // reset multiplier |
jamesheavey | 99:d8f1570faa05 | 296 | breakout.reset_game(); // return game to initial positions |
jamesheavey | 99:d8f1570faa05 | 297 | loss_screen(); |
jamesheavey | 58:a159cd976aca | 298 | } |
jamesheavey | 91:c01a736fb0d9 | 299 | |
jamesheavey | 59:fdc05d5778a6 | 300 | if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen |
jamesheavey | 99:d8f1570faa05 | 301 | breakout.reset_game(); // return game to initial positions |
jamesheavey | 99:d8f1570faa05 | 302 | breakout.inc_mult(); // increment multiplier |
jamesheavey | 58:a159cd976aca | 303 | victory_screen(); |
jamesheavey | 58:a159cd976aca | 304 | } |
jamesheavey | 99:d8f1570faa05 | 305 | number_of_frames ++; // track the number of frames passed to add to the score (inversely proportional) |
jamesheavey | 58:a159cd976aca | 306 | wait(1.0f/fps); |
jamesheavey | 58:a159cd976aca | 307 | } |
jamesheavey | 50:3da51f34e253 | 308 | } |
jamesheavey | 16:1761dfe801af | 309 | |
jamesheavey | 99:d8f1570faa05 | 310 | void loss_screen() // loss screen when lives of paddle == 0 |
jamesheavey | 64:c3426b417ad9 | 311 | { |
jamesheavey | 15:b867a6620f96 | 312 | lcd.clear(); |
jamesheavey | 64:c3426b417ad9 | 313 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 314 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 315 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 64:c3426b417ad9 | 316 | lcd.printString(" RESTART? ",2,1); |
jamesheavey | 72:7254d2a8a1cd | 317 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 91:c01a736fb0d9 | 318 | lcd.refresh(); |
jamesheavey | 15:b867a6620f96 | 319 | pad.tone(750.0,0.3); |
jamesheavey | 15:b867a6620f96 | 320 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 321 | |
jamesheavey | 15:b867a6620f96 | 322 | pad.tone(300.0,0.3); |
jamesheavey | 15:b867a6620f96 | 323 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 324 | |
jamesheavey | 91:c01a736fb0d9 | 325 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
jamesheavey | 10:9f445faa892c | 326 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 327 | |
jamesheavey | 64:c3426b417ad9 | 328 | lcd.printString(" RESTART? ",2,1); //print score here |
jamesheavey | 64:c3426b417ad9 | 329 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 10:9f445faa892c | 330 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 331 | |
jamesheavey | 64:c3426b417ad9 | 332 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 333 | |
jamesheavey | 64:c3426b417ad9 | 334 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 335 | |
jamesheavey | 64:c3426b417ad9 | 336 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 337 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 338 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 64:c3426b417ad9 | 339 | lcd.printString(" RESTART? ",2,1); //print score here |
jamesheavey | 64:c3426b417ad9 | 340 | lcd.printString(" PRESS START ",1,4); |
jamesheavey | 64:c3426b417ad9 | 341 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 342 | |
jamesheavey | 64:c3426b417ad9 | 343 | wait(0.4); |
jamesheavey | 10:9f445faa892c | 344 | } |
jamesheavey | 64:c3426b417ad9 | 345 | breakout.set_prev_score(0); |
jamesheavey | 49:31eb7807dbd3 | 346 | title_screen(); |
jamesheavey | 10:9f445faa892c | 347 | } |
jamesheavey | 10:9f445faa892c | 348 | |
jamesheavey | 91:c01a736fb0d9 | 349 | void victory_screen() |
jamesheavey | 64:c3426b417ad9 | 350 | { |
jamesheavey | 99:d8f1570faa05 | 351 | int bonus = NULL; |
jamesheavey | 99:d8f1570faa05 | 352 | |
jamesheavey | 99:d8f1570faa05 | 353 | 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 | 354 | bonus = breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720; |
jamesheavey | 99:d8f1570faa05 | 355 | } |
jamesheavey | 99:d8f1570faa05 | 356 | else { |
jamesheavey | 99:d8f1570faa05 | 357 | bonus = 0; |
jamesheavey | 99:d8f1570faa05 | 358 | } |
jamesheavey | 99:d8f1570faa05 | 359 | |
jamesheavey | 15:b867a6620f96 | 360 | lcd.clear(); |
jamesheavey | 99:d8f1570faa05 | 361 | |
jamesheavey | 64:c3426b417ad9 | 362 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 363 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 364 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 99:d8f1570faa05 | 365 | |
jamesheavey | 99:d8f1570faa05 | 366 | char buffer2[14]; |
jamesheavey | 99:d8f1570faa05 | 367 | sprintf(buffer2,"%2d",bonus); |
jamesheavey | 100:9842813b7131 | 368 | lcd.printString(buffer2,WIDTH/2 + 10,3); |
jamesheavey | 100:9842813b7131 | 369 | lcd.printString(" BONUS: ",2,3); |
jamesheavey | 99:d8f1570faa05 | 370 | |
jamesheavey | 43:81d22f68dfae | 371 | lcd.printString(" VICTORY! ",2,1); |
jamesheavey | 47:1d1a827be81b | 372 | lcd.printString(" CONT = START ",0,4); // print score here |
jamesheavey | 100:9842813b7131 | 373 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 15:b867a6620f96 | 374 | lcd.refresh(); |
jamesheavey | 22:fa2e0b58043a | 375 | pad.tone(2500.0,0.1); |
jamesheavey | 22:fa2e0b58043a | 376 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 377 | |
jamesheavey | 22:fa2e0b58043a | 378 | pad.tone(2500.0,0.1); |
jamesheavey | 22:fa2e0b58043a | 379 | wait(0.2); |
jamesheavey | 91:c01a736fb0d9 | 380 | |
jamesheavey | 15:b867a6620f96 | 381 | pad.tone(4000.0,0.6); |
jamesheavey | 15:b867a6620f96 | 382 | wait(0.6); |
jamesheavey | 99:d8f1570faa05 | 383 | |
jamesheavey | 91:c01a736fb0d9 | 384 | while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) { |
jamesheavey | 13:418a71b24b37 | 385 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 386 | |
jamesheavey | 43:81d22f68dfae | 387 | lcd.printString(" VICTORY! ",2,1); |
jamesheavey | 47:1d1a827be81b | 388 | lcd.printString(" CONT = START ",0,4); |
jamesheavey | 100:9842813b7131 | 389 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 43:81d22f68dfae | 390 | |
jamesheavey | 13:418a71b24b37 | 391 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 392 | |
jamesheavey | 64:c3426b417ad9 | 393 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 394 | |
jamesheavey | 64:c3426b417ad9 | 395 | lcd.clear(); |
jamesheavey | 91:c01a736fb0d9 | 396 | |
jamesheavey | 99:d8f1570faa05 | 397 | char buffer1[14]; |
jamesheavey | 64:c3426b417ad9 | 398 | sprintf(buffer1,"%2d",breakout.get_score()); |
jamesheavey | 67:c362df66fac9 | 399 | lcd.printString(buffer1,WIDTH/2 - 12,2); |
jamesheavey | 99:d8f1570faa05 | 400 | |
jamesheavey | 99:d8f1570faa05 | 401 | char buffer2[14]; |
jamesheavey | 99:d8f1570faa05 | 402 | sprintf(buffer2,"%2d",bonus); |
jamesheavey | 99:d8f1570faa05 | 403 | lcd.printString(buffer2,WIDTH/2 + 8,3); |
jamesheavey | 100:9842813b7131 | 404 | lcd.printString(" BONUS:",2,3); |
jamesheavey | 99:d8f1570faa05 | 405 | |
jamesheavey | 64:c3426b417ad9 | 406 | lcd.printString(" VICTORY! ",2,1); |
jamesheavey | 64:c3426b417ad9 | 407 | lcd.printString(" CONT = START ",0,4); // print score here |
jamesheavey | 100:9842813b7131 | 408 | lcd.printString(" MENU = BACK ",0,5); |
jamesheavey | 64:c3426b417ad9 | 409 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 410 | |
jamesheavey | 64:c3426b417ad9 | 411 | pad.tone(2500.0,0.1); |
jamesheavey | 64:c3426b417ad9 | 412 | |
jamesheavey | 64:c3426b417ad9 | 413 | lcd.refresh(); |
jamesheavey | 91:c01a736fb0d9 | 414 | |
jamesheavey | 64:c3426b417ad9 | 415 | wait(0.4); |
jamesheavey | 91:c01a736fb0d9 | 416 | |
jamesheavey | 43:81d22f68dfae | 417 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jamesheavey | 99:d8f1570faa05 | 418 | breakout.set_prev_score(breakout.get_score() + bonus); |
jamesheavey | 99:d8f1570faa05 | 419 | number_of_frames = 0; |
jamesheavey | 91:c01a736fb0d9 | 420 | main_game(tilt,sens); |
jamesheavey | 91:c01a736fb0d9 | 421 | } else if (pad.check_event(Gamepad::BACK_PRESSED)) { |
jamesheavey | 64:c3426b417ad9 | 422 | breakout.set_prev_score(0); |
jamesheavey | 99:d8f1570faa05 | 423 | number_of_frames = 0; |
jamesheavey | 49:31eb7807dbd3 | 424 | title_screen(); |
jamesheavey | 43:81d22f68dfae | 425 | } |
jamesheavey | 13:418a71b24b37 | 426 | } |
jamesheavey | 13:418a71b24b37 | 427 | } |
jamesheavey | 13:418a71b24b37 | 428 | |
jamesheavey | 91:c01a736fb0d9 | 429 | void flash_screen(N5110 &lcd) // move to engine |
jamesheavey | 91:c01a736fb0d9 | 430 | { |
jamesheavey | 58:a159cd976aca | 431 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 432 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 433 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 434 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 435 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 436 | wait(0.1); |
jamesheavey | 47:1d1a827be81b | 437 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 438 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 439 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 440 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 441 | lcd.setBrightness(1); |
jamesheavey | 58:a159cd976aca | 442 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 443 | lcd.setBrightness(0); |
jamesheavey | 58:a159cd976aca | 444 | wait(0.1); |
jamesheavey | 58:a159cd976aca | 445 | lcd.setBrightness(1); |
jamesheavey | 59:fdc05d5778a6 | 446 | } |
jamesheavey | 59:fdc05d5778a6 | 447 | |
jamesheavey | 91:c01a736fb0d9 | 448 | void countdown() |
jamesheavey | 91:c01a736fb0d9 | 449 | { |
jamesheavey | 59:fdc05d5778a6 | 450 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 451 | three.render(lcd, 0, 0); // render the 3 |
jamesheavey | 59:fdc05d5778a6 | 452 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 453 | pad.tone(500.0,0.5); |
jamesheavey | 59:fdc05d5778a6 | 454 | wait(1); // wait 1 second |
jamesheavey | 91:c01a736fb0d9 | 455 | |
jamesheavey | 59:fdc05d5778a6 | 456 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 457 | two.render(lcd, 0, 0); // render 2 |
jamesheavey | 59:fdc05d5778a6 | 458 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 459 | pad.tone(500.0,0.5); |
jamesheavey | 59:fdc05d5778a6 | 460 | wait(1); // wait 1 second |
jamesheavey | 91:c01a736fb0d9 | 461 | |
jamesheavey | 59:fdc05d5778a6 | 462 | lcd.clear(); |
jamesheavey | 59:fdc05d5778a6 | 463 | one.render(lcd, 0, 0); // render 1 |
jamesheavey | 59:fdc05d5778a6 | 464 | lcd.refresh(); |
jamesheavey | 59:fdc05d5778a6 | 465 | pad.tone(1000.0,1); |
jamesheavey | 59:fdc05d5778a6 | 466 | wait(1); // wait 1 second |
jamesheavey | 98:2ce2c666266b | 467 | } |
jamesheavey | 101:0df767523dbd | 468 | |
jamesheavey | 98:2ce2c666266b | 469 | void save_hi_score(int hi_score) |
jamesheavey | 98:2ce2c666266b | 470 | { |
jamesheavey | 98:2ce2c666266b | 471 | serial.baud(115200); // full-speed! |
jamesheavey | 98:2ce2c666266b | 472 | FILE *fp; // this is our file pointer |
jamesheavey | 98:2ce2c666266b | 473 | fp = fopen("/sd/hiscore.txt", "w"); |
jamesheavey | 98:2ce2c666266b | 474 | if (fp == NULL) { // if it can't open the file then print error message |
jamesheavey | 98:2ce2c666266b | 475 | serial.printf("Error! Unable to open file!\n"); |
jamesheavey | 98:2ce2c666266b | 476 | } else { // opened file so can write |
jamesheavey | 98:2ce2c666266b | 477 | serial.printf("Writing to file...."); |
jamesheavey | 98:2ce2c666266b | 478 | fprintf(fp, "%d",hi_score); // ensure data type matches |
jamesheavey | 98:2ce2c666266b | 479 | serial.printf("Done.\n"); |
jamesheavey | 98:2ce2c666266b | 480 | fclose(fp); // ensure you close the file after writing |
jamesheavey | 98:2ce2c666266b | 481 | } |
jamesheavey | 98:2ce2c666266b | 482 | } |
jamesheavey | 98:2ce2c666266b | 483 | |
jamesheavey | 98:2ce2c666266b | 484 | int get_hi_score() |
jamesheavey | 98:2ce2c666266b | 485 | { |
jamesheavey | 101:0df767523dbd | 486 | serial.baud(115200); |
jamesheavey | 101:0df767523dbd | 487 | FILE *fp; |
jamesheavey | 98:2ce2c666266b | 488 | fp = fopen("/sd/hiscore.txt", "r"); |
jamesheavey | 98:2ce2c666266b | 489 | int stored_hi_score = NULL; |
jamesheavey | 98:2ce2c666266b | 490 | if (fp == NULL) { // if it can't open the file then print error message |
jamesheavey | 98:2ce2c666266b | 491 | serial.printf("Error! Unable to open file!\n"); |
jamesheavey | 98:2ce2c666266b | 492 | } else { // opened file so can write |
jamesheavey | 98:2ce2c666266b | 493 | fscanf(fp, "%d",&stored_hi_score); // ensure data type matches - note address operator (&) |
jamesheavey | 101:0df767523dbd | 494 | serial.printf("Read %d from file.\n",stored_hi_score); |
jamesheavey | 98:2ce2c666266b | 495 | fclose(fp); // ensure you close the file after reading |
jamesheavey | 98:2ce2c666266b | 496 | } |
jamesheavey | 98:2ce2c666266b | 497 | return stored_hi_score; |
jamesheavey | 99:d8f1570faa05 | 498 | } |
jamesheavey | 101:0df767523dbd | 499 | |
jamesheavey | 101:0df767523dbd | 500 | bool compare_to_hi_score(int score) |
jamesheavey | 101:0df767523dbd | 501 | { |
jamesheavey | 101:0df767523dbd | 502 | if (score >= get_hi_score()) { |
jamesheavey | 101:0df767523dbd | 503 | printf("hi score! \n"); |
jamesheavey | 101:0df767523dbd | 504 | save_hi_score(score); |
jamesheavey | 101:0df767523dbd | 505 | return true; |
jamesheavey | 101:0df767523dbd | 506 | } |
jamesheavey | 101:0df767523dbd | 507 | else { |
jamesheavey | 101:0df767523dbd | 508 | return false; |
jamesheavey | 101:0df767523dbd | 509 | } |
jamesheavey | 101:0df767523dbd | 510 | } |
jamesheavey | 101:0df767523dbd | 511 |