James Heavey
/
EL17JH
FINAL VERSION
main.cpp
- Committer:
- jamesheavey
- Date:
- 2019-05-06
- Revision:
- 83:645cdbd79c21
- Parent:
- 82:d1341d632890
- Child:
- 84:6483503a72fc
File content as of revision 83:645cdbd79c21:
///////// pre-processor directives //////// #include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "BreakoutEngine.h" #include "Bitmap.h" #include "Sprites.h" /////////////// structs ///////////////// struct UserInput { Direction d; float mag; }; /////////////// objects /////////////// N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; BreakoutEngine breakout; ///////////// 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(); bool tilt = false; float sens = 0.5; // default sens ///////////// 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 Bitmap breakwhite(breakwhite_data, 48, 84); // assign the title screen sprites Bitmap breakblack(breakblack_data, 48, 84); 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 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 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); } } 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); 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); // have it so it fills half the transparent one (default position) // the sens can only change via pot when the pointer is on the right pointer 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; wait(0.3); main_menu(); } } } void how_to_play() { 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 lcd.clear(); lcd.printString(" explain ",2,2); lcd.printString(" PRESS B ",1,4); lcd.refresh(); wait(0.1); } 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 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 BACK pressed, toggle pause, leaving pause screen pause = !pause; } if (pad.check_event(Gamepad::BACK_PRESSED) == true) { // if BACK pressed, toggle pause, leaving pause screen 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.reset_game(); breakout.set_mult_zero(); loss_screen(); } if (breakout.get_num_left() == 0) { // when all bricks destroyed, display victory screen breakout.reset_game(); breakout.inc_mult(); victory_screen(); } wait(1.0f/fps); } } void loss_screen() { 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() { lcd.clear(); char buffer1[14]; sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); 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(); sprintf(buffer1,"%2d",breakout.get_score()); lcd.printString(buffer1,WIDTH/2 - 12,2); 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()); main_game(tilt,sens); } else if (pad.check_event(Gamepad::BACK_PRESSED)) { breakout.set_prev_score(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() { 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 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 }