A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Revision:
5:a0e4a67266d6
Parent:
4:c6305031f80d
Child:
6:fa2928bcec17
--- a/main.cpp	Tue Apr 14 02:06:45 2015 +0000
+++ b/main.cpp	Tue Apr 14 19:06:53 2015 +0000
@@ -419,6 +419,8 @@
     // Power indicator - RED LED
     ledR = 1;
 
+    int exitFlag = 0;
+
     // initialize time seed for psuedo randomaisation
     srand (time(NULL));
 
@@ -443,13 +445,15 @@
 
     /////// Menu Selection Loop ////////////////////////////////////////////////////////////
 
-    // options counter
-    int option = 0;
+    // option counters
+    int mainOption = 0;//counter for main menu
+    int exitOption = 0;//counter for exit menu
 
     // loop 1 - menu
     while(1) {
 
         mainMenu();
+        actionButton();
 
         // joystick selection
         if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
@@ -459,14 +463,14 @@
             // option up
             if (joystick.direction == UP) {
                 serial.printf(" UP\n");
-                option = option--;
-                if (option < 0)option = 0;
+                mainOption = mainOption--;
+                if (mainOption < 0)mainOption = 0;
             }
             // option down
             if (joystick.direction == DOWN) {
                 serial.printf(" DOWN\n");
-                option = option++;
-                if (option > 2)option = 2;
+                mainOption = mainOption++;
+                if (mainOption > 2)mainOption = 2;
             }
             // Centre / Unknown orientation
             if (joystick.direction == CENTRE)
@@ -475,15 +479,15 @@
                 serial.printf(" Unsupported direction\n");
 
             // 'Play Game' option 1
-            if (option == 0) {
+            if (mainOption == 0) {
                 lcd.printString("Play Game",3,4);
             }
             // 'High Scores' option 2
-            if (option == 1) {
+            if (mainOption == 1) {
                 lcd.printString("  Scores ",3,4);
             }
             // 'Options' option 3
-            if (option == 2) {
+            if (mainOption == 2) {
                 lcd.printString(" Options ",3,4);
             }
         }
@@ -491,7 +495,7 @@
 //////////// Game Loop /////////////////////////////////////////////////////////////
 
         // 'Play Game' selected
-        if ((option == 0)&&(buttonA == 1)) {
+        if ((mainOption == 0)&&(buttonA == 1)) {
 
             lcd.clear();
 
@@ -541,19 +545,22 @@
                     if (joystick.direction == UNKNOWN)
                         serial.printf(" Unsupported direction\n");
 
-                    // escape code (character hit)
-                    int x = 0;
+                    // integer to represent character being
+                    // struck by falling object
+                    int contactPoint = 0;
 
                     // contact points
                     if(lcd.getPixel((a1+4),32))
-                        x++;
+                        contactPoint++;
                     if(lcd.getPixel((a1),32))
-                        x++;
+                        contactPoint++;
                     if(lcd.getPixel((a7),32))
-                        x++;
+                        contactPoint++;
 
-                    // game over
-                    if ( x !=0) {
+                    // if contact point is not zero
+                    // character has been hit
+                    // and the game ends
+                    if ( contactPoint !=0) {
                         lcd.printString("Game Over!!",8,25);
                         wait(0.5);
                         lcd.inverseMode();
@@ -569,30 +576,83 @@
                     }
                     startrek();//clears unset pixels, keeps set pixels
 
-                    // escape code (Back button pressed)
+///////////////////// Exit Menu (Back button pressed)///////////////////////////////////////////
+
                     if(buttonB == 1) {
-                        backButton();
-                        resetGame();//resets co-ordinates
-                        break;
+
+                        // set exit menu
+                        lcd.clear();
+                        lcd.drawRect(8,6,70,30,0);//title outline
+                        lcd.printString("Exit Game?",10,25);
+                        backGround();
+
+                        while(1) {
+
+                            if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
+                                printFlag = 0;
+                                serial.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button);
+
+                                // check joystick direction
+                                if (joystick.direction == LEFT) {
+                                    serial.printf(" LEFT\n");
+                                    exitOption--;
+                                    if(exitOption < 0)exitOption = 0;
+                                }
+                                if (joystick.direction == RIGHT) {
+                                    serial.printf(" RIGHT\n");
+                                    exitOption++;
+                                    if(exitOption > 1)exitOption = 1;
+                                }
+                                if (joystick.direction == CENTRE)
+                                    serial.printf(" CENTRE\n");
+                                if (joystick.direction == UNKNOWN)
+                                    serial.printf(" Unsupported direction\n");
+                            }
+                            // draws option cursor
+                            if(exitOption == 0) {
+                                lcd.printString("YES",28,27);
+                            }
+                            if(exitOption == 1) {
+                                lcd.printString("NO ",28,27);
+                            }
+
+                            // exits game
+                            if((buttonA == 1)&&(exitOption == 0)) { //returns to menu
+                                actionButton();
+                                lcd.clear();//clears screen
+                                resetGame();//resets scores/objects
+                                exitFlag = 1;//sets exit flag
+                                break;
+                            }
+                            // returns to game
+                            if((buttonA == 1)&&(exitOption == 1)) {
+                                break;
+                            }
+                        }
+                        // GAME LOOP
+
+                        if (exitFlag!=0) { //if exit flag set
+                            exitFlag = 0;//reset flag
+                            break;//break to main menu
+                        }
                     }
                     serial.printf("Score: %i \n",score);
-
-                }//if
-            }//while
-        }//if
+                }
+            }
+        }
 
 ////////// High Scores Loop //////////////////////////////////////////////////
 
-        if((option == 1)&&(buttonA == 1)) {
+        if((mainOption == 1)&&(buttonA == 1)) {
 
             actionButton();
             backButton();
-            
+
             lcd.clear();//clear screen
             lcd.drawRect(3,6,77,10,0);//title outline
             backGround();//set background
             lcd.printString("High Scores",10,7);//title
-            
+
             while(1) {
 
                 actionButton();
@@ -608,22 +668,20 @@
 
 ////////// OPTIONS LOOP /////////////////////////////////////////////////////
 
-        if((option == 2)&&(buttonA == 1)) {
+        if((mainOption == 2)&&(buttonA == 1)) {
 
             actionButton();
             backButton();
 
             lcd.clear();//clear screen
-            
+
             lcd.drawRect(3,6,77,10,0);//title outline
             lcd.drawRect(0,47,84,0,1);//bottom border
             lcd.drawRect(0,0,84,2,1);//top border
             lcd.printString("Options",20,7);//title
             lcd.printString("Difficulty",5,3);//difficulty menu
             lcd.printString("Sound FX",5,4);//sounf fx on / off
-            
-            int o = 0;
-            
+
             while(1) {
 
                 actionButton();