Menu class used as basis for program; running the game and allowing the user to change the style and difficulty of the game via an interface.

Revision:
5:56f13954666e
Parent:
4:4f20bcef2c0c
Child:
6:2a05dd8b9750
diff -r 4f20bcef2c0c -r 56f13954666e Menu.cpp
--- a/Menu.cpp	Mon May 01 09:13:40 2017 +0000
+++ b/Menu.cpp	Wed May 03 21:13:31 2017 +0000
@@ -2,7 +2,7 @@
  
  @file Menu.cpp
  
- (c) Max Houghton 02.14.17
+ (c) Max Houghton 02.04.17
  Roller Maze Project, ELEC2645, Univeristy of Leeds
  
  */
@@ -24,7 +24,7 @@
 void Menu::init()
 {
     // set all default game parameters
-    _mazeIndex = 1;
+    _difficulty = 4;
     _FPS = 50;
     
     _control = true;
@@ -48,7 +48,7 @@
             (d == NE)){
             
             if (_tone){
-                pad.tone(196.00, 0.20);
+                pad.tone(G_NOTE, 0.20);
             }
             selected -= 1;
         }
@@ -58,7 +58,7 @@
             (d == SE)){
             
             if (_tone){
-                pad.tone(196.00, 0.20);
+                pad.tone(G_NOTE, 0.20);
             }
             selected += 1;
         }
@@ -69,11 +69,11 @@
             case 1:
                 
                 lcd.clear();
-                lcd.printString(">Play game", 0, 0);
-                lcd.printString(" Game options", 0, 1);
-                lcd.printString(" LCD settings", 0, 2);
-                lcd.printString(" Sound", 0, 3);
-                
+                lcd.printString(" ROLLER MAZE ", 0, 0);
+                lcd.printString(">Play game", 0, 2);
+                lcd.printString(" Game options", 0, 3);
+                lcd.printString(" LCD settings", 0, 4);
+                lcd.printString(" Sound", 0, 5);
                 lcd.refresh();
                 
                 // either clicking joystick or pressing A selects function
@@ -81,11 +81,10 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    wait_ms(100);
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
-                    playGame(lcd, pad, device, animate, _mazeIndex, _tone, _FPS);
+                    playGame(lcd, pad, device, animate, _difficulty, _tone, _FPS);
                     // call the game function
                 }
                 
@@ -96,21 +95,19 @@
             case 2:
                 
                 lcd.clear();
-                lcd.printString(" Play game", 0, 0);
-                lcd.printString(">Game options", 0, 1);
-                lcd.printString(" LCD settings", 0, 2);
-                lcd.printString(" Sound", 0, 3);
-                
+                lcd.printString(" ROLLER MAZE ", 0, 0);
+                lcd.printString(" Play game", 0, 2);
+                lcd.printString(">Game options", 0, 3);
+                lcd.printString(" LCD settings", 0, 4);
+                lcd.printString(" Sound", 0, 5);
                 lcd.refresh();
                 
-                
                 if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    wait_ms(250);
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     options(lcd, pad, animate, _tone);
                 }
@@ -122,21 +119,19 @@
             case 3:
                 
                 lcd.clear();
-                lcd.printString(" Play game", 0, 0);
-                lcd.printString(" Game options", 0, 1);
-                lcd.printString(">LCD settings", 0, 2);
-                lcd.printString(" Sound", 0, 3);
-                
+                lcd.printString(" ROLLER MAZE ", 0, 0);
+                lcd.printString(" Play game", 0, 2);
+                lcd.printString(" Game options", 0, 3);
+                lcd.printString(">LCD settings", 0, 4);
+                lcd.printString(" Sound", 0, 5);
                 lcd.refresh();
                 
-                
                 if  ((pad.check_event(Gamepad::A_PRESSED)     ||
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    wait_ms(250);
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     lcdSettings(lcd, pad, animate);
                 }
@@ -148,11 +143,11 @@
             case 4:
                 
                 lcd.clear();
-                lcd.printString(" Play game", 0, 0);
-                lcd.printString(" Game options", 0, 1);
-                lcd.printString(" LCD settings", 0, 2);
-                lcd.printString(">Sound", 0, 3);
-                
+                lcd.printString(" ROLLER MAZE ", 0, 0);
+                lcd.printString(" Play game", 0, 2);
+                lcd.printString(" Game options", 0, 3);
+                lcd.printString(" LCD settings", 0, 4);
+                lcd.printString(">Sound", 0, 5);
                 lcd.refresh();
                 
                 
@@ -160,9 +155,8 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    wait_ms(250);
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     soundSettings(lcd, pad, animate);
                 }
@@ -199,7 +193,7 @@
                     Gamepad &pad,
                     FXOS8700CQ &device,
                     Animations &animate,
-                    int mazeIndex,
+                    int difficulty,
                     bool tone,
                     float FPS)
 {
@@ -207,33 +201,32 @@
     int y;
     int radius;
     
-    int _mazeIndex = randomMazeIndexGenerator(mazeIndex);
+    _mazeIndex = randomMazeIndexGenerator(_difficulty);
     // generates random number for maze index between different
     // pairs of values with corresponding difficulties
-    _FPS = FPS;
     
-    if (_mazeIndex < 3) {
+    _FPS = 100 - FPS;
+    // from function, low FPS value corresponds to fast gameplay
+    // so perform basic inversion (works when value is between 25 - 75)
+    
+    if (difficulty == 1) {
         
-        x = 5; // place ball in start of screen approx.
+        x = 5; // place ball in appropriate location for each difficulty
         y = 5;
-        radius = 2;
+        radius = 2; // set radius accordingly
     }
     
-    else if ((_mazeIndex == 3) ||
-             (_mazeIndex == 4) ||
-             (_mazeIndex == 5)){
+    else if (difficulty == 2){
         
-        x = 4;
-        y = 3;
+        x = 3;
+        y = 4;
         radius = 1;
     }
     
-    else if ((_mazeIndex == 6) ||
-             (_mazeIndex == 7) ||
-             (_mazeIndex == 8)){
+    else if (difficulty == 3){
         
-        x = 3;
-        y = 4;
+        x = 5;
+        y = 3;
         radius = 0;
     }
     
@@ -244,7 +237,7 @@
         radius = 0;
     }
     
-    
+    // now initialise game with correct game parameters for chosen difficulty
     
     _engine.init(_mazeIndex,    // selects maze difficulty
                  x,             // defines starting x position for ball
@@ -276,7 +269,7 @@
         
         wait_ms(_FPS);
         
-        if (_engine.checkGoal(lcd)){
+        if (_engine.checkGoal()){
             
             animateStickman(lcd, pad, animate);
             exit ++;
@@ -291,29 +284,26 @@
     }
 }
 
-int Menu::randomMazeIndexGenerator(int mazeIndex)
+int Menu::randomMazeIndexGenerator(int difficulty)
 {
-    /// rand() % (max - min+1) + min ///
+    // generates a random number between a pair of values
+    // based on the difficulty level chosen
     
-    _mazeIndex = mazeIndex;
+    _difficulty = difficulty;
     
-    if (_mazeIndex < 3) {
+    if (_difficulty == 1) {
         
         _mazeIndex = rand() % 3;    // random number between 0 and 2
     }
     
-    else if ((_mazeIndex == 3) ||
-             (_mazeIndex == 4) ||
-             (_mazeIndex == 5)){
+    else if (difficulty == 2){
         
-        _mazeIndex = rand() % 6 + 3; // random number between 3 and 5
+        _mazeIndex = rand() % 3 + 3; // random number between 3 and 5
     }
     
-    else if ((_mazeIndex == 6) ||
-             (_mazeIndex == 7) ||
-             (_mazeIndex == 8)){
+    else if (difficulty == 3){
         
-        _mazeIndex = rand() % 9 + 6; // random number between 6 and 8
+        _mazeIndex = rand() % 3 + 6; // random number between 6 and 8
     }
     
     else {
@@ -340,7 +330,9 @@
     float _fps = FPS;
     
     // printf out all game parameters
-    printf("Game parameters: \nMaze Index = %i \n", _mazeIndex);
+    printf("Game parameters: \n");
+    printf("Difficulty = %i \n", _difficulty);
+    printf("Maze Index = %i \n", _mazeIndex);
     printf("Ball position = (%i, %i)\n", _x, _y);
     printf("Ball radius = %i \n", _radius);
     printf("Game speed = %f \n", _fps);
@@ -377,6 +369,9 @@
             (d == N)  ||
             (d == NE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected -= 1;
         }
         
@@ -384,6 +379,9 @@
             (d == S)  ||
             (d == SE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected += 1;
         }
         
@@ -409,7 +407,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     difficultyOptions(lcd, pad);
                 }
@@ -436,7 +434,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     ballColourOptions(lcd, pad, animate);
                 }
@@ -462,7 +460,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     controlOptions(lcd, pad, animate);
                 }
@@ -488,7 +486,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     speedSettings(lcd, pad);
                 }
@@ -527,6 +525,9 @@
             (d == N)  ||
             (d == NE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected -= 1;
         }
         
@@ -534,6 +535,9 @@
             (d == S)  ||
             (d == SE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected += 1;
         }
         
@@ -557,7 +561,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     lcdBackgroundColour(lcd, pad, animate);
                 }
@@ -581,7 +585,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     lcdInverseColour(lcd, pad);
                 }
@@ -608,81 +612,53 @@
 
 void Menu::soundSettings(N5110 &lcd, Gamepad &pad, Animations &animate)
 {
-    int selected = 0;
     int exit = 0;
     
     while (exit == 0){
         
+        // animate.soundSwitch(lcd, _tone);
+        
         char d = pad.get_direction();
         
         if (d == E){
             
-            selected -= 1;
+            _tone = false;
         }
         
         if (d == W){
             
-            selected += 1;
+            _tone = true;
+            pad.tone(G_NOTE, 0.20);
+            
         }
         
+        lcd.clear();
+        // draw casing rectangle
+        lcd.drawRect(33, 27, 17, 9, FILL_TRANSPARENT);
+        
+        lcd.printString("Sound Settings", 0, 0);
+        lcd.printString("  ON     OFF  ", 0, 2);
+        
+        // casing rectangle is filled in one side
+        // depending on value of _tone
+        if (_tone){
+            lcd.drawRect(34, 28, 7, 7, FILL_BLACK);
+        }
+        else {
+            lcd.drawRect(42, 28, 7, 7, FILL_BLACK);
+        }
+        
+        lcd.refresh();
+        
+        wait_ms(10);
+        
         
-        switch (selected) {
-                
-            case 1:
-                
-                lcd.clear();
-                // displays options page with indicator on first
-                lcd.printString("Sound Settings", 0, 0);
-                lcd.printString("Turn sound on", 0, 2);
-                animate.soundSwitch(lcd, _tone);
-                lcd.refresh();
-                
-                wait_ms(250);
-                
-                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
-                      pad.check_event(Gamepad::JOY_PRESSED))) {
-                    
-                    _tone = true;
-                    animate.soundSwitch(lcd, _tone);
-                    pad.tone(261.63, 0.20);
-                }
-                
-                wait_ms(250);
-                
-                break;
-                
-            case 2:
-                
-                lcd.clear();
-                // displays options page with indicator on first
-                lcd.printString("Sound Settings", 0, 0);
-                lcd.printString("Turn sound off", 0, 2);
-                animate.soundSwitch(lcd, _tone);
-                lcd.refresh();
-                
-                wait_ms(250);
-                
-                if  ((pad.check_event(Gamepad::A_PRESSED)     ||
-                      pad.check_event(Gamepad::JOY_PRESSED))) {
-                    
-                    _tone = false;
-                    animate.soundSwitch(lcd, _tone);
-                }
-                
-                wait_ms(250);
-                
-                break;
-                
-            default:
-                
-                selected = 1;
-                
-                break;
-        }
-        
         if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
-            (d == W)){
+            (pad.check_event(Gamepad::A_PRESSED))){
             
+            if (_tone){
+                pad.tone(C_NOTE, 0.20);
+            }
             exit++;
         }
     }
@@ -702,11 +678,17 @@
         
         if (d == W){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected--;
         }
         
         if (d == E){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected++;
         }
         
@@ -727,10 +709,12 @@
                     pad.check_event(Gamepad::JOY_PRESSED)){
                     
                     _control = true;
-                    animateJoystick(lcd, animate);
+                    
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
+                    
+                    animateJoystick(lcd, animate);
                     exit++;
                 }
                 
@@ -752,10 +736,12 @@
                     pad.check_event(Gamepad::JOY_PRESSED)){
                     
                     _control = false;
-                    animateGamepad(lcd, animate);
+                    
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
+                    
+                    animateGamepad(lcd, animate);
                     exit++;
                 }
                 
@@ -792,6 +778,9 @@
             (d == N)  ||
             (d == NE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected -= 1;
         }
         
@@ -799,6 +788,9 @@
             (d == S)  ||
             (d == SE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected += 1;
         }
         
@@ -820,10 +812,10 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    _mazeIndex = 1;
+                    _difficulty = 1;
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     lcd.clear();
@@ -853,10 +845,10 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    _mazeIndex = 3;
+                    _difficulty = 2;
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     lcd.clear();
@@ -886,10 +878,10 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    _mazeIndex = 6;
+                    _difficulty = 3;
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     lcd.clear();
@@ -919,10 +911,10 @@
                       pad.check_event(Gamepad::JOY_PRESSED))  ||
                      (d == E)) {
                     
-                    _mazeIndex = 9;
+                    _difficulty = 4;
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     lcd.clear();
@@ -966,6 +958,9 @@
             (d == N)  ||
             (d == NE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected -= 1;
         }
         
@@ -973,6 +968,9 @@
             (d == S)  ||
             (d == SE)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             selected += 1;
         }
         
@@ -995,7 +993,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     _colour = true;     // true colour => outline
@@ -1023,7 +1021,7 @@
                      (d == E)) {
                     
                     if (_tone){
-                        pad.tone(196.00, 0.20);
+                        pad.tone(C_NOTE, 0.20);
                     }
                     
                     _colour = false;    // false colour => solid
@@ -1067,7 +1065,7 @@
             
             lcd.inverseMode();
             if (_tone){
-                pad.tone(196.00, 0.20);
+                pad.tone(G_NOTE, 0.20);
             }
             
         }
@@ -1077,7 +1075,7 @@
             lcd.normalMode();
             
             if (_tone){
-                pad.tone(196.00, 0.20);
+                pad.tone(G_NOTE, 0.20);
             }
         }
         
@@ -1125,6 +1123,9 @@
         if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
             (d == W)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             exit++;
         }
     }
@@ -1172,6 +1173,9 @@
         if ((pad.check_event(Gamepad::BACK_PRESSED)) ||
             (d == W)){
             
+            if (_tone){
+                pad.tone(G_NOTE, 0.20);
+            }
             exit++;
         }
     }