James Heavey
/
EL17JH
FINAL VERSION
main.cpp
- Committer:
- jamesheavey
- Date:
- 2019-05-07
- Revision:
- 100:9842813b7131
- Parent:
- 99:d8f1570faa05
- Child:
- 101:0df767523dbd
File content as of revision 100:9842813b7131:
///////// pre-processor directives //////// #include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "BreakoutEngine.h" #include "Bitmap.h" #include "Sprites.h" //#include "SDFileSystem.h" //#include "Menus.h" //#include "Game.h" /////////////// structs ///////////////// struct UserInput { Direction d; float mag; }; /////////////// objects /////////////// N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; BreakoutEngine breakout; //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS //Serial serial(USBTX, USBRX); // for PC debug ///////////// prototypes /////////////// void init(); void update_game(UserInput input); void render(); void main_menu(); void settings(); void how_to_play(); void loss_screen(); void victory_screen(); void title_screen(); void main_game(bool tilt,float sens); void flash_screen(N5110 &lcd); void countdown(); void save_hi_score(int hi_score); int get_hi_score(); Bitmap breakwhite(breakwhite_data, 48, 84); // assign the title screen sprites Bitmap breakblack(breakblack_data, 48, 84); Bitmap arrowup(arrowup_data, 5, 7); // assign the arrow up sprite data Bitmap arrowdown(arrowdown_data, 5, 7); // assign the arrow down sprite data Bitmap three(three_data, 48, 84); // assign the 3 sprite data Bitmap two(two_data, 48, 84); // assign the 2 sprite data Bitmap one(one_data, 48, 84); // assign the 1 sprite data bool tilt = false; float sens = 0.5; // default sens int number_of_frames = 0; ///////////// functions //////////////// int main() { init(); // initialise the gamepad title_screen(); // run the title screen } // initialies all classes and libraries void init() { // need to initialise LCD and Gamepad lcd.init(); pad.init(); breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); // init the objects } // this function draws each frame on the LCD void render() { // clear screen, re-draw and refresh lcd.clear(); breakout.draw(lcd); lcd.refresh(); } void title_screen() { tilt = false; // reset the tilt variable so that on start, joystick is default lcd.clear(); breakblack.render(lcd, 0, 0); // render the first frame lcd.refresh(); pad.leds_off(); wait(0.5); while (pad.check_event(Gamepad::START_PRESSED) == false) { // while start is not pressed alternate sprites lcd.clear(); breakwhite.render(lcd, 0, 0); lcd.refresh(); pad.leds_on(); wait(0.5); lcd.clear(); breakblack.render(lcd, 0, 0); lcd.refresh(); pad.leds_off(); wait(0.5); } pad.tone(750.0,0.3); wait(0.2); main_menu(); // load main menu } void main_menu() { lcd.clear(); lcd.printString(" START ",0,1); // start game with default as joystick lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to do stuff lcd.refresh(); wait(0.3); int pointer = 1; while (pad.check_event(Gamepad::A_PRESSED) == false) { lcd.clear(); lcd.printString(" START ",0,1); // start game with default as joystick lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt lcd.printString(" HOW TO PLAY ",0,3); // brief text on how to do stuff lcd.printString(" >",0,pointer); lcd.refresh(); wait(0.1); if (pad.check_event(Gamepad::L_PRESSED) && pointer > 1) { // if L is pressed and pointer isnt already on START, move it up one line pointer -= 1; pad.tone(750.0,0.3); wait(0.1); } 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 pointer += 1; pad.tone(750.0,0.3); wait(0.1); } //printf("Pointer 1 = %d",pointer); } if (pointer == 1) { // if START was selected on exit of the while loop, run main game with the appropriate tilt/joystick setting pad.tone(750.0,0.3); number_of_frames = 0; main_game(tilt,sens); } else if (pointer == 2) { // if SETTINGS was selected, enter the settings menu pad.tone(750.0,0.3); settings(); } else if (pointer == 3) { // if HOW TO PLAY was selected, display instructions on how to play pad.tone(750.0,0.3); how_to_play(); } } void settings() { lcd.clear(); lcd.printString(" JOYSTICK ",0,1); // choose joystick lcd.printString(" TILT ",0,2); // choose tilt lcd.printString("SENS :",0,4); lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT); lcd.drawRect(42,30,40 * pad.read_pot() + 1,10,FILL_BLACK); // represents the sensitivity which is changed by turning the pot lcd.refresh(); wait(0.4); int pointer = 1; while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu lcd.clear(); lcd.printString(" JOYSTICK ",0,1); // choose joystick lcd.printString(" TILT ",0,2); // choose tilt lcd.printString("SENS :",0,4); lcd.printString(" >",0,pointer); lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT); lcd.drawRect(42,30,40 * pad.read_pot() + 1,10,FILL_BLACK); // have it so it fills half the transparent one (default position) lcd.refresh(); wait(0.1); if (pad.check_event(Gamepad::L_PRESSED) && pointer > 1) { // if L is pressed and pointer isnt already on JOYSTICK, move it up one line pointer -= 1; pad.tone(750.0,0.3); wait(0.1); } 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 pointer += 1; pad.tone(750.0,0.3); wait(0.1); } if (pad.check_event(Gamepad::A_PRESSED)) { // if A is pressed, switch the tilt option accordingly pad.tone(750.0,0.3); wait(0.1); if (pointer == 1) { // if A is pressed on JOYSTICK, tilt = false tilt = false; } else if (pointer == 2) { // if A is pressed on TILT, tilt == true tilt = true; } } if (pad.check_event(Gamepad::B_PRESSED)) { // when B is pressed return to main menu pad.tone(750.0,0.3); sens = pad.read_pot() + 0.5f; //printf("Sensitivity = %d",sens); wait(0.3); main_menu(); } //printf("Pointer 2 = %d",pointer); } } void how_to_play() { lcd.clear(); lcd.printString(" B = LASER ",0,0); lcd.printString(" START = PAUSE ",0,1); lcd.printString(" DESTROY ALL ",0,2); lcd.printString(" BRICKS ",0,3); arrowdown.render(lcd, 38, 43); lcd.refresh(); wait(0.4); 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 if (pad.check_event(Gamepad::R_PRESSED)) { lcd.clear(); lcd.printString(" CONTINUE TO ",0,2); lcd.printString("INCREASE SCORE. ",0,3); arrowup.render(lcd, 38, 0); lcd.refresh(); wait(0.1); } if (pad.check_event(Gamepad::L_PRESSED)) { lcd.clear(); lcd.printString(" B = LASER ",0,0); lcd.printString(" START = PAUSE ",0,1); lcd.printString(" DESTROY ALL ",0,2); lcd.printString(" BRICKS ",0,3); arrowdown.render(lcd, 38, 43); lcd.refresh(); wait(0.1); } } wait(0.3); main_menu(); // when B is pressed, the loop is exited and main menu is entered once again } void main_game(bool tilt, float sens) { int fps = 8; // frames per second bool pause = false; // set pause screen to false //printf("Pause = %d",pointer); lcd.setBrightness(1); countdown(); // run the countdown render(); // first draw the initial frame wait(1.0f/fps); // and wait for one frame period // game loop - read input, update the game state and render the display while (1) { breakout.read_input(pad,tilt,sens); // read input from pad breakout.update(pad); // update game render(); // draw new frame if (breakout.check_loss(pad) == true) { // if life lost flash screen flash_screen(lcd); } if (pad.check_event(Gamepad::START_PRESSED) == true) { // if BACK pressed, toggle pause pause = !pause; } while (pause == true) { // if pause is true, display pause screen lcd.clear(); lcd.printString(" PAUSED ",0,1); lcd.printString(" START = PLAY",0,3); lcd.printString(" BACK = MENU",0,4); lcd.refresh(); if (pad.check_event(Gamepad::START_PRESSED) == true) { // if START pressed, toggle pause, leaving pause screen pause = !pause; } if (pad.check_event(Gamepad::BACK_PRESSED) == true) { // if BACK pressed, return to the title screen breakout.set_mult_one(); // reset multiplier breakout.set_prev_score(0); breakout.reset_game(); //reset game to initial positions title_screen(); } } if (breakout.get_lives() == 0) { // when all lives lost, enter loss screen pad.leds_off(); //turns last led off (doesnt run through and update board so last led doent turn off via lives leds breakout.set_mult_one(); // reset multiplier breakout.reset_game(); // return game to initial positions loss_screen(); } if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen breakout.reset_game(); // return game to initial positions breakout.inc_mult(); // increment multiplier victory_screen(); } number_of_frames ++; // track the number of frames passed to add to the score (inversely proportional) wait(1.0f/fps); } } void loss_screen() // loss screen when lives of paddle == 0 { lcd.clear(); char buffer1[14]; sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); lcd.printString(" RESTART? ",2,1); lcd.printString(" PRESS START ",1,4); lcd.refresh(); pad.tone(750.0,0.3); wait(0.4); pad.tone(300.0,0.3); wait(0.4); while (pad.check_event(Gamepad::START_PRESSED) == false) { lcd.clear(); lcd.printString(" RESTART? ",2,1); //print score here lcd.printString(" PRESS START ",1,4); lcd.refresh(); wait(0.4); lcd.clear(); char buffer1[14]; sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); lcd.printString(" RESTART? ",2,1); //print score here lcd.printString(" PRESS START ",1,4); lcd.refresh(); wait(0.4); } breakout.set_prev_score(0); title_screen(); } void victory_screen() { int bonus = NULL; if (breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720 > 0) { // beat within 90 seconds and you get a bonus bonus = breakout.get_score()/2 - ((breakout.get_score()/2) * number_of_frames)/720; } else { bonus = 0; } lcd.clear(); char buffer1[14]; sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); char buffer2[14]; sprintf(buffer2,"%2d",bonus); lcd.printString(buffer2,WIDTH/2 + 10,3); lcd.printString(" BONUS: ",2,3); lcd.printString(" VICTORY! ",2,1); lcd.printString(" CONT = START ",0,4); // print score here lcd.printString(" MENU = BACK ",0,5); lcd.refresh(); pad.tone(2500.0,0.1); wait(0.4); pad.tone(2500.0,0.1); wait(0.2); pad.tone(4000.0,0.6); wait(0.6); while (pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED) == false) { lcd.clear(); lcd.printString(" VICTORY! ",2,1); lcd.printString(" CONT = START ",0,4); lcd.printString(" MENU = BACK ",0,5); lcd.refresh(); wait(0.4); lcd.clear(); char buffer1[14]; sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); char buffer2[14]; sprintf(buffer2,"%2d",bonus); lcd.printString(buffer2,WIDTH/2 + 8,3); lcd.printString(" BONUS:",2,3); lcd.printString(" VICTORY! ",2,1); lcd.printString(" CONT = START ",0,4); // print score here lcd.printString(" MENU = BACK ",0,5); lcd.refresh(); pad.tone(2500.0,0.1); lcd.refresh(); wait(0.4); if (pad.check_event(Gamepad::START_PRESSED)) { breakout.set_prev_score(breakout.get_score() + bonus); number_of_frames = 0; main_game(tilt,sens); } else if (pad.check_event(Gamepad::BACK_PRESSED)) { breakout.set_prev_score(0); number_of_frames = 0; title_screen(); } } } void flash_screen(N5110 &lcd) // move to engine { lcd.setBrightness(0); wait(0.1); lcd.setBrightness(1); wait(0.1); lcd.setBrightness(0); wait(0.1); lcd.setBrightness(1); wait(0.1); lcd.setBrightness(0); wait(0.1); lcd.setBrightness(1); wait(0.1); lcd.setBrightness(0); wait(0.1); lcd.setBrightness(1); } void countdown() { lcd.clear(); three.render(lcd, 0, 0); // render the 3 lcd.refresh(); pad.tone(500.0,0.5); wait(1); // wait 1 second lcd.clear(); two.render(lcd, 0, 0); // render 2 lcd.refresh(); pad.tone(500.0,0.5); wait(1); // wait 1 second lcd.clear(); one.render(lcd, 0, 0); // render 1 lcd.refresh(); pad.tone(1000.0,1); wait(1); // wait 1 second } /* void save_hi_score(int hi_score) { serial.baud(115200); // full-speed! FILE *fp; // this is our file pointer fp = fopen("/sd/hiscore.txt", "w"); if (fp == NULL) { // if it can't open the file then print error message serial.printf("Error! Unable to open file!\n"); } else { // opened file so can write serial.printf("Writing to file...."); fprintf(fp, "%d",hi_score); // ensure data type matches serial.printf("Done.\n"); fclose(fp); // ensure you close the file after writing } } int get_hi_score() { fp = fopen("/sd/hiscore.txt", "r"); int stored_hi_score = NULL; if (fp == NULL) { // if it can't open the file then print error message serial.printf("Error! Unable to open file!\n"); } else { // opened file so can write fscanf(fp, "%d",&stored_hi_score); // ensure data type matches - note address operator (&) serial.printf("Read %d from file.\n",stored_top_score); fclose(fp); // ensure you close the file after reading } return stored_hi_score; } */