FINAL VERSION

Dependencies:   mbed

Revision:
58:a159cd976aca
Parent:
57:d498dd835cfc
Child:
59:fdc05d5778a6
--- a/main.cpp	Tue Apr 30 10:06:46 2019 +0000
+++ b/main.cpp	Sat May 04 18:43:08 2019 +0000
@@ -35,8 +35,10 @@
 void settings();
 void how_to_play();
 void end_screen();
+void victory_screen();
 void title_screen();
 void main_game(bool tilt);
+void flash_screen(N5110 &lcd);
 int prev_score = 0;
 
 bool tilt = false;
@@ -49,7 +51,9 @@
 
     if(number_of_failures > 0) return number_of_failures;
 #endif
-    title_screen();
+    
+    init();     // initialise the gamepad
+    title_screen();  // run the title screen
     
 }
 
@@ -59,8 +63,6 @@
     // need to initialise LCD and Gamepad 
     lcd.init();
     pad.init();
-     
-    // initialise the game with correct ball and paddle sizes
 
 }
 
@@ -73,10 +75,44 @@
     lcd.refresh();
 }
 
-// simple splash screen displayed on start-up
+void title_screen() {
+    tilt = false;   // reset the tilt variable so that on start, joystick is default
+    prev_score = 0; // reset the prev_score variable so that on reset, prev score is returned to 0, and therefore the multiplier is too
+    
+    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() {
-    // wait flashing LEDs until start button is pressed 
-    //bool joy = true;
     
     lcd.clear();
     lcd.printString("   START ",0,2);    // start with default as joystick
@@ -87,7 +123,7 @@
     
     int pointer = 2;
     
-    while (pad.check_event(Gamepad::A_PRESSED) == false) {  //maybe add a jingle
+    while (pad.check_event(Gamepad::A_PRESSED) == false) {  
         lcd.clear();
         lcd.printString("   START ",0,2);    // start with default as joystick
         lcd.printString("   SETTINGS ",0,3);   // choose between joystick and tilt
@@ -95,26 +131,26 @@
         lcd.printString(" >",0,pointer);
         lcd.refresh();  
         wait(0.1);
-        if (pad.check_event(Gamepad::L_PRESSED) && pointer > 2) {  // change to joystick
+        if (pad.check_event(Gamepad::L_PRESSED) && pointer > 2) {  // 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 < 4) {
+        } else if (pad.check_event(Gamepad::R_PRESSED) && pointer < 4) {  // 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 == 2) {
+    if (pointer == 2) {    // 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);
     }
-    else if (pointer == 3){
+    else if (pointer == 3){ // if SETTINGS was selected, enter the settings menu
         pad.tone(750.0,0.3);
         settings();
     }
-    else if (pointer == 4){
+    else if (pointer == 4){ // if HOW TO PLAY WAS SELECTED, display instructions on how to play
         pad.tone(750.0,0.3);
         how_to_play();
     }
@@ -123,57 +159,123 @@
 void settings() {
 
     lcd.clear();
-    lcd.printString("   JOYSTICK ",0,2);    // start with default as joystick
-    lcd.printString("   TILT ",0,3);   // choose between joystick and tilt
+    lcd.printString("   JOYSTICK ",0,2);    // choose joystick
+    lcd.printString("   TILT ",0,3);   // choose tilt
     lcd.refresh();  
     wait(0.3);
     
     int pointer = 2;
     
-    while (pad.check_event(Gamepad::B_PRESSED) == false) {
+    while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu
         lcd.clear();
-        lcd.printString("   JOYSTICK ",0,2);    // start with default as joystick
-        lcd.printString("   TILT ",0,3);   // choose between joystick and tilt
+        lcd.printString("   JOYSTICK ",0,2);    // choose joystick
+        lcd.printString("   TILT ",0,3);   // choose tilt
         lcd.printString(" >",0,pointer);
         lcd.refresh();  
         wait(0.1);
-        if (pad.check_event(Gamepad::L_PRESSED) && pointer > 2) {
+        if (pad.check_event(Gamepad::L_PRESSED) && pointer > 2) {  // 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 < 3) {
+        } else if (pad.check_event(Gamepad::R_PRESSED) && pointer < 3) {  // 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 (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 == 2) {
+            if (pointer == 2) {  // if A is pressed on JOYSTICK, tilt = false
                 tilt = false;
             }
-            else if (pointer == 3) {
+            else if (pointer == 3) { // if A is pressed on TILT, tilt == true
                 tilt = true;
             }
+        }
         
-        if (pad.check_event(Gamepad::B_PRESSED)) {
+        if (pad.check_event(Gamepad::B_PRESSED)) {  // when B is pressed return to main menu
             pad.tone(750.0,0.3);
             main_menu();
         }
     }
-    //settings(); //sometimes the button bounce causes it to skip the while loop (could add wait in at the top)
 }
 
 void how_to_play() {
-    while (pad.check_event(Gamepad::B_PRESSED) == false) {
+    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();
+    main_menu();  // when B is pressed, the loop is exited and main menu is entered once again
+}
+
+
+void main_game(bool tilt) {
+    int fps = 8;  // frames per second
+    bool pause = false; // set pause screen to false
+    
+    breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,pad.read_pot()*3+2,prev_score); // init the objects
+    
+    lcd.setBrightness(1);
+    
+    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
+    
+    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);
+        breakout.update(pad);
+        render();
+        if (breakout.check_goal(pad) == true) {
+            flash_screen(lcd);
+        }
+        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
+            pause = !pause;
+        }
+        while (pause == true) {
+            lcd.clear();
+            lcd.printString("    PAUSED ",0,2);
+            lcd.refresh();
+            if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
+                pause = !pause;
+            }
+            wait(1.0f/fps);
+        }
+        if (breakout.get_lives() == 0) {
+            pad.leds_off(); //turns last led off (doesnt run through and update bored so last led doent turn off via lives leds
+            end_screen();
+        }
+        if (breakout.get_num_left() == 0) {
+            victory_screen();
+        }
+        wait(1.0f/fps);
+    }
 }
 
 void end_screen() {
@@ -229,115 +331,20 @@
     }
 }
 
-void title_screen() {
-    tilt = false;
-    prev_score = 0;
-    init();     // initialise and then display welcome screen...
-    
-    Bitmap breakwhite(breakwhite_data, 48, 84);
-    Bitmap breakblack(breakblack_data, 48, 84);
-    
-    while (pad.check_event(Gamepad::START_PRESSED) == false) {  //maybe add a jingle
-        
-        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();
-}
-
-void main_game(bool tilt) {
-    int fps = 8;  // frames per second
-    
-    breakout.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,pad.read_pot()*3+2,prev_score); // init the objects
-    
-    bool pause = false;
+void flash_screen(N5110 &lcd) {
+    lcd.setBrightness(0);
+    wait(0.1);
+    lcd.setBrightness(1);
+    wait(0.1);
+    lcd.setBrightness(0);
+    wait(0.1);
     lcd.setBrightness(1);
-    
-    Bitmap three(three_data, 48, 84);
-    Bitmap two(two_data, 48, 84);
-    Bitmap one(one_data, 48, 84);
-    
-    lcd.clear();
-    three.render(lcd, 0, 0);
-    lcd.refresh();
-    pad.tone(500.0,0.5);
-    wait(1);
-    
-    lcd.clear();
-    two.render(lcd, 0, 0);
-    lcd.refresh();
-    pad.tone(500.0,0.5);
-    wait(1);
-    
-    lcd.clear();
-    one.render(lcd, 0, 0);
-    lcd.refresh();
-    pad.tone(1000.0,1);
-    wait(1);
-    
-    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);
-        breakout.update(pad);
-        render();
-        if (breakout.check_goal(pad) == true) {
-            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);
-        }
-        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
-            pause = !pause;
-        }
-        while (pause == true) {
-            lcd.clear();
-            lcd.printString("    PAUSED ",0,2);
-            lcd.refresh();
-            if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
-                pause = !pause;
-            }
-            wait(1.0f/fps);
-        }
-        if (breakout.get_lives() == 0) {
-            //if (end_screen() == true) {
-            //    main_menu();
-            //}
-            //else {
-            //    main_menu();
-            //}
-            pad.leds_off(); //turns last led off (doesnt run through and update bored so last led doent turn off via lives leds
-            end_screen();
-        }
-        if (breakout.get_num_left() == 0) {
-            victory_screen();
-        }
-        wait(1.0f/fps);
-    }
-}
-    
\ No newline at end of file
+    wait(0.1);
+    lcd.setBrightness(0);
+    wait(0.1);
+    lcd.setBrightness(1);
+    wait(0.1);
+    lcd.setBrightness(0);
+    wait(0.1);
+    lcd.setBrightness(1);
+}
\ No newline at end of file