FINAL VERSION

Dependencies:   mbed

Revision:
91:c01a736fb0d9
Parent:
84:6483503a72fc
Child:
94:8fa117200f6b
--- a/main.cpp	Mon May 06 19:24:32 2019 +0000
+++ b/main.cpp	Mon May 06 22:57:04 2019 +0000
@@ -39,16 +39,16 @@
 ///////////// 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 
+    // need to initialise LCD and Gamepad
     lcd.init();
     pad.init();
     breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); // init the objects
@@ -59,65 +59,67 @@
 void render()
 {
     // clear screen, re-draw and refresh
-    lcd.clear();  
+    lcd.clear();
     breakout.draw(lcd);
     lcd.refresh();
 }
 
-void title_screen() {
+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 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() {
-    
+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();  
+    lcd.refresh();
     wait(0.3);
-    
+
     int pointer = 1;
-    
-    while (pad.check_event(Gamepad::A_PRESSED) == false) {  
+
+    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();  
+        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;
@@ -128,23 +130,22 @@
             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
+    } 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
+    } 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() {
+void settings()
+{
 
     lcd.clear();
     lcd.printString("   JOYSTICK ",0,1);    // choose joystick
@@ -153,11 +154,11 @@
     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();  
+    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
@@ -166,7 +167,7 @@
         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();  
+        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;
@@ -177,56 +178,56 @@
             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
+            } 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); 
+            wait(0.3);
             main_menu();
         }
     }
 }
 
-void how_to_play() {
+void how_to_play()
+{
     Bitmap arrowup(arrowup_data, 5, 7);  // assign the arrow up sprite data
     Bitmap arrowdown(arrowdown_data, 5, 7);  // assign the arrow down sprite data
-    
+
     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); 
+    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)){
+
+        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(); 
+            arrowup.render(lcd, 38, 0);
+            lcd.refresh();
             wait(0.1);
         }
-        if (pad.check_event(Gamepad::L_PRESSED)){
+        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); 
+            arrowdown.render(lcd, 38, 43);
             lcd.refresh();
             wait(0.1);
         }
@@ -236,15 +237,16 @@
 }
 
 
-void main_game(bool tilt, float sens) {
+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 
+
+    render();  // first draw the initial frame
     wait(1.0f/fps);  // and wait for one frame period
 
 
@@ -253,14 +255,14 @@
         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);
@@ -274,14 +276,14 @@
                 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();
@@ -291,7 +293,7 @@
     }
 }
 
-void loss_screen() 
+void loss_screen()
 {
     lcd.clear();
     char buffer1[14];
@@ -299,38 +301,38 @@
     lcd.printString(buffer1,WIDTH/2 - 12,2);
     lcd.printString("   RESTART? ",2,1);
     lcd.printString("  PRESS START ",1,4);
-    lcd.refresh();    
+    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) { 
+
+    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() 
+void victory_screen()
 {
     lcd.clear();
     char buffer1[14];
@@ -342,50 +344,50 @@
     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) { 
+    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)) {
+            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
+void flash_screen(N5110 &lcd)   // move to engine
+{
     lcd.setBrightness(0);
     wait(0.1);
     lcd.setBrightness(1);
@@ -403,23 +405,24 @@
     lcd.setBrightness(1);
 }
 
-void countdown() {
+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();